Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

New & Delete question

Posted on 2001-06-25
7
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 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