Dynamic Mmeory Allocation with 2D Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Dynamic Mmeory Allocation with 2D Arrays

 
0
  #1
Apr 14th, 2007
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:

  1. int main(){
  2. int **a, x;
  3.  
  4. a = (int **)malloc(sizeof(int) * 10);
  5. for(x = 0; x < 10; x ++) {
  6. a[x] = (int *)malloc(sizeof(int) * 3);
  7. }
  8.  
  9. /* ... */
  10.  
  11. for(x = 0; x < 10; x ++) {
  12. free(a[x]);
  13. }
  14. 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...
Last edited by FoX_; Apr 14th, 2007 at 8:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #2
Apr 14th, 2007
>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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 53
Reputation: FoX_ is an unknown quantity at this point 
Solved Threads: 7
FoX_'s Avatar
FoX_ FoX_ is offline Offline
Junior Poster in Training

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #3
Apr 14th, 2007
I accept I babbled a little but when I thought the 2D arrays' and pointers' mental I really understood it.
Thanks for your helps...
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,611
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #4
Apr 15th, 2007
Originally Posted by FoX_ View Post
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:
  1. int main()
  2. {
  3. const int SIZE = 10;
  4.  
  5. double **arr = (double**)malloc(SIZE * sizeof(*arr));
  6. for(int i = 0; i < SIZE; ++i)
  7. {
  8. arr[i] = (double*)malloc(SIZE * sizeof(*arr[0]));
  9. }
  10.  
  11. for(int i = 0; i < SIZE; ++i)
  12. for(int j = 0; j < SIZE; ++j)
  13. arr[i][j] = i * j + i + j;
  14.  
  15. for(int i = 0; i < SIZE; ++i)
  16. {
  17. for(int j = 0; j < SIZE; ++j)
  18. cout << arr[i][j] << "\t";
  19. cout << '\n';
  20. }
  21. getchar();
  22. return 0;
  23. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #5
Apr 15th, 2007
Heh, I completely missed that little detail...
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,611
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #6
Apr 15th, 2007
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... ;-)
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 120
Reputation: ft3ssgeek is an unknown quantity at this point 
Solved Threads: 7
ft3ssgeek's Avatar
ft3ssgeek ft3ssgeek is offline Offline
Junior Poster

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #7
Apr 15th, 2007
Originally Posted by ~s.o.s~ View Post
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??
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,611
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 465
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #8
Apr 15th, 2007
Yes, you got that right my friend..
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #9
Apr 15th, 2007
Originally Posted by ~s.o.s~ View Post
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.....
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Dynamic Mmeory Allocation with 2D Arrays

 
0
  #10
Apr 15th, 2007
Originally Posted by ~s.o.s~ View Post
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,
  1. int a[50][30] ; // automatic or static storage class
  2. 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).
Last edited by vijayan121; Apr 15th, 2007 at 10:44 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC