Solved

new operator .

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now