Allocationg memory: contigously ?

H

horacius.rex

Hi,

if I allocate memory in a simple program like

int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];

then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

Thanks
 
R

robin

Hi,

if I allocate memory in a simple program like

int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];

then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

Thanks

1). They might be allocated contigously but this is not always
guaranteed.
2). Honestly I'm not quite clear about this point, I think you might
need to define your own memory allocation strategy, using some
"placement new operator" or something...

Regards,
-robin
 
F

farbe90

Hi,

if I allocate memory in a simple program like

int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];

then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

Thanks


hey
im sure sbout that memory may not be contiguous....since it is done
dynamically...rather never..coz u can take that probability as
1:trillion.
and about the thing im not 140% sure is that nthng can be done about
it...only thing u can do is that u can join them by linking stacks..
well thats all i know frm my school... ;)...im still studying..pl tell
me if there;s a mistake
 
G

GameboyHippo

if I allocate memory in a simple program like
int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];
then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

hey
im sure sbout that memory may not be contiguous....since it is done
dynamically...rather never..coz u can take that probability as
1:trillion.
and about the thing im not 140% sure is that nthng can be done about
it...only thing u can do is that u can join them by linking stacks..
well thats all i know frm my school... ;)...im still studying..pl tell
me if there;s a mistake

y do people talk lik thiz. I mean seriously people. Is it that hard
to communicate using complete words? To me, it topples your
credibility.

Okay, now for something that really matters. Will the data be
contigously? Maybe. Maybe not. How to make them contigous: Here's
what I would do.

int* pint=new[3000];
int* pint1 = pint;
int* pint2 = pint + 1000;
int* pint3 = pint + 2000;
 
T

tragomaskhalos

Hi,

if I allocate memory in a simple program like

int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];

then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

No - there is almost certainly going to be some extra
stuff before each block which prevents them being
contiguous. To ensure contiguity, you could do this:

int* p1 = new int[3000];
int* p2 = p1 + 1000;
int* p3 = p2 + 1000;

Obviously in this case you must be sure to only
delete[] the p1 pointer.

But why do you care about contiguity ?
 
D

Daniel T.

GameboyHippo said:
Okay, now for something that really matters. Will the data be
contigously? Maybe. Maybe not. How to make them contigous: Here's
what I would do.

int* pint=new[3000];
int* pint1 = pint;
int* pint2 = pint + 1000;
int* pint3 = pint + 2000;

That would work, I'd make pint a vector<int> though instead.
 
M

Marcin Kalicinski

if I allocate memory in a simple program like
int* pint1=new[1000];
int* pint2=new[1000];
int* pint3=new[1000];

then memory is allocated on the heap. My question is if this three
"memory fragmetns" are allocated contigously. And if not, what could I
do to allocate them contigously.

int *bigfatpint = new int[3000];
int *pint1 = bigfatpint;
int *pint2 = bigfatpint + 1000;
int *pint3 = bigfatpint + 2000;

cheers,
Marcin
 

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
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top