473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

pointing to first element of an array**

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
4 1629

"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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
8
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
6
by: xian_hong2046 | last post by:
Hello, Could someone please tell me what's the correct way of representing a pointer pointing to an array? If I have an array of int, then I have: int a; I understand I can create a...
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
7
by: Szabolcs Borsanyi | last post by:
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a) { int...
4
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
6
by: CSharper | last post by:
I am trying to use the following; I have an array with bunch of values in it. I am trying to find a value that contains part of the string I am passing eg string array = {"help","Csharp rocks"} ...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.