Solved

new operator .

Posted on 2000-04-28
3
206 Views
Last Modified: 2010-04-02
I use VC++ 5.0
I define a class like that
class FACT
{ char * content}
after that I init an array
 arr=new FACT[3];
but it allocate 4 elements each 32 bits.
the first elements contain number of elements in the array(3),and then "Debug assertion failed" because of arr point to the second elements so it can't free the mem .
How can I allocate only 3 elements by new operator.
Thanks.
0
Comment
Question by:tungsim
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2760554
>> but it allocate 4 elements each 32 bits.
In VC (and most C++ implimentations as well) when you allocate an array with new [] operator it alocates a little extra space for recording the number of items in the array.  this extra space if for the "private" use of the  c++ implimentation.  In particular, it is is for the use of the delete [] operator.  You should not be concerd about this extra memory.  it is necessary, not a problem, and not something for you to fool with.  Note that the memory is an extra integer (or some size integral) amount. it is not an entire extra element in the array beign allocated.  Like if you allocate an array of 10 big objects, it won't allocate space for 11 objects, just space for 10 plus a little extra  (4 bytes in VC).

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2760567
>> "Debug assertion failed"
Now where do you get this error?  is it when you delete?   do you delete using "delete" or using delete []"?  If you allocate with new []" you must delete with "delete []"  Like

arr=new FACT[3];
delete [] arr;

not

delete arr; // this is an error.

Let me know if you have any questions.
0
 

Author Comment

by:tungsim
ID: 2761469
Thanks nietod .
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
returning a dereferenced pts in C++ 10 158
Least Squares Curve Fitting 4 99
Android development question 2 55
I could not set window to top 4 22
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

820 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