RE: memory allocation problems updated code

Discussion in 'C++' started by Kieran Simpson, Jul 1, 2003.

  1. I updated the code as per suggestions but the problem still occurs, so I
    though I might post the revised code,
    maybe I stuffed it up somehow.

    The pointer that the memory is allocated to is declared using the following
    code

    char *string_value;

    The following code takes place in the constuctor

    class1::class1(char *new_value) {
    /* set the string size */
    string_size = (strlen(new_value) + 1);

    /* initilize the memory */
    string_value = new char(string_size);

    /* check to see if string_value = NULL
    ie: memory allocation failed
    */
    if (string_value == NULL) {
    /* memory allocation failed */
    string_status = STRING_ERROR;
    return;
    }

    /* set the string value */
    strcpy(string_value, new_value);
    }

    The following takes place in the destructor

    delete string_value;

    In main() the following takes place

    class1 *string = new class1("somestring");

    Everything works fine uptil here, manipulation of various pieces of data and
    functions work perfectly. The problem occurs

    here

    delete string;

    After this I get the debug errors and my program crashes.
    I am using Visual C++ 6 and the message is as follows:
    DAMAGE: after Normal block (#36) at 0x00430210
    Kieran Simpson, Jul 1, 2003
    #1
    1. Advertising

  2. "Kieran Simpson" <> wrote in message
    news:GFdMa.135$...
    > I updated the code as per suggestions but the problem still occurs, so I
    > though I might post the revised code,
    > maybe I stuffed it up somehow.
    >
    > The pointer that the memory is allocated to is declared using the

    following
    > code
    >
    > char *string_value;


    Why are you still not using std::string?

    > The following code takes place in the constuctor
    >
    > class1::class1(char *new_value) {
    > /* set the string size */
    > string_size = (strlen(new_value) + 1);
    >
    > /* initilize the memory */
    > string_value = new char(string_size);
    >
    > /* check to see if string_value = NULL
    > ie: memory allocation failed
    > */



    > if (string_value == NULL) {
    > /* memory allocation failed */
    > string_status = STRING_ERROR;
    > return;
    > }


    The above whole block is incorrect. If new operator fails then an exception
    is thrown (bad_alloc). Otherwise use nothrow, then checking for NULL makes
    sense.
    >
    > /* set the string value */
    > strcpy(string_value, new_value);
    > }
    >
    > The following takes place in the destructor
    >
    > delete string_value;
    >
    > In main() the following takes place
    >
    > class1 *string = new class1("somestring");
    >
    > Everything works fine uptil here, manipulation of various pieces of data

    and
    > functions work perfectly. The problem occurs
    >
    > here
    >
    > delete string;
    >
    > After this I get the debug errors and my program crashes.
    > I am using Visual C++ 6 and the message is as follows:
    > DAMAGE: after Normal block (#36) at 0x00430210


    Why not post the whole code ?Even now what type is string_size is not
    known.
    Josephine Schafer, Jul 1, 2003
    #2
    1. Advertising

  3. Kieran Simpson

    Pete Becker Guest

    Kieran Simpson wrote:
    >
    > string_value = new char(string_size);
    >


    This allocates one char with the value given by string_size, and stores
    the address of that char in string_value. What you need is this:

    string_value = new char[string_size];

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 1, 2003
    #3
  4. Kieran Simpson

    tom_usenet Guest

    On Tue, 1 Jul 2003 20:48:02 +1000, "Kieran Simpson"
    <> wrote:

    >I updated the code as per suggestions but the problem still occurs, so I
    >though I might post the revised code,
    >maybe I stuffed it up somehow.
    >
    >The pointer that the memory is allocated to is declared using the following
    >code
    >
    >char *string_value;
    >
    >The following code takes place in the constuctor
    >
    >class1::class1(char *new_value) {
    > /* set the string size */
    > string_size = (strlen(new_value) + 1);
    >
    > /* initilize the memory */
    > string_value = new char(string_size);


    That only allocates a single char (with the value string_size)! You
    want:

    string_value = new char[string_size];


    >
    > /* check to see if string_value = NULL
    > ie: memory allocation failed
    > */
    > if (string_value == NULL) {
    > /* memory allocation failed */
    > string_status = STRING_ERROR;
    > return;
    > }
    >
    > /* set the string value */
    > strcpy(string_value, new_value);
    >}
    >
    >The following takes place in the destructor
    >
    >delete string_value;


    When you use new[], you must use delete[] (although some compilers
    will let you get away with it under certain circumstances):

    delete[] string_value;

    >In main() the following takes place
    >
    >class1 *string = new class1("somestring");


    Why not:

    class1 string("somestring");

    Tom
    tom_usenet, Jul 1, 2003
    #4
  5. Kieran Simpson

    Gavin Deane Guest

    "Kieran Simpson" <> wrote in message news:<GFdMa.135$>...
    > I updated the code as per suggestions but the problem still occurs, so I
    > though I might post the revised code,
    > maybe I stuffed it up somehow.
    >
    > The pointer that the memory is allocated to is declared using the following
    > code
    >
    > char *string_value;
    >
    > The following code takes place in the constuctor
    >
    > class1::class1(char *new_value) {
    > /* set the string size */
    > string_size = (strlen(new_value) + 1);
    >
    > /* initilize the memory */
    > string_value = new char(string_size);
    >
    > /* check to see if string_value = NULL
    > ie: memory allocation failed
    > */
    > if (string_value == NULL) {
    > /* memory allocation failed */
    > string_status = STRING_ERROR;
    > return;
    > }
    >
    > /* set the string value */
    > strcpy(string_value, new_value);
    > }
    >
    > The following takes place in the destructor
    >
    > delete string_value;
    >
    > In main() the following takes place
    >
    > class1 *string = new class1("somestring");
    >
    > Everything works fine uptil here, manipulation of various pieces of data and
    > functions work perfectly. The problem occurs
    >
    > here
    >
    > delete string;
    >
    > After this I get the debug errors and my program crashes.
    > I am using Visual C++ 6 and the message is as follows:
    > DAMAGE: after Normal block (#36) at 0x00430210


    The actual code, rather than descriptions in English interspersed with
    code snippets would help. However you have:

    string_value = new char(string_size);

    which allocates a *single* char (initialised with the value of
    string_size) and later you have

    strcpy(string_value, new_value);

    trying to copy an entire string into a single char. Which won't work.

    I expect you want this in your constructor:

    string_value = new char[string_size];
    Note square brackets ^ ^

    and this in your destructor:

    if (string_status != STRING_ERROR)
    {
    delete [] string_value;
    }
    Note square brackets again.

    It is a quirk of VC++6 that new returns 0 if it fails. Standard
    behaviour is for it to throw std::bad_alloc unless you use
    new(nothrow).

    What is wrong with std::string?

    #include <string>

    class class1
    {
    public:
    class1(char* new_value) : string_value(new_value) {}
    // destructor not needed.

    private:
    std::string string_value;
    };

    int main()
    {
    class1 a_class1("somestring");

    return 0;
    }

    GJD
    Gavin Deane, Jul 1, 2003
    #5
    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:
    678
    Eric Sosman
    Mar 22, 2005
  2. Ken
    Replies:
    24
    Views:
    3,832
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    971
    chris
    Oct 28, 2005
  4. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,167
    Bjarke Hammersholt Roune
    Mar 6, 2011
  5. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    402
    Dmitry Korolyov
    Sep 22, 2003
Loading...

Share This Page