Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clarification... 1

Status
Not open for further replies.

LiquidBinary

Programmer
Jul 27, 2001
148
US
What are the advantages to say ->

void someFun()
{
char *sString = new char[MAX];

//Some code.

delete [] sString;
}

VS.

void someFun()
{
char sString[MAX];

//Some code.

}

I am having a hard time finding clarification on something...When the second function ends, do the contents (if any) of sString just get discarded, or does the entire memory space get de-allocated? If only the contents of sString get discarded (not the memory space) then the first function would be justified, right? Is there really any sense in dynamically allocating an array if you all ready know the MAX size before hand? Mike L.G.
mlg400@blazemail.com
 
If you only allocate the one array (or a known fixed number of arrays), know its size and don't need it after return from the function, the second variation is more efficient. Otherwise heap allocation is most likely better. In the second case the entire contents of the string is stored in the stack frame for the function call, and is released at the end of the function. :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top