Follow us on Twitter!
One mans freedom fighter, another's terrorist.
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 42
Guests Online: 38
Members Online: 4

Registered Members: 82839
Newest Member: fezphantom
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

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

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
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:

Code
//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:
Moderate
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~


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

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

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
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++ Sad
Author

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

ynori7
Member



Posts: 1486
Location: #valhalla
Joined: 08.10.07
Rank:
God
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.
Code
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.


halls-of-valhalla.org/images/affiliateLogo.png voodoorage.halls-of-valhalla.org/images/smallLogo.png
i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg
ynori7 http://halls-of-valhalla.org
Author

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

chess_rock
Member



Posts: 244
Location:
Joined: 20.02.08
Rank:
Apprentice
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.
Code
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!