Solved

New & Delete question

Posted on 2001-06-25
7
189 Views
Last Modified: 2010-04-02
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);
}
0
Comment
Question by:carlpaddick
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6224084
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.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6224088
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6224091
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.  
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:carlpaddick
ID: 6224114
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.

Carl

(Thanks too jasonclarke for your response)
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6224154
> 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).
0
 
LVL 22

Expert Comment

by:nietod
ID: 6224186
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.
0
 

Author Comment

by:carlpaddick
ID: 6224382
Thanks jasonclarke for pointing that out and nietod for your comments too.  You both have been very helpfull with this question.  Thanks once again.

Carl
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now