Solved

Character Arrays or Dynamic memory allocation

Posted on 1998-08-11
12
206 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

856 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