Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory allocation problem

Reply
Thread Tools

Memory allocation problem

 
 
Robbie Hatley
Guest
Posts: n/a
 
      12-18-2005
AlabiChin wrote:

> I noticed that when I dynamically create an array of chars


Why would you want to do that?

> the resulting size of the allocated memory block
> is larger by about 15 bytes than what I specified.


Why would you care?

> Here is example code:
>
> char *createTextBuffer(char *buffer, int length)
> {
> buffer = new char[length];
>
> if (buffer == NULL) {
> return NULL;
> }
> length = strlen(buffer); // Current buffer shows larger size.
> return buffer;
> }



Yuck!


Here's my version of your code:

{ // Begin block; stuff declared in here dies at end.

// Make a buffer to hold some text:
std::string TextBuffer = "This is some text.";

// Find the length of the text in the buffer, which is
// NOT the same thing as the memory used by the buffer:
int Length = TextBuffer.size();

// (do stuff with TextBuffer and Size)

} // End block; TextBuffer and Size are deallocated here.

In my version, allocation and deallocation are always done
for you automatically. There is no "memory allocation
problem". If you absolutely need a C string version of
TextBuffer (say, to pass to some C std lib function), then
use the .c_str() member function of TextBuffer:

int CSize = strlen(TextBuffer.c_str());

I think you'll find that Size and CSize are then both
equal to 18 (the length of "This is some text.").

> The above program causes a crash.


Of course. For one thing, it's full of errors, as others
here have pointed out. But more importantly, it's C, not
C++. A zero-terminated array of char is a very antiquated
and crude way of handling strings. C++ std::string is much
better. It's easier, safer, and much more versatile, with
loads of cool member functions such as "size" and "find"
and "substr". Time to upgrade your approach, I think.

--
Cheers!
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
memory allocation and freeing memory Rodrigo Dominguez C Programming 11 06-14-2005 11:54 PM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments