Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

new operator .

Posted on 2000-04-28
3
Medium Priority
?
216 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 200 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

Hire Technology Freelancers with Gigs

Work with 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

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 …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

772 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