Solved

New & Delete question

Posted on 2001-06-25
7
218 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
max float value 3 51
Precision Problem in C++ 7 44
Android development question 2 54
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

830 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