Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Memory allocation problem

 
 
AlabiChin
Guest
Posts: n/a
 
      12-17-2005
Hello,

I noticed that when I dynamically create an array of chars, the
resulting size of the allocated memory block is larger by about 15
bytes than what I specified. 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;
}

The above program causes a crash when destroying the allocated memory
via delete. I suspect the crash problem originates at the time of
allocation (i.e., possible memory corruption at time of allocation).

I am using MS Developer Studio 6.0 (SP 6) on AMD Athlon. I get the same
results on a Pentium 4. Any suggestions or comments on what I am doing
in the code is much appreciated.

TIA
Alabi

 
Reply With Quote
 
 
 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      12-17-2005
AlabiChin napisał:
> Hello,
>
> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified. Here is example code:
>
> char *createTextBuffer(char *buffer, int length)
> {
> buffer = new char[length];
>
> if (buffer == NULL) {
> return NULL;


// new does not return NULL on error, it throws
// so this check is meaningless

> }
> length = strlen(buffer); // Current buffer shows larger size.
> return buffer;
> }
>
> The above program causes a crash when destroying the allocated memory
> via delete.


That's because you should destroy it using 'delete[]',
not 'delete'.

> I suspect the crash problem originates at the time of
> allocation (i.e., possible memory corruption at time of allocation).


I don't think so.

>
> I am using MS Developer Studio 6.0 (SP 6) on AMD Athlon. I get the same
> results on a Pentium 4. Any suggestions or comments on what I am doing
> in the code is much appreciated.


Notice that you don't clear the allocated buffer, therefore
it contains garbage. The function strlen() looks for the
first occurrence of the '\0' terminator, which luckily
falls 'about 15 bytes' after the end of your buffer.

HTH,
- J.
 
Reply With Quote
 
 
 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      12-17-2005
AlabiChin wrote:
> Hello,
>
> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified. 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;
> }
>
> The above program causes a crash when destroying the allocated memory
> via delete. I suspect the crash problem originates at the time of
> allocation (i.e., possible memory corruption at time of allocation).
>
> I am using MS Developer Studio 6.0 (SP 6) on AMD Athlon. I get the same
> results on a Pentium 4. Any suggestions or comments on what I am doing
> in the code is much appreciated.
>
> TIA
> Alabi


You have an uninitialized buffer. Calling strlen with such a buffer is
likely to cause a crash right then and there. strlen does not return
the length of the buffer, it returns the length of a nul-terminated
string that has been stored in the buffer. But you do not have such a
string in the buffer.

--
Scott McPhillips [VC++ MVP]

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-17-2005
AlabiChin wrote:

> Hello,
>
> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified. Here is example code:
>
> char *createTextBuffer(char *buffer, int length)
> {
> buffer = new char[length];
>
> if (buffer == NULL) {


This check is unnecessary. new either returns a valid pointer or (if no
memory is available) throws an exception. It never returns a null pointer.

> return NULL;
> }
> length = strlen(buffer); // Current buffer shows larger size.
> return buffer;
> }


strlen expects a C style string, i.e. a null terminated character array. You
gave it uninitialized memory, which means the behavior is undefined.

> The above program causes a crash when destroying the allocated memory
> via delete. I suspect the crash problem originates at the time of
> allocation (i.e., possible memory corruption at time of allocation).


No, it originates at the time you used strlen on it. Probably, strlen went
past the end of the array to find the null character.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-17-2005
AlabiChin wrote:

> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified.


whilst "new" can be expected to allocate a few more bytes than
requested
(housekeeping) 15 bytes sounds like a lot.


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


you can't do strlen on a newly allocated chunk of memory. How do you
know
"new" returns a nul terminated string?

> return buffer;
> }
>
> The above program causes a crash when destroying the allocated memory
> via delete.


what does your delete look like?


> I suspect the crash problem originates at the time of
> allocation (i.e., possible memory corruption at time of allocation).


I suspect there's something else wrong with your program. Standard
libraries
can have bugs, but always suspect your own (newly written) code first

> I am using MS Developer Studio 6.0 (SP 6) on AMD Athlon. I get the same
> results on a Pentium 4. Any suggestions or comments on what I am doing
> in the code is much appreciated.



--
Nick Keighley

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-17-2005
AlabiChin wrote:
> Hello,
>
> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified. 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;
> }


strlen is a "C" library function which returns the length of a "null
terminated string". In other words, in the current case, it will count
the number of characters from start of buffer till it finds a null
character. Since char is a basic data type, buffer remains
uninitialized, and thus contains a junk. It is incorrect to relie on
strlen.

>
> The above program causes a crash when destroying the allocated memory
> via delete.

This is because usage of new[] must be matched via delete[], not
delete.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-17-2005

"AlabiChin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello,
>
> I noticed that when I dynamically create an array of chars, the
> resulting size of the allocated memory block is larger by about 15
> bytes than what I specified. 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.


strlen looks for the first null character to determine the end of the
string. Since you have not initialized the array, the array contains random
information, and your length will be some arbitary number.

To see this add this line just before your length =

buffer[0] = '\0';

Now your length will be 0 because it will find the null in the first
position.

When you delete this array you use delete[]


 
Reply With Quote
 
Viktor Prehnal
Guest
Posts: n/a
 
      12-17-2005
Hmm, I studied documentation in VS 2003.NET which I consider not too
out-of-date and found out that new returns 0/NULL. If you want exception
handling you need to use set_new_handler function.
From what time new throws exception? What type of exception is it?
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-17-2005
Viktor Prehnal wrote:

> Hmm, I studied documentation in VS 2003.NET which I consider not too
> out-of-date and found out that new returns 0/NULL. If you want exception
> handling you need to use set_new_handler function.


new throws an exception when it fails

> From what time new throws exception? What type of exception is it?


std::bad_alloc


--
Nick Keighley

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-18-2005
Viktor Prehnal wrote:

> Hmm, I studied documentation in VS 2003.NET which I consider not too
> out-of-date and found out that new returns 0/NULL. If you want exception
> handling you need to use set_new_handler function.
> From what time new throws exception?


In Standard C++ (which is existing since 199, this has always been the
case.

> What type of exception is it?


std::bad_alloc

 
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