Advertisement

Simple C++ Question.

Started by February 27, 2000 12:28 AM
8 comments, last by Esap1 24 years, 9 months ago
When using a class you have made, whats the difference from going like this: World_class World1; And Just using the class, as opposed to this: World_class *World1; World1 = new World_class; I dont see any benifit from using a pointer and using new, what the difference and whats are the benifits if any, Thanks a lot
The big difference is that
World_class World1;
will create the structure on the stack whereas
World_class * World1 = new World_class();
will allocate the data in the heap. Stack structures are automatically deallocated when the function goes out of scope, so you can''t pass it to other functions that have a longer life span than the stack. On the plus side they don''t fragment the heap, are less likely to cause cache misses when accessed and take up slightly less room (1 pointer''s worth.)
Advertisement
So probaly safer just to do the pointer way?

Oh, and one more, to call a constructer you would just go

World1; //or whatever your class is

And To call a Destructor would you just go like this?

~World1;

and that would call the function in your class ~World1 and do what? I mean it calls ~World1 and then does what?

Edited by - Esap1 on 2/27/00 1:08:13 AM
You don''t need to call your destructor when it''s on the stack (not a pointer). If you use the stack, all you have to do is declare it, and it''s ready to use, and will be deallocated automatically when it goes out of scope. If it''s on the heap, then you allocate it by calling

World_class* World1 = new World_class;

then when you want to get rid of it, you have to call

delete World1;

that will call the destructor. You have to be careful to always call delete though, or else you will have a memory leak, and unless Windows takes care of it, that memory will be GONE until you restart.

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
another thing to mention is that the stack is limited, whereas the heap can be dynamically sized by windows pagefile. am i right?
No, the stack can expand throughout the memory space. In old DOS days the stack was limited to 64K, but the heap, depending on your memory model could exceed that. However, in the flat memory architecture of Win32, the stack can extend for the full address space.
Advertisement
So really it boils down to how long you need the class instance, or if you need to pass it to a function, or if you need to switch pointer types.

I think.


Lack
Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
Don''t forget to use delete [] when you create a array of World_class with new.

E.g.:

if you use:

World_class * object=new World_class;

it''s ok to use

delete object;

but if you use :

World_class * object=new World_class[10];

you must use :

delete [] object;

I felt like telling you this, cuz'' it can really f*** up your project ... (memory leaks are the worst).
Hey SiCrane, although I''ve learned not to dispute you (the hard way ), are you certain the stack can grow? I''m sure you can set the stack size for a Win32 program, and the default is a 1 Meg stack. I''ve never seen anything to lead me to believe it will resize it.

Rock
quote: Original post by Rock2000

Hey SiCrane, although I''ve learned not to dispute you (the hard way ), are you certain the stack can grow? I''m sure you can set the stack size for a Win32 program, and the default is a 1 Meg stack. I''ve never seen anything to lead me to believe it will resize it.

Rock

Positive. I can''t point you towards a spot that says definatively the behavior of the windows memory model, but here are some sources that support the idea that windows stack grows:
1.) documentation for the /Gs switch has the following blurb:
quote:
The default value of size is carefully chosen to allow the program stack of applications for Win32 to grow at run time. Do not change the default setting of /Gs unless you know exactly why you need to change it.

2) the PE executable format has fields for the StackReserve and StackCommit which controls how stack memory is intially allocated, which implies that stack memory can change.
3) the _chkstk() function is called when stack grows by more than a threshold value (default 4K)

I''m not sure where the 1 meg value is coming from. The default starting stack size is 1 page (4K) and the reserved stack size is 16 pages (64K). And seeing as how I''ve pushed objects much larger than 4K on the stack before, I''m pretty sure that the stack can grow.

However, the application often will do stack checks to make sure that the stack isn''t overflowing, hence the /Gs switch and the _chkstk() function. That way it can check for runaway recursion, etc. It could be the 1 meg limit you''re refering do is the default overflow value. However, with a flip of a /Gs switch, you can turn it off, and the application will have a stack that can grow throughout the memory space.

But, I''m not advocating this by any means!

This topic is closed to new replies.

Advertisement