The Daily Click ::. Forums ::. Non-Klik Coding Help ::. 2D array using malloc?
 

Post Reply  Post Oekaki 
 

Posted By Message

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
5th June, 2004 at 09:08:50 -

I've got this code:

int ** array = (int**)malloc(100*100);

but I have no idea how to use it as a 2d array, eg array[5][5]. Could anyone shed some light on this?
Thanks

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Long John Kickbag



Registered
  26/08/2002
Points
  148
5th June, 2004 at 09:20:18 -

Use this expression: y*xwidth+x.

 
Resize! - www.clicksplat.com/comparison.html

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
5th June, 2004 at 11:21:18 -

Ah, logic ;P

If you REALLY want the [5][5] coding, use this instead:

int** array = (int**)malloc(100);

for (int i=0;i<100;i++)
array[i] = (int*)malloc(100);


 
n/a

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
5th June, 2004 at 11:37:24 -

good ideas. thanks both of you

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
5th June, 2004 at 12:01:50 -

IMO it's easier to do: (dont forget that sizeof)
int* array = (int*)malloc(width * height * sizeof(int));
and then access co-ordinates 7,3 by using the formula Erik gave:
int number = array[7 * width + 3];
You can make it easier by making functions or macros to do that sum for you.
Using pointers-to-pointers and loops to allocate lists of memory seems a bit of overcomplication. Freeing the array is complicated. You only need one Free with the formula. Also the list of pointers will use more memory.

Note: the formula for 3D arrays is (z * height * width) + (y * width) + x
Note 2: use C++ and do int* array = new int[width * height];

 
- Tigerworks

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
5th June, 2004 at 12:13:18 -

i guess there's no way of doing array[x][y] then? It's just for the sake of neatness really

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
5th June, 2004 at 12:42:36 -

Yes, use my method :|

 
n/a

Long John Kickbag



Registered
  26/08/2002
Points
  148
5th June, 2004 at 12:42:51 -

You can reference it like that in FM's method but like Tigs says it makes it more complicated. Alternatively you could make a class and you may be able to use operator overloading to get [] to work how you want.

 
Resize! - www.clicksplat.com/comparison.html

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
5th June, 2004 at 19:33:41 -

Operator Overloading is good but in this case it's a bit more trouble than it's worth if the designer is after "neatness" I assume your using C, btw? It's been a while since I last used it, but I dont think Operator Overloading is available in that language? Only C++? Perhaps I'm wrong, as I said it has been a while.

CBFM's method works the way you want, though you wouldnt want to be doing that many times, since you're increasing the array greatly with each loop it constantly has to find new space if the space it previously allocated becomes too small.

Microsoft Visual C++ and other good compilers MIGHT be able to minimize the code when it's compiled so that it isnt executing a loop but finds all the space straight away. But that depends on the compiler -- I certinaly dont know for sure which (if any) would be able to do this.

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

Long John Kickbag



Registered
  26/08/2002
Points
  148
5th June, 2004 at 19:49:07 -

You're right about C not having operator overloading, since it's a class specific thing and C has no OOP.

 
Resize! - www.clicksplat.com/comparison.html

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
5th June, 2004 at 20:43:21 -

Aren't most C compilers also C++ compilers? Why does anyone still use C these days if C++ is around? Isn't C++ entirely superior? Classes are too cool not to use. Only disadvantage I can see to C++ is a few CPU cycles extra here and there, but with a modern machine doing 3 billion cycles per second, that's not too much of a worry.

 
- Tigerworks

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
6th June, 2004 at 02:19:22 -

TW is accurate.

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
6th June, 2004 at 07:45:04 -

My idea's good if you're going to change the number of rows, as all you are doing is creating a new array of pointers, copying the old array and deleting it. With one big chunk, the overhead is far greater.

 
n/a

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
6th June, 2004 at 07:59:32 -

Sorry, didnt read your post properly chris daft mistake

I'm using bits of C and C++, whichever's easier/most powerful in whatever situation

Image Edited by the Author.

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Tigerworks

Klik Legend

Registered
  15/01/2002
Points
  3882
6th June, 2004 at 09:05:59 -

Well I'd say C++ is easier and more powerful in all cases. Classes, inheritence, encapsulation, polymorphism, constructors, destructors, void pointers, overloading, operator overloading, exceptions, new, delete and STL all feature in C++ to name a few.
While FM's method may make it easier to add new rows, it also uses more memory. Personally, I'd use one of the STL classes like a vector or something, and have all the memory handled for you

 
- Tigerworks

ChrisB

Crazy?

Registered
  16/08/2002
Points
  5457
6th June, 2004 at 15:40:48 -

n.b. vector is memory inefficient

 
n/a

ShadowCaster

Possibly Insane

Registered
  02/01/2002
Points
  2203
9th June, 2004 at 20:27:33 -

NOOOOOOOOOOOOOO! VEEEEECTOOOOORSSSSS!

Mike

 
"Now I guess we're... 'Path-E-Tech Management'" -Dilbert
   

Post Reply



 



Advertisement

Worth A Click