944,137 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 8728
  • C RSS
Jun 6th, 2007
0

2D array allocation problem

Expand Post »
Hello guys,

It's been a while since I last time posted in this forum.
I have strange problem regarding matrix allocation. I was asked to write code that includes 2D array dynamic allocation. I write two version of matrix allocation.
Here is in my opinio relevant part of the code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main( void )
  5. {
  6.  
  7. int rows, cols;
  8. int i, j;
  9. /*int * mat1Blok;*/
  10. int ** mat1;
  11.  
  12.  
  13. printf("Enter number of rows: ");
  14. scanf("%d", &rows);
  15. printf("Enter number of columns: ");
  16. scanf("%d", &cols);
  17.  
  18. /*memory allocation*/
  19.  
  20. /* mat1Blok = malloc(rows * cols * sizeof(int));
  21. mat1 = malloc(rows * sizeof(int*));
  22. for (i = 0; i < rows; ++i)
  23. {
  24. mat1[i] = &mat1Blok[i * cols];
  25. }
  26. */
  27.  
  28. mat1 = malloc(cols * sizeof(int*));
  29. for(i = 0; i < cols; i++)
  30. {
  31. mat1[i] = malloc(rows * sizeof(int));
  32. }
  33.  
  34. printf("\nEnter elements row by row:\n");
  35.  
  36. for (i = 0; i < rows; i++)
  37. for(j = 0; j < cols; j++)
  38. scanf("%d",&mat1[i][j]);
  39. /*
  40.   free (mat1);
  41.   free (mat1Blok);
  42.   */
  43.  
  44. for (i = 0; i < cols; i++)
  45. {
  46. free (mat1[i]);
  47. }
  48. free (mat1);
  49. system("PAUSE");
  50. return 0;
  51. }

I have tested both versions with Dev-Cpp on Windows platform and sent to my friend who discovered that if he uses version which is commented, everything is OK, but if he the use version as in the above code he gets segmentation fault when entering elements. He tested it in Linux. I don't have linux installed and i ask you to test this code and check if it will fail on linux machine. I don't see a reason for such behaviour and simply can't figure out what is wrong. He sad he got segmentation fault with rows = 3 and cols = 2.
Can you please check it?
Thanks
Last edited by Micko; Jun 6th, 2007 at 2:28 am.
Similar Threads
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Jun 6th, 2007
0

Re: 2D array allocation problem

> mat1 = malloc(cols * sizeof(int*));
You've got the rows and cols mixed up in a couple of places.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 6th, 2007
0

Re: 2D array allocation problem

Click to Expand / Collapse  Quote originally posted by Salem ...
> mat1 = malloc(cols * sizeof(int*));
You've got the rows and cols mixed up in a couple of places.
Hmm, this is important part:
  1. mat1 = malloc(cols * sizeof(int*));
  2. for(i = 0; i < cols; i++)
  3. {
  4. mat1[i] = malloc(rows * sizeof(int));
  5. }
First I allocate array of pointerts to int. Every array member will point to new array of integers (columns). In every columns there are exactly "rows" elements.
Still don't understand, why this code works on windows machine and fails on linux.
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Jun 6th, 2007
0

Re: 2D array allocation problem

You're mixing up the meaning of a row and a column. The following allocates using column major order (a[cols][rows]):
  1. mat1 = malloc(cols * sizeof(int*));
  2. for(i = 0; i < cols; i++)
  3. {
  4. mat1[i] = malloc(rows * sizeof(int));
  5. }
And this prints using row major order (a[rows][cols]):
  1. for (i = 0; i < rows; i++)
  2. for(j = 0; j < cols; j++)
  3. scanf("%d",&mat1[i][j]);
Unless rows and cols have the same value, the two aren't interchangeable and you're accessing memory outside the bounds of the array you just allocated. Linux is giving you a segmentation fault.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 7th, 2007
0

Re: 2D array allocation problem

Yes, I see now, what a stupid mistake.

Thanks Narue
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Product Of Even numbers [HELP]
Next Thread in C Forum Timeline: Is the





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC