Solved

Character Arrays or Dynamic memory allocation

Posted on 1998-08-11
12
181 Views
Last Modified: 2010-04-10
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
0
Comment
Question by:Woodster
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1170010
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.
0
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1170011
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170012
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.
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1170013
I completely agree with nietod.  If you know how much you need, a static array is more efficient both in space and time.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170014
Musashi agrees with me.
I agree with Yonat.

Therefore, by the transative property of agreement, Musashi agrees with Yonat.   : - )
0
 
LVL 4

Author Comment

by:Woodster
ID: 1170015
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?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:nietod
ID: 1170016
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.
0
 
LVL 4

Author Comment

by:Woodster
ID: 1170017
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?
0
 
LVL 4

Author Comment

by:Woodster
ID: 1170018
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
0
 
LVL 4

Author Comment

by:Woodster
ID: 1170019
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?


0
 
LVL 5

Expert Comment

by:yonat
ID: 1170020
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170021
>> 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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now