Solved

new operator .

Posted on 2000-04-28
3
205 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

775 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