Memory allocation problem

Discussion in 'C++' started by AlabiChin, Dec 17, 2005.

  1. AlabiChin

    AlabiChin Guest

    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
    AlabiChin, Dec 17, 2005
    #1
    1. Advertising

  2. 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.
    Jacek Dziedzic, Dec 17, 2005
    #2
    1. Advertising

  3. 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]
    Scott McPhillips [MVP], Dec 17, 2005
    #3
  4. AlabiChin

    Rolf Magnus Guest

    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.
    Rolf Magnus, Dec 17, 2005
    #4
  5. 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
    Nick Keighley, Dec 17, 2005
    #5
  6. 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.
    Neelesh Bodas, Dec 17, 2005
    #6
  7. AlabiChin

    Jim Langston Guest

    "AlabiChin" <> wrote in message
    news:...
    > 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[]
    Jim Langston, Dec 17, 2005
    #7
  8. 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?
    Viktor Prehnal, Dec 17, 2005
    #8
  9. 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
    Nick Keighley, Dec 17, 2005
    #9
  10. AlabiChin

    Rolf Magnus Guest

    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 1998), this has always been the
    case.

    > What type of exception is it?


    std::bad_alloc
    Rolf Magnus, Dec 18, 2005
    #10
  11. 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
    Robbie Hatley, Dec 18, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    701
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    605
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,864
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    989
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,187
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page