473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Why new[] adds 4 bytes to the returned address

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?

Feb 28 '07 #1
10 1367
JeanDean wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 28 '07 #2
On Feb 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JeanDean wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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?

Feb 28 '07 #3
JeanDean wrote:
>
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.

--
Ian Collins.
Feb 28 '07 #4
Ian Collins wrote:
JeanDean wrote:
>>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.
Implementation issue!

--
Ian Collins.
Feb 28 '07 #5
On Feb 28, 3:36 pm, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
JeanDean wrote:
>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.

Implementation issue!

--
Ian Collins.
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[].

Feb 28 '07 #6
JeanDean wrote:
On Feb 28, 3:36 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>Ian Collins wrote:
>>>JeanDean wrote:
>>>>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.

Implementation issue!
*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.

--
Ian Collins.
Feb 28 '07 #7
On Feb 28, 4:00 pm, Ian Collins <ian-n...@hotmail.comwrote:
JeanDean wrote:
On Feb 28, 3:36 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Ian Collins wrote:
>>JeanDean wrote:
>>>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.
>Implementation issue!

*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.

--
Ian Collins.
Thanks for the prompt replies.

Feb 28 '07 #8
On 28 Feb, 07:10, "JeanDean" <rohits...@gmail.comwrote:
On Feb 28, 4:00 pm, Ian Collins <ian-n...@hotmail.comwrote:


JeanDean wrote:
On Feb 28, 3:36 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>Ian Collins wrote:
>>>JeanDean wrote:
>>>>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.
>>Implementation issue!
*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.
--
Ian Collins.

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.
Feb 28 '07 #9
JeanDean wrote:
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

Feb 28 '07 #10
On 2007-02-28 08:06:27 -0800, red floyd <no*****@here.dudesaid:
JeanDean wrote:
>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

Feb 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
13
by: ppateel | last post by:
Hi, I am new to c++ and I am converting a c program to c++. I changed malloc call to new and I am getting an exception violation. Here is the relevant piece of code. Compiler vc++ 7.0 (.Net...
8
by: Tony A. | last post by:
I'm using VB .Net 2005 with Access 2003 for the database. The database has two tables: tblContact (ContactNum (k), lastname, firstname, phone, email, compNum) and tblCompany (CompNun (k),...
2
by: Shafik | last post by:
Hello folks, I am having a really weird problem. Under windows XP, a simple C program designed to write to a file has been adding EXTRA characters to the output file. First I used "fwrite",...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
1
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.