New & Delete question

Can anyone tell me why I get an assert error (Debug Error! Damage after Normal Block .......) when I perform following the steps in my function?  I've abbreviated it a bit for clarity:

int myfunc(int nArgno)
     char* pStatus = NULL;

                  // m_szArgv is an array of char strings

     pStatus = new char (sizeof(m_szArgv[nArgno]) + 1);

     if (pStatus == NULL)
          return (0);

     strcpy(pStatus, m_szArgv[nArgno]);

                   // pStatus is compared but never modified in this function
                   // .....................

     if (pStatus)
          delete (pStatus);

                  return (1);
Who is Participating?
nietodConnect With a Mentor Commented:
First of all

   delete (pStatus);

must be

   delete [] pStatus;

(you can have the parenthesis if you want them.  You MUST have the [].


the problem is that you cannot use sizeof() in that way to measure the length of a string.    The variable m_szArgv is a pointer, not a character array.  This pointer points to a character array, but sizeof() doesn't "know" about that.  sizeof() does its job which is to return the size of the specified variable.  That is, it returns the size of the pointer--which for you is 4 bytes.  No matter how long the string it points to is, the sizeof() function will return 4.  

You need to use strlen() instead.
because this is almost certainly not what you want:

> pStatus = new char (sizeof(m_szArgv[nArgno]) + 1);

you should do something like this:

pStatus = new char[strlen(m_szArgv[nArgno])+1];

and then delete with:

delete [] pStatus.

The code you had just allocates one character, assigned with the value of the expression that is in the brackets.
For example

  pStatus = new char (strlen(m_szArgv[nArgno]) + 1)

note that strlen() takes a pointer parameter that it assumes points to a NUL terminated character array.  Which is exactly what you have.   Note that strlen() does its work at run-time.  i.e. if you change the length of the string a 2nd call to strlen() will return a different length.   This is very unlike sizeof().  sizeof() works at compile time and returns the storage length of the specified variable.  This length never can change so the work can be done at compile time.  
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

carlpaddickAuthor Commented:
Thanks nietod.  Excellent answer and well put in an easy to understand manner.

Of course, looking at it now, I see where I have misinterpreted the sizeof() operator.

Thanks for your help, and please have the points.  Keep up the good work.


(Thanks too jasonclarke for your response)
> pStatus = new char (strlen(m_szArgv[nArgno]) + 1)

note that the critical bit still missing is that you need to replace the outer '(' & ')' on the new statement with '[' and ']'.  If you fail to do so only 1 byte of memory will be allocated (set to the value of the expression).
Right, I missed that.    

what it does is to allocate 1 character and initialize it to a number.  i.e.

char *p1 = new char (10);

creates a single character and sets it to store 10, then returns a pointer to that character which is stored in p1.  but

char *p2 = new char [10];

creates an array of 10 characters, and stores the pointer to that array in p2.
carlpaddickAuthor Commented:
Thanks jasonclarke for pointing that out and nietod for your comments too.  You both have been very helpfull with this question.  Thanks once again.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.