Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 'new'd memeroy is contiguous?

Reply
Thread Tools

'new'd memeroy is contiguous?

 
 
divya_rathore_@gmail.com
Guest
Posts: n/a
 
      12-19-2005
No pun intended in the subject

Is dynamically allocated memory contiguous in C++? In C? Deails would
be appreciated.


warm regards,
Divya Rathore
(remove underscores for email ID)

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-19-2005
divya_rathore_@gmail.com wrote:
> No pun intended in the subject


memeroy?

> Is dynamically allocated memory contiguous in C++? In C? Deails would
> be appreciated.


Yes, it is. What details do you expect?
 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-19-2005
On 19 Dec 2005 09:04:23 -0800, "divya_rathore_@gmail.com"
<> wrote:

>Is dynamically allocated memory contiguous in C++? In C? Details would
>be appreciated.


C questions are off-topic here unless they are somehow also relevant
to C++ (comp.lang.c is the place to ask), so I'll limit myself to the
question concerning "new".

Do you mean, contiguous across different calls to new? If so, the
answer is: not necessarily, because it is implementation-defined how
new obtains and utilizes its raw memory. I would say, no, because
unused blocks of memory released by calling delete are normally
recycled within the total memory used by the process taken from its
free store (i.e. heap memory) provided by the operating system. Often,
the memory is only returned to the operating system when the process
ends, but that is also an implementation-specific detail which can be
quite different on another compiler and operating system.

A class can also overload new and delete to use a different memory
allocation scheme.

Otherwise, I don't really understand your question. Allocations
performed by new allocate a single object of a given type. Allocations
performed by new[] allocate arrays of objects of the same type.
Although the memory occupied by the array is guaranteed to be
contiguous, and the memory occupied by any given object is contiguous,
it is implementation-defined how the compiler lays out the individual
members of the object in that memory. The implementation (i.e. your
compiler) is free to add padding bytes between the individual members
for purposes of alignment and can be influenced by different
optimization and alignment settings at compile-time (e.g., see the
documentation for "#pragma pack" if your compiler supports it).

Consider the following:

struct X {
short a;
char b;
int c;
};

What is sizeof X here? Depending on the compiler settings, you will
most likely get an additional byte of padding thrown in after the char
member. However, it is also possible that there are two extra bytes
after the short member as well. Switch around the order of the members
and see if you get different results.

Of course, an instance of a class can allocate additional memory and
store just the pointer to that memory. When querying the size of such
an object with sizeof(), only the memory occupied by the pointer would
count towards the total size of the object.

If you want more details, I suggest you get a copy of the C++ standard
at: http://www.ansi.org

--
Bob Hairgrove

 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-19-2005
On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
<> wrote:

>divya_rathore_@gmail.com wrote:
>> No pun intended in the subject

>
>memeroy?


Just a typo or misspelling.
I think the pun was with "new'd" and "nude" (groan...)

--
Bob Hairgrove

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-19-2005
Bob Hairgrove wrote:
> On Mon, 19 Dec 2005 12:26:41 -0500, Victor Bazarov
> <> wrote:
>
>
>>divya_rathore_@gmail.com wrote:
>>
>>>No pun intended in the subject

>>
>>memeroy?

>
>
> Just a typo or misspelling.
> I think the pun was with "new'd" and "nude" (groan...)


Ah... OK. I was thinking of "hemorrhoids".
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-19-2005
divya_rathore_@gmail.com wrote:

> No pun intended in the subject
>
> Is dynamically allocated memory contiguous in C++?


Yes.

> In C?


Yes.

> Deails would be appreciated.


What details do you want? It's contiguous, that's how you have dynamic
arrays.


Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
divya_rathore_@gmail.com
Guest
Posts: n/a
 
      12-19-2005
Thank you all..
so, in:


int* x;
x = new int[MAX];

memory allocated is contiguous for how-so-ever-large MAX (till system
supports)?

 
Reply With Quote
 
divya_rathore_@gmail.com
Guest
Posts: n/a
 
      12-19-2005
>> Is dynamically allocated memory contiguous in C++? In C? Deails would
>> be appreciated.



>Yes, it is. What details do you expect?


As someone pointed, asking about malloc/calloc is out of scope of this
newsgroup, so I will restrain myself.
Thanks anyways.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-19-2005
divya_rathore_@gmail.com wrote:
>>>Is dynamically allocated memory contiguous in C++? In C? Deails would
>>>be appreciated.

>
>
>
>>Yes, it is. What details do you expect?

>
>
> As someone pointed, asking about malloc/calloc is out of scope of this
> newsgroup, so I will restrain myself.


Actually, since C Standard Library circa 1990 is part of C++ Standard
Library, it's not out of scope to talk about malloc or calloc. Please
ask your library questions as you wish, it's absolutely _in_ the scope
of this newsgroup. C _language_ questions are better asked in
comp.lang.c, however.

V
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-19-2005
On 19 Dec 2005 12:18:29 -0800, "divya_rathore_@gmail.com"
<> wrote:

>Thank you all..
>so, in:
>
>
>int* x;
>x = new int[MAX];
>
>memory allocated is contiguous for how-so-ever-large MAX (till system
>supports)?


The limit on the array size (MAX) is not specified by the C++
standard. If the call to new succeeds, x will be a valid pointer to
that memory. Otherwise, an exception (std::bad_alloc) is thrown.

If you need to allocate very large sections of RAM, e.g. for image
processing, you can also do it with operating-system specific means
and use placement new.

--
Bob Hairgrove

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57