By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,910 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,910 IT Pros & Developers. It's quick & easy.

overloading new[] operator

P: n/a
I seem to be having problems with overloading the new[] operator (I am
using visual studio .net 2002 if that has any affect on my question).

Lets say I do this:

MyClass *x = new (1) MyClass[100];

Okay so my new[] function says that it needs sizeof(MyClass) * 100
bytes allocated so I do that and everything seems fine. But when I get
to myfree the program crashes and what I find is that the address
passed to myfree is not the same as the start of the space that I
allocated. It is actually the address I allocated plus four.

The allocation works fine if I am allocated basic types such as int,
double, float, etc. It also works fine if I am not allocating arrays.
It appears that I should actually be allocating four more bytes than it
is telling me to and when I free I need to back up the address by four
positions. If I do these things then the program runs fine, memory is
all deallocated properly, etc.

Any ideas?

Nathan Bullock

Aug 3 '05 #1
Share this Question
Share on Google+
1 Reply

This discussion thread is closed

Replies have been disabled for this discussion.