Why new[] adds 4 bytes to the returned address

J

JeanDean

MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?
 
V

Victor Bazarov

JeanDean said:
MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,

How do you know that?
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

Speculation: the 4 bytes contain some kind of control block
needed for the implementation to successfully deallocate the
memory (could be the size of the array you requested)...

Have you tried to see what those 4 bytes contain?

V
 
J

JeanDean

JeanDean said:
MyClass *obj = new MyClass[3];
Even though address returned by new [] is 0x00365f50,

How do you know that?
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

Speculation: the 4 bytes contain some kind of control block
needed for the implementation to successfully deallocate the
memory (could be the size of the array you requested)...

Have you tried to see what those 4 bytes contain?

V

Actually, it is storing number of elements of the array:

eg. code outputs 0000000B

///////////////////////
MyClass* obj = new MyClass[11];
char* ptr = (char*)obj;
ptr = (char*)obj - sizeof(int);
printf("%p",*ptr);
/////////////////////

But the problem is , in VC++ it reserves 4 Bytes but if I compile same
code in g++ it reserves 8 bytes for the same.
I am not able to understand what is the standard for this additional
memory reservation?
 
I

Ian Collins

JeanDean said:
But the problem is , in VC++ it reserves 4 Bytes but if I compile same
code in g++ it reserves 8 bytes for the same.
I am not able to understand what is the standard for this additional
memory reservation?
Where's the problem? It's an implantation issue, which you never have
to worry about.
 
J

JeanDean

Implementation issue!

The problem is :

I am using overloaded new for getting the memory from the big chunk of
memory.
Now , when I use overloaded new[], it is assigning addr + some bytes
to the obj pointer.

MyClass *obj = new(POOL_TYPE) MyClass[3];

where new[] is overloaded like

void* operator new[](unsigned int sz, int POOL) {
..
..
..
MyMalloc (sz, POOL);
..
}
Even though address returned by new[] was 0x0074, but 0x0078 gets
assigned to obj in case of windows and
0x007c gets assigned in case of g++.

While calling overloaded delete :

operator delete[] (ptr , POOL);

I need to pass the ptr which was returned by MyMalloc (), not the one
updted with some additional bytes.

This is because during free I need to deal with the corresponding
pointers by myself.

So, I need to know what is the standard being followed for this
reserved memory in case of new[].
 
I

Ian Collins

*Please trim signatures*
The problem is :

I am using overloaded new for getting the memory from the big chunk of
memory.
Now , when I use overloaded new[], it is assigning addr + some bytes
to the obj pointer.

MyClass *obj = new(POOL_TYPE) MyClass[3];

where new[] is overloaded like

void* operator new[](unsigned int sz, int POOL) {
..
..
..
MyMalloc (sz, POOL);
..
}
Even though address returned by new[] was 0x0074, but 0x0078 gets
assigned to obj in case of windows and
0x007c gets assigned in case of g++.
Returned by which operator new[]? If you are providing both new and
delete, how the memory is managed is down to you. You will have save
the number of items in the array somewhere in order to deallocate it.
While calling overloaded delete :

operator delete[] (ptr , POOL);

I need to pass the ptr which was returned by MyMalloc (), not the one
updted with some additional bytes.
The update will be done by the built in new/delete to facilitate its
housekeeping.
This is because during free I need to deal with the corresponding
pointers by myself.
Pointers that you returned?
So, I need to know what is the standard being followed for this
reserved memory in case of new[].
There isn't one, it's an implementation detail. Provided you don't mix
default and overloaded new/delete, you are sweet.
 
J

JeanDean

*Please trim signatures*


The problem is :
I am using overloaded new for getting the memory from the big chunk of
memory.
Now , when I use overloaded new[], it is assigning addr + some bytes
to the obj pointer.
MyClass *obj = new(POOL_TYPE) MyClass[3];
where new[] is overloaded like
void* operator new[](unsigned int sz, int POOL) {
..
..
..
MyMalloc (sz, POOL);
..
}
Even though address returned by new[] was 0x0074, but 0x0078 gets
assigned to obj in case of windows and
0x007c gets assigned in case of g++.

Returned by which operator new[]? If you are providing both new and
delete, how the memory is managed is down to you. You will have save
the number of items in the array somewhere in order to deallocate it.
While calling overloaded delete :
operator delete[] (ptr , POOL);
I need to pass the ptr which was returned by MyMalloc (), not the one
updted with some additional bytes.

The update will be done by the built in new/delete to facilitate its
housekeeping.
This is because during free I need to deal with the corresponding
pointers by myself.

Pointers that you returned?
So, I need to know what is the standard being followed for this
reserved memory in case of new[].

There isn't one, it's an implementation detail. Provided you don't mix
default and overloaded new/delete, you are sweet.

Thanks for the prompt replies.
 
E

Earl Purple

*Please trim signatures*
The problem is :
I am using overloaded new for getting the memory from the big chunk of
memory.
Now , when I use overloaded new[], it is assigning addr + some bytes
to the obj pointer.
MyClass *obj = new(POOL_TYPE) MyClass[3];
where new[] is overloaded like
void* operator new[](unsigned int sz, int POOL) {
..
..
..
MyMalloc (sz, POOL);
..
}
Even though address returned by new[] was 0x0074, but 0x0078 gets
assigned to obj in case of windows and
0x007c gets assigned in case of g++.
Returned by which operator new[]? If you are providing both new and
delete, how the memory is managed is down to you. You will have save
the number of items in the array somewhere in order to deallocate it.
While calling overloaded delete :
operator delete[] (ptr , POOL);
I need to pass the ptr which was returned by MyMalloc (), not the one
updted with some additional bytes.
The update will be done by the built in new/delete to facilitate its
housekeeping.
Pointers that you returned?
So, I need to know what is the standard being followed for this
reserved memory in case of new[].
There isn't one, it's an implementation detail. Provided you don't mix
default and overloaded new/delete, you are sweet.

Thanks for the prompt replies.- Hide quoted text -

- Show quoted text -

You might consider using std::vector anyway. If you really don't want
your memory initialised then you might use boost::scoped_array or
shared_array.
 
R

red floyd

JeanDean said:
MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

Please don't multi-post. You've also posted this to gnu.g++.help and to
comp.lang.c++.moderated
 
C

Clark Cox

JeanDean said:
MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

Please don't multi-post. You've also posted this to gnu.g++.help and to
comp.lang.c++.moderated

Just as an FYI to the OP "multi-posting" (aka "double-posting") is
different than "cross-posting". The former is never appropriate, and
the latter is just slightly more approprate.

http://www.blakjak.demon.co.uk/mul_crss.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top