i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app
for example: class test { public: int x;
}
int main(int argc, char **argv) { test *n= new test; . . ... delete n;
return 0;
}
i know that the object created this way is in the heap which have much memory than stack but why they always define objects that way , why not just say "test n" and the object will be destroyed by itself at the end of the program! , instead of using "new" and maybe u will forget to "delete" at the end
Medvedev wrote: > i see serveral source codes , and i found they almost only use "new" > and "delete" keywords to make they object. > Why should i do that , and as i know the object is going to be destroy > by itself at the end of the app
> for example: > class test > { > public: > int x; > }
> int main(int argc, char **argv) > { > test *n= new test; > . > . > ... > delete n;
> return 0; > } > i know that the object created this way is in the heap which have much > memory than stack but why they always define objects that way , why > not just say "test n" and the object will be destroyed by itself at > the end of the program! , instead of using "new" and maybe u will > forget to "delete" at the end
Several reasons.
1. They're coming from Java and they don't know any better 2. They're storing polymorphic objects inside containers 3. They need the lifetime of the object to exceed the scope in which it was declared.
> Medvedev wrote: > > i see serveral source codes , and i found they almost only use "new" > > and "delete" keywords to make they object. > > Why should i do that , and as i know the object is going to be destroy > > by itself at the end of the app
> > for example: > > class test > > { > > public: > > int x; > > }
> > int main(int argc, char **argv) > > { > > test *n= new test; > > . > > . > > ... > > delete n;
> > return 0; > > } > > i know that the object created this way is in the heap which have much > > memory than stack but why they always define objects that way , why > > not just say "test n" and the object will be destroyed by itself at > > the end of the program! , instead of using "new" and maybe u will > > forget to "delete" at the end
> Several reasons.
> 1. They're coming from Java and they don't know any better > 2. They're storing polymorphic objects inside containers > 3. They need the lifetime of the object to exceed the scope in which > it was declared.
Still, I agree with you, people over use new and delete. I recently saw a class library written in C++ that tried to be all C#'ish and required you to write code like:
*STUPID*... if your code is too stupid to decide what kind of object should be created (and keep in mind, I could see doing this for user defined objects, but these were all internal objects), then why would you expect a user of your class to?
Plus, that hurts performance.
"Medvedev" <3D.v.Wo...@gmail.com> wrote in message
> On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote: >> Medvedev wrote: >> > i see serveral source codes , and i found they almost only use "new" >> > and "delete" keywords to make they object. >> > Why should i do that , and as i know the object is going to be destroy >> > by itself at the end of the app
>> > for example: >> > class test >> > { >> > public: >> > int x; >> > }
>> > int main(int argc, char **argv) >> > { >> > test *n= new test; >> > . >> > . >> > ... >> > delete n;
>> > return 0; >> > } >> > i know that the object created this way is in the heap which have much >> > memory than stack but why they always define objects that way , why >> > not just say "test n" and the object will be destroyed by itself at >> > the end of the program! , instead of using "new" and maybe u will >> > forget to "delete" at the end
>> Several reasons.
>> 1. They're coming from Java and they don't know any better >> 2. They're storing polymorphic objects inside containers >> 3. They need the lifetime of the object to exceed the scope in which >> it was declared.
Medvedev wrote: > On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote: >> 3. They need the lifetime of the object to exceed the scope in which >> it was declared.
> how u can use object after it's scope ends!!
I don't know about "u", but the rest of us don't.
A pointer to a dynamically allocated object can be returned from the function that created it, or declared in an outer scope and assigned in an inner one.
On Jul 5, 1:13 pm, "Somebody" <someb...@cox.net> wrote:
> Still, I agree with you, people over use new and delete. I recently saw a > class library written in C++ that tried to be all C#'ish and required you to > write code like:
It's not obvious what you say that. Is it because the dynamic objects are not handed to smart pointers right away? Or do you propose something like the following?
Medvedev wrote: > i see serveral source codes , and i found they almost only use "new" > and "delete" keywords to make they object.
If you write an OO program, you will find yourself needing a base class pointer that points to a derived class. (That is the very goal of "OO" - to override some critical method into that derived class.)
Otherwise, you could construct an object on the stack, use it, and let it destroy when its method returns.
Because you need dynamically sized and typed objects, you must sometimes new them.
> Why should i do that , and as i know the object is going to be destroy > by itself at the end of the app
You should code as if you don't know that. Always clean up after yourself. One good way is with "smart pointers".
> for example: > class test > { > public: > int x; > }
> int main(int argc, char **argv) > { > test *n= new test; > . > . > ... > delete n;
> return 0; > } > i know that the object created this way is in the heap which have much > memory
Not necessarily. On modern architectures with virtual memory, both the heap and stack can grow arbitrarily.
> than stack but why they always define objects that way , why
> not just say "test n" and the object will be destroyed by itself at > the end of the program! , instead of using "new" and maybe u will > forget to "delete" at the end
Because that's one of the many things C++ will let you do that are sloppy. Don't do any of them, because any one of them could come back to bite you on the butt.
For example, you could refactor the n = new test and move it inside a working loop. Then the loop would silently leak (virtual!) memory. You would not notice until your program ran for hours, and got very slow.
> On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:
> > Medvedev wrote: > > > i see serveral source codes , and i found they almost only use "new" > > > and "delete" keywords to make they object. > > > Why should i do that , and as i know the object is going to be destroy > > > by itself at the end of the app
> > > for example: > > > class test > > > { > > > public: > > > int x; > > > }
> > > return 0; > > > } > > > i know that the object created this way is in the heap which have much > > > memory than stack but why they always define objects that way , why > > > not just say "test n" and the object will be destroyed by itself at > > > the end of the program! , instead of using "new" and maybe u will > > > forget to "delete" at the end
> > Several reasons.
> > 1. They're coming from Java and they don't know any better > > 2. They're storing polymorphic objects inside containers > > 3. They need the lifetime of the object to exceed the scope in which > > it was declared.
> how u can use object after it's scope ends!!
Thats not what he stated. The object is _declared_ in a finite scope. If you allocate the object on the heap, it's lifetime no longer relies on the declaring scope.
Basicly, new and new[] transfers the responsability to you, the programmer, to delete and delete[].
Is using new and new[] a good habit? no, its not. You'll find C++ programmers to be retiscent in using it and would rather rely on a smart pointer if heap allocation is indeed required. Java programmers don't really have a choice but in C++ an automatic variable should and usually is the default.
Generally speaking, if you see new and new[], you aren't reading a programmer who has his roots in modern C++. Allocating on the heap what should be automatic is frowned upon here.
And the reason for that is because smart pointers have much to offer as long as you know their limitations. Good examples of those are std::auto_ptr and boost::shared_ptr to name a few.
> On Jul 5, 1:13 pm, "Somebody" <someb...@cox.net> wrote:
>> Still, I agree with you, people over use new and delete. I recently saw a >> class library written in C++ that tried to be all C#'ish and required you >> to >> write code like:
> It's not obvious what you say that. Is it because the dynamic objects > are not handed to smart pointers right away? Or do you propose > something like the following?
So they had a UI control, and they were inserting items into it. ButtonTypeX was a class *the class library* defined... not something that a *user* of the class library would (or could) define.
What I was saying... was I failed to see why I should do the dirty work for the library and not only determine what class to use, but also to allocate it for them. They should determine whether to create the internal ButtonTypeA, ButtonTypeB, ButtonTypeC, etc. in some other way (like a param for example)... so I'd rather see something like:
Like other posters said, that style of code is C#'ish or Java'ish... it is not typical C++ style. Unless you are doing some type of class factory type thing.
Allocating memory is slow... which is why this particular UI library was commonly regarded as having poor performance.
> Medvedev wrote: > > i see serveral source codes , and i found they almost only > > use "new" and "delete" keywords to make they object. Why > > should i do that , and as i know the object is going to be > > destroy by itself at the end of the app > > for example: > > class test > > { > > public: > > int x; > > } > > int main(int argc, char **argv) > > { > > test *n= new test; > > . > > . > > ... > > delete n; > > return 0; > > } > > i know that the object created this way is in the heap which > > have much memory than stack but why they always define > > objects that way , why not just say "test n" and the object > > will be destroyed by itself at the end of the program! , > > instead of using "new" and maybe u will forget to "delete" > > at the end > Several reasons. > 1. They're coming from Java and they don't know any better > 2. They're storing polymorphic objects inside containers
Not just storing them inside containers. I've a couple of places where I've code something like:
std::auto_ptr< Base > obj( someCondition ? static_cast< Base* >( new D1 ) : static_cast< Base* >( new D2 ) ) ;
It's not that common, however.
> 3. They need the lifetime of the object to exceed the scope in which > it was declared.
Often, the last two reasons go together: although there's no formal link between them, in practice, polymorphic objects tend to have arbitrary lifetimes.
Note that you normally would prefer copying an object to extending its lifetime, if the object supports copy.
-- James Kanze (GABI Software) email:james.ka...@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Jul 5, 10:05 pm, Medvedev <3D.v.Wo...@gmail.com> wrote:
> On Jul 5, 11:59 am, red floyd <no.spam.h...@example.com> wrote:
[...]
> > > i know that the object created this way is in the heap > > > which have much memory than stack but why they always > > > define objects that way , why not just say "test n" and > > > the object will be destroyed by itself at the end of the > > > program! , instead of using "new" and maybe u will forget > > > to "delete" at the end > > Several reasons. > > 1. They're coming from Java and they don't know any better > > 2. They're storing polymorphic objects inside containers > > 3. They need the lifetime of the object to exceed the scope in which > > it was declared. > how u can use object after it's scope ends!!
Objects don't have scope, they have lifetime. Scope concerns the visibility of a declaration (and is linked with the structure of the program). Lifetime concerns when the object comes into and goes out of being. C++ defines several different types of lifetime, some linked to scope (e.g. automatic), and others not (e.g. dynamic). If you create an object with new, it has dynamic lifetime, and exists until you delete it. Which could be somewhere else entirely.
-- James Kanze (GABI Software) email:james.ka...@gmail.com Conseils en informatique orientée objet/ Beratung in objektorientierter Datenverarbeitung 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34