954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Mmeory Allocation with 2D Arrays

Hi all;

I've to use a dynamic array(2D) in my program in C.

I've read some tutorials about this and I understood its mental.But still there exist a few points which I didn't understand.

Here is a code:

int main(){
int **a, x;

a = (int **)malloc(sizeof(int) * 10);
for(x = 0; x < 10; x ++) {
    a[x] = (int *)malloc(sizeof(int) * 3);
}

/* ... */

for(x = 0; x < 10; x ++) {
    free(a[x]);
}
free(a);


Firstly: Does the **a mean that first * points the row and second * points the column???(If not what does it mean???)

Secondly: In the "a[x] = (int *)malloc(sizeof(int) * 3);" , malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.So what is the problem???

Thanks for your helps...

FoX_
Junior Poster in Training
53 posts since Mar 2007
Reputation Points: 10
Solved Threads: 7
 

>Does the **a mean that first * points the row and second * points the column???
Almost. The first * points to a row, however the second * points to an individual "cell" if you will, of a row. It points to the integer.

>malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
It assigns the address of the freshly-allocated memory to a[x], yes.

>But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.
What do you mean? a[x] just holds a pointer to the actual memory, you'll have to dereference the address contained inside a[x] if you want to print out the value.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I accept I babbled a little but when I thought the 2D arrays' and pointers' mental I really understood it.
Thanks for your helps...:cool:

FoX_
Junior Poster in Training
53 posts since Mar 2007
Reputation Points: 10
Solved Threads: 7
 
a = (int **)malloc(sizeof(int) * 10);
for(x = 0; x < 10; x ++) {
    a[x] = (int *)malloc(sizeof(int) * 3);
}


In this case you were lucky enough to have the size of integer the same as the size of a pointer. If it would have been array of doubles, you would have messed big time. a = malloc(sizeof(int*) * 10);

A better way would have been:

int main()
{
    const int SIZE = 10;

    double **arr = (double**)malloc(SIZE * sizeof(*arr));
    for(int i = 0; i < SIZE; ++i)
    {
        arr[i] = (double*)malloc(SIZE * sizeof(*arr[0]));
    }

    for(int i = 0; i < SIZE; ++i)
        for(int j = 0; j < SIZE; ++j)
            arr[i][j] = i * j + i + j;

    for(int i = 0; i < SIZE; ++i)
    {
        for(int j = 0; j < SIZE; ++j)
            cout << arr[i][j] << "\t";
        cout << '\n';
    }
    getchar();
    return 0;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Heh, I completely missed that little detail... :icon_redface:

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)


So, uh, in other words, don't follow your lead?? ;)

ft3ssgeek
Junior Poster
126 posts since Mar 2007
Reputation Points: 9
Solved Threads: 7
 

Yes, you got that right my friend.. :)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
Just to let you know that the old smileys are deprecated, you are better off using the new ones, the ones which recent compiler..err Daniweb currently supports... ;-)


Nooooo..... :icon_biggrin:

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 
const int SIZE = 10; double **arr = (double**)malloc(SIZE * sizeof(*arr)); for(int i = 0; i < SIZE; ++i) { arr[i] = (double*)malloc(SIZE * sizeof(*arr[0])); } }[/code]


This is first allocating an array of pointers, and allocating memory one by one for each one dimensional
array. (this kind of array is called a jagged array in C#).
A true two dimensional array is rectangular; a contigous area of memory which contains I*J elements.
we simulate this in C by creating an array, every element of which is an array. for example,

int a[50][30] ; // automatic or static storage class
int (*b)[30] = malloc( sizeof( int[30] ) * 50 ) ; // dynamic storage class


note:b is really a pointer to the first element (which is an array of 30 integers).

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Yes I agree, treating the array which I created as being contiguous would be a mistake. The code snippet was just meant to correct the OP's typo.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Thanks for your answers again.I understood it better.(with the help of drawing diagrams of memory on a paper...:) )

FoX_
Junior Poster in Training
53 posts since Mar 2007
Reputation Points: 10
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You