Automatic storage vs Free store

J

Joe Van Dyk

Is there a general rule of thumb of when to use automatic storage (dunno
if that terminology is correct) and when to use the free store?

Thanks,
Joe
 
T

Thomas J. Gritzan

Joe said:
Is there a general rule of thumb of when to use automatic storage (dunno
if that terminology is correct) and when to use the free store?

Use a bike or walk everywhere, if you can,
if you can't, use your car. That way you save gas.

In C++:

Use automatic storage when possible,
only use new/delete when you have to.

And prefer references over pointers, same idea.

Isn't this a FAQ issue?
 
J

Jim Langston

Thomas J. Gritzan said:
Use a bike or walk everywhere, if you can,
if you can't, use your car. That way you save gas.

In C++:

Use automatic storage when possible,
only use new/delete when you have to.

And prefer references over pointers, same idea.

Isn't this a FAQ issue?

I would add, for extremely large objects (large arrays, etc..) you probably
want to use the free store instead of automatic storage. The reason being
for very large arrays and such the stack just isn't big enough.

int MyBigArray[1000000]
probably will crash when you try to run it complaining about out of stack
space since that's 8 megs.

int MyBigArray[];
MyBigArray = new int[1000000]
may work as long as it can grab 8 megs from the free store
 
G

Gavin Deane

Jim said:
Thomas J. Gritzan said:
Use a bike or walk everywhere, if you can,
if you can't, use your car. That way you save gas.

In C++:

Use automatic storage when possible,
only use new/delete when you have to.

And prefer references over pointers, same idea.

Isn't this a FAQ issue?

I would add, for extremely large objects (large arrays, etc..) you probably
want to use the free store instead of automatic storage. The reason being
for very large arrays and such the stack just isn't big enough.

int MyBigArray[1000000]
probably will crash when you try to run it complaining about out of stack
space since that's 8 megs.

int MyBigArray[];
MyBigArray = new int[1000000]
may work as long as it can grab 8 megs from the free store

But in that case prefer an object that handles the memory management
for you rather than raw pointers an arrays.

std::vector<int> MyBigVector(1000000);

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top