'new'd memeroy is contiguous?

D

divya_rathore_

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)
 
V

Victor Bazarov

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?
 
B

Bob Hairgrove

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
 
D

Default User

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
 
D

divya_rathore_

Thank you all..
so, in:


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

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

divya_rathore_

Is dynamically allocated memory contiguous in C++? In C? Deails would
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.
 
V

Victor Bazarov

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
 
B

Bob Hairgrove

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

Victor Bazarov

Thank you all..
so, in:


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

Why not write them in the same statement?
memory allocated is contiguous for how-so-ever-large MAX (till system
supports)?

"how-so-ever-large"? Google only has three links with that text in them
(and without hyphens, mind you). I have to admit, I'd never seen it
before reading your post. Some new English every day -- that's my goal.

The default allocation mechanism (if that's the one you're asking about)
will attempt to locate a contiguous segment of memory large enough to
accommodate the array. If it fails, an exception will be thrown. You can
overload 'new[]' and do whatever you would like...

V
 
J

jsanga

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)

Dynamically allocated memory is contiguous. Don't confuse that with a
dynamically allocated object is contiguous.

class MyObj {
public:
MyObj()
: blockA( new char [12] )
, blockB( new int [12] )
{}
~MyObj()
{ delete [] blockA;
delete [] blockB;
}
private:
char *blockA;
int*blockB;
};

MyObj *p = new MyObj();

MyObj is contiguous. blockA is contiguous. blockB is contiguous. But
MyObj, blockA and blockB as a group might not be contiguous.
 
V

Victor Bazarov

Dynamically allocated memory is contiguous. Don't confuse that with a
dynamically allocated object is contiguous.

Every object occupies a contiguous block of memory, so I am not sure
where your second sentence is coming from.
class MyObj {
public:
MyObj()
: blockA( new char [12] )
, blockB( new int [12] )
{}
~MyObj()
{ delete [] blockA;
delete [] blockB;
}
private:
char *blockA;
int*blockB;
};

MyObj *p = new MyObj();

MyObj is contiguous. blockA is contiguous. blockB is contiguous. But
MyObj, blockA and blockB as a group might not be contiguous.

Why are they suddenly "a group"?

V
 
K

Kaz Kylheku

No pun intended in the subject :)

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

Suppose it wasn't contiguous. You get one pointer out of the allocator,
which points to uninitialized storage. How would your program find the
other pieces? With what information?
 
V

Victor Bazarov

Kaz said:
Suppose it wasn't contiguous. You get one pointer out of the allocator,
which points to uninitialized storage. How would your program find the
other pieces? With what information?

Somehow (in an implementation-defined manner) the pointer to a dynamic
array would be different from a pointer to any other array and any pointer
arithmetic on it would be performed using some magic (implementation-
specific magic, mind you). So, indexing, increments, pointer difference,
would be done for a pointer from 'new[]' somehow differently than for
a pointer to an element of an automatic or static array. Possible?

V
 
N

n2xssvv g02gfr12930

Victor said:
Ah... OK. I was thinking of "hemorrhoids".

Well, what can I say in my C++'d state, I'd never have C'n it coming.
Oh, the joys of a symbiotic relationship with yeast, alcohol being
derived from yeast etc, etc.

JB
 
K

Kaz Kylheku

Victor said:
Kaz said:
Suppose it wasn't contiguous. You get one pointer out of the allocator,
which points to uninitialized storage. How would your program find the
other pieces? With what information?

Somehow (in an implementation-defined manner) the pointer to a dynamic
array would be different from a pointer to any other array and any pointer
arithmetic on it would be performed using some magic (implementation-
specific magic, mind you). So, indexing, increments, pointer difference,
would be done for a pointer from 'new[]' somehow differently than for
a pointer to an element of an automatic or static array. Possible?

The semantics of these pointer operations is what defines contiguity.
:)
 
D

divya_rathore_

Victor said:
"how-so-ever-large"? Google only has three links with that text in them
(and without hyphens, mind you). I have to admit, I'd never seen it
before reading your post. Some new English every day -- that's my goal.

hyphens were to put stress. I, well, could have done with italisizing
the phrase had it been allowed (I am using web interface of google
groups). Yes.. English, being a flexible language, allows one to be
experimental.

The default allocation mechanism (if that's the one you're asking about)
will attempt to locate a contiguous segment of memory large enough to
accommodate the array. If it fails, an exception will be thrown. You can
overload 'new[]' and do whatever you would like...

V


Thanks! You and others have been very informative.
 
D

divya_rathore_

Kaz said:
Suppose it wasn't contiguous. You get one pointer out of the allocator,
which points to uninitialized storage. How would your program find the
other pieces? With what information?


Yes, that's what I was worried about.
I, indeed, am working in a image processing application and was trying
to figure out if a 2D array is better for dynamic allocation or a big
1-dimensional one.

Case 1: 1D case will allocate it in one go. So contguity is there
indeed.


Case 2: 2D will involve memory to be new'd (again, grammatical usage is
casual here. This is *NOT* correct english.) once for the columns and
then itertively with in a 'for' loop so as to cover the rows.
int** array2D = new int*[height];
for (int i=0; i<height; i++)
{
array2D = new int[width];
}

My querry is that whether this array2D is contiguous or not.

So far so good.. now which of these 2 cases will be faster to access?
Obviously the one which is contiguous. Hence this question on the
newsgroup.
 
J

joepizzi

Case 1: 1D case will allocate it in one go. So contguity is there
indeed.

Yes, and you could use this allocated memory as a 2D array, if you
wanted to.

int** array2D = new int*[height];
int* temp = new int[height*width];
for (int i=0; i<height; ++i)
{
array2D = &(temp[i*width]);
}
Case 2: 2D will involve memory to be new'd (again, grammatical usage is
casual here. This is *NOT* correct english.) once for the columns and
then itertively with in a 'for' loop so as to cover the rows.
int** array2D = new int*[height];
for (int i=0; i<height; i++)
{
array2D = new int[width];
}

My querry is that whether this array2D is contiguous or not.


Not necessarily.
So far so good.. now which of these 2 cases will be faster to access?
Obviously the one which is contiguous. Hence this question on the
newsgroup.

The obvious is not necessarily the case. It could be that because of
cache hits/misses, the non-contiguous one is faster. You should just
try it and see.

Another thing that no one mentioned. Do you mean physically contiguous,
or virtually contiguous. It is a fact that the memory allocated will be
virtually contiguous. It is not a fact that the memory will be
physically contiguous.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top