By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

pointing to first element of an array**

P: n/a
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
Feb 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"John den Haan" <no****@nospam.comwrote in message
news:43**************************@news.chello.nl.. .
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
Feb 20 '07 #2

P: n/a
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[i] = malloc(cols * sizeof *(BUFFER[i]));
if (BUFFER[i] == 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
Feb 20 '07 #3

P: n/a
John den Haan schreef:
for (i=0;i<rows;i++) {
BUFFER[i] = malloc(cols * sizeof *(BUFFER[i]));
if (BUFFER[i] == 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[i]=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
Feb 20 '07 #4

P: n/a

"John den Haan" <no****@nospam.comwrote in message
news:97***************************@news.chello.nl. ..
John den Haan schreef:
for (i=0;i<rows;i++) {
BUFFER[i] = malloc(cols * sizeof *(BUFFER[i]));
if (BUFFER[i] == 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[i]=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

Feb 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.