2D array allocation problem

Thread Solved

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

2D array allocation problem

 
0
  #1
Jun 6th, 2007
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 1:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,590
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 851
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 2D array allocation problem

 
0
  #2
Jun 6th, 2007
> mat1 = malloc(cols * sizeof(int*));
You've got the rows and cols mixed up in a couple of places.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: 2D array allocation problem

 
0
  #3
Jun 6th, 2007
Originally Posted by Salem View Post
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: 2D array allocation problem

 
0
  #4
Jun 6th, 2007
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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: 2D array allocation problem

 
0
  #5
Jun 7th, 2007
Yes, I see now, what a stupid mistake.

Thanks Narue
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 5795 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC