?
Solved

new operator .

Posted on 2000-04-28
3
Medium Priority
?
213 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
[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
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

718 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