Link to home
Start Free TrialLog in
Avatar of Woodster
WoodsterFlag for Australia

asked on

Character Arrays or Dynamic memory allocation

This is related more to good programming than a problem. When working with strings, I generally new a pointer to a given number of characters and then delete it when it is no longer required within a function, with the new and delete[] easily being ablt to be replaced with just a character array.  Is there a preferred method for this or is one seen to be better than the other.  Obviously, new'ed pointers need to be used if the life of the string is variedbut in instances such as below where it is just used within a function, which is the better method.

void MyFunction1(...)
{
   char *string = new char[FixedLength];
   // Other processing on string variable here.
   delete[] string
}

could just as easily be replaced by:

void MyFunction2(...)
{
   char string[FixedLength];
   // Other processing on string variable here.
}

Are there any implications of either of these methods that I should be aware of before making a function specific decision or is it more of a general preference?

Any information would be greatly appreciated.

Sean Hannan
Avatar of yonat
yonat

new and delete calls have the following liabilities:
1. They are time costly.
2. new may fail.

On the other hand, static allocation cannot be used if the array (string) size is not known at compile time, as you said.

The preferd method would be to use a std::vector, std::string or similar class. These classes allow dynamic size, and they usually come with allocators that are less time costly and less likely to fail. So you gain both performance and flexibility.
ASKER CERTIFIED SOLUTION
Avatar of yonat
yonat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
New and delete are used so much that every effort is made to mafe them as fast as possibly and they do an amazing job.  You can allocate and delete 1000s of times a second.  However, if you ever traced through new or delete with your debugger I think you would be shocked to see how much work is being done to provide you that memory.  In most cases, I would guess that more time is being spent providing you with mememory than you spend using it.   If you can use a static array rather than new and delete (without having to use a rediculously large array "to be safe") then do so.  The code will run MUCH faster.
I completely agree with nietod.  If you know how much you need, a static array is more efficient both in space and time.
Musashi agrees with me.
I agree with Yonat.

Therefore, by the transative property of agreement, Musashi agrees with Yonat.   : - )
Avatar of Woodster

ASKER

Musashi = nietod = Yonat;
// I'm glad we got that one sorted out!

So, we have sorted out that it is best to use static arrays in this situation.

On to the other oringinal suggestion from Yonat.  Coming from a C background, I have a lot of C habits and as a result, never quite got the hang of templates and so on, preferring to use my own code instead of the templates as defined in the Standard Template Library.  Looking at Visual C++ help on string/vector (I assume std:: referes to the Standard Template Library) classes, it doesn't tell me how they should be used.  Where should be looking to get more help on these?
I like Bjarne Stroustrup's book "The C++ programming Language" currently in its 3rd edition.  It doesn't teach programming.  But you already know how to programm.  It teaches how to use the C++ languange, including the template libraries.

P.J. Plaugar also has a book on the C++ "draft" standard template library.  This is more indepth on the template library, but might be overkill.
Musashi = nietod = Yonat;
// I'm glad we got that one sorted out!

So, we have sorted out that it is best to use static arrays in this situation.

On to the other oringinal suggestion from Yonat.  Coming from a C background, I have a lot of C habits and as a result, never quite got the hang of templates and so on, preferring to use my own code instead of the templates as defined in the Standard Template Library.  Looking at Visual C++ help on string/vector (I assume std:: referes to the Standard Template Library) classes, it doesn't tell me how they should be used.  Where should be looking to get more help on these?
Ooops - Pressed Reload on the form and posted that last comment again!  Darn.

I've got a previous version of Stroustrup, obviously before the STL.  I wondered how lopng it would be before I had to update that book - I guess now is the time.  Thanks for the reference though
One other point to note.  Will arrays of characters be allocated from the stack or will they be positioned in far memory?  In previous versions of VIsual C++, there was a compiler switch that let me specify that all allocations over a certain size would be in far memory which cam in very handy for strings but this appears to be removed in Visual C++ 5.0.

Obviously, have strings being allocated from the stack will soon eat up stack space in a recursive function.  Any advice on this?


Here's my list of online STL Tutorials:
http://www.research.att.com/~bs/3rd_tour2.pdf (from Bjarne Stroupstrup's 3rd ed.)
http://www.sgi.com/Technology/STL/stl_introduction.html
http://www.objectplace.com/technology/articles/9508.shtml
http://www.eckelobjects.com/stlsimpl.html
http://www.yrl.co.uk/~phil/stl/stl.htmlx
http://www.cs.brown.edu/people/jak/programming/stl-tutorial/tutorial.html
http://www.infosys.tuwien.ac.at/Research/Component/tutorial/prwmain.htm
http://web.ftech.net/~honeyg/articles/eff_stl.htm

As for where the static arrays are positioned: I am not familiar with the switch to put them in far memory, but in win32 programming all this is not important. The stack can grow to whatever size your application needs, and memory looks "flat" - there is no need for near and far.
>> Will arrays of characters be allocated from the stack or
>> will they be positioned in far memory?
It is safe to assume that local variables of any type will be allocated on the stack.  Variables that are allocated with new will be located on one or more heaps.  Both the stack and the heaps can grow to extremely large sizes under win32 so you usually don't have to be concerned about running out of either.