Join us at IRC!
Hacking isn't just Computers & Exploits. It's a Philosophy. - Mr_Cheese
Wednesday, May 23, 2012
Navigation
Members Online
Total Online: 34
Web Spiders: 17
Guests Online: 32
Members Online: 2

Registered Members: 70179
Newest Member: prankst3r
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Dynamic Allocation of Multi-Dimensional Arrays - C/C++

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 27-02-10 00:30
Hey there everyone.

I've been studying a bit of C and C++ here, especially the part of object oriented programming, and i just banged my head in a big wall. I never really needed to dynamically allocate memory for multi dimensional arrays. It would be easy if i was dealing with simple vectors with my beautiful function called malloc and pointers. See the example below:

//For a linear 1000 values' vector
int *a;
int i=1000;

a=(int *)malloc(i*sizeof(int))


In c++ i'd use the new keyword.

I'd be really grateful if anyone could explain me how to dynamically allocate values in a 2 dimensional array both in C and C++. Yet C is more important right now. I've been looking it up in google but i couldn't understand it really well. They mention creating arrays in structs and calling it with pointers, but i'd like to see some sort of code to understand it.
Author

RE: Dynamic Allocation of Multi-Dimensional Arrays - C/C++

fashizzlepop
Member



Posts: 482
Location: Old folks home.
Joined: 08.04.08
Rank:
Uber Elite
Posted on 27-02-10 00:49
C is not really object oriented. I assume you could use some creative programming to make it seem so but that might be more complicated than what your looking for. Hope this helps.


"The definition of insanity is doing the same thing over and over again and expecting different results.”
~Albert Einstein~


fashizzlepop@gmail.com http://csullivan.codeinspire.net/
Author

RE: Dynamic Allocation of Multi-Dimensional Arrays - C/C++

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 27-02-10 01:11
fashizzlepop wrote:
C is not really object oriented. I assume you could use some creative programming to make it seem so but that might be more complicated than what your looking for. Hope this helps.


Well, yeah, i expressed myself badly. The thing is, i'm studying more deeply c++ now because i don't like c, but it is important for me to learn that in c as well as i'll be seeing this thing in university soon. We unfortunately study c and not c++ :(
Author

RE: Dynamic Allocation of Multi-Dimensional Arrays - C/C++

ynori7
Future Emperor of Earth



Posts: 1481
Location: #valhalla
Joined: 08.10.07
Rank:
Diabolical
Posted on 27-02-10 04:37
I believe this is how you create a multidimensional array in C/C++, though it's been a while so I could be wrong.
int **a;
int i=10;
int j=1000;

//make an array with 10 sets of 1000 ints
a=(int *)malloc(i*sizeof(int*));
for(int x=0; x<i; x++)
{
a[x] = (int *)malloc(j*sizeof(int));
}

Not entirely sure if that was what you were looking for.




ynori7 http://halls-of-valhalla.org
Author

RE: Dynamic Allocation of Multi-Dimensional Arrays - C/C++

chess_rock
Member



Posts: 243
Location:
Joined: 20.02.08
Rank:
God
Posted on 28-02-10 02:44
ynori7 wrote:
I believe this is how you create a multidimensional array in C/C++, though it's been a while so I could be wrong.
int **a;
int i=10;
int j=1000;

//make an array with 10 sets of 1000 ints
a=(int *)malloc(i*sizeof(int*));
for(int x=0; x<i; x++)
{
a[x] = (int *)malloc(j*sizeof(int));
}

Not entirely sure if that was what you were looking for.


Thanks a lot ynori :) That's exactly what i was looking for. I didn't realize i needed to add a double pointer (int **a). That makes it much clearer :) I'll program something soon to test this.

btw, sorry for taking too long to reply. Was away all day long. And again, thanks a lot!
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.