pointing to first element of an array**

J

John den Haan

Hello!

I was wondering how I can pass a pointer to a 2D-array (declared with a
malloc-construct) as if it were a continuous 1D-array of the size
(rows*cols)? Is this at all possible, and if so, how?

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
M

Mike Wahler

John den Haan said:
Hello!

I was wondering how I can pass a pointer to a 2D-array (declared with a
malloc-construct) as if it were a continuous 1D-array of the size
(rows*cols)? Is this at all possible, and if so, how?

That depends upon exactly what your 'malloc-construct'
is. Each (successful) call to 'malloc()' allocates
a contiguous area of memory (i.e. it *is* a 1D array).
If you've allocated an array of pointers and assigned
each pointer the address of a subsequently allocated array,
then the total memory allocated will almost certainly not
be contiguous, so a single pointer could not be used
to step through it. Show your code, and perhaps explain
more specifically what you want to do, and we can offer
advice.

-Mike
 
J

John den Haan

Mike Wahler schreef:
Show your code, and perhaps explain more specifically what you want to do,
> and we can offer advice.

-Mike

OK, this is the construct:

BUFFER = malloc(rows * sizeof *BUFFER);
if (BUFFER == NULL){
return false;
}

for (i=0;i<rows;i++) {
BUFFER = malloc(cols * sizeof *(BUFFER));
if (BUFFER == NULL) {
return false;
}
}

Where ofcourse BUFFER is a double pointer (of the type CHAR_INFO** in
case you're interested). This patch of memory is probably fragmented,
and I'd like to know how to make it contiguous (just as if I were to
statically allocate memory for BUFFER). This way I can feed a pointer to
the first element to a platform-specific function which reads the memory
(and ironically, will 'treat it as if it were a 2D-array of dimension
{param1, param2}' -cheers to M$ on that one-).

--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
J

John den Haan

John den Haan schreef:
for (i=0;i<rows;i++) {
BUFFER = malloc(cols * sizeof *(BUFFER));
if (BUFFER == NULL) {
return false;
}
}

Where ofcourse BUFFER is a double pointer (of the type CHAR_INFO** in
case you're interested). This patch of memory is probably fragmented,
and I'd like to know how to make it contiguous (just as if I were to
statically allocate memory for BUFFER). This way I can feed a pointer to
the first element to a platform-specific function which reads the memory
(and ironically, will 'treat it as if it were a 2D-array of dimension
{param1, param2}' -cheers to M$ on that one-).


OK, I've found and adapted this snippet of code that works but I don't
understand exactly HOW it works. Could anyone explain to me what exactly
is done here in words?

BUFFER = malloc(rows * sizeof(*BUFFER) );
BUFFER[0] = malloc(rows * cols * sizeof *(BUFFER[0]) );
for(i=1; i<rows; i++) {
BUFFER=BUFFER[i-1]+cols;
}
--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 
B

Barry

John den Haan said:
John den Haan schreef:
for (i=0;i<rows;i++) {
BUFFER = malloc(cols * sizeof *(BUFFER));
if (BUFFER == NULL) {
return false;
}
}

Where ofcourse BUFFER is a double pointer (of the type CHAR_INFO** in
case you're interested). This patch of memory is probably fragmented,
and I'd like to know how to make it contiguous (just as if I were to
statically allocate memory for BUFFER). This way I can feed a pointer to
the first element to a platform-specific function which reads the memory
(and ironically, will 'treat it as if it were a 2D-array of dimension
{param1, param2}' -cheers to M$ on that one-).


OK, I've found and adapted this snippet of code that works but I don't
understand exactly HOW it works. Could anyone explain to me what exactly
is done here in words?

BUFFER = malloc(rows * sizeof(*BUFFER) );


Allocate pointers for all the rows.
BUFFER[0] = malloc(rows * cols * sizeof *(BUFFER[0]) );

Allocate enough space for the data, taking advantage of the fact
that the first row will point the the beginning of the data.
for(i=1; i<rows; i++) {
BUFFER=BUFFER[i-1]+cols;


Set each row pointer just past the end of the previous row.
}
--

Cheers,

John den Haan
joDhn[dot]haEan[at]chLello[dot]nl

Remove capital 'DEL' from above addy to obtain e-mail address
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top