| | |
2D array allocation problem
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
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:
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
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main( void ) { int rows, cols; int i, j; /*int * mat1Blok;*/ int ** mat1; printf("Enter number of rows: "); scanf("%d", &rows); printf("Enter number of columns: "); scanf("%d", &cols); /*memory allocation*/ /* mat1Blok = malloc(rows * cols * sizeof(int)); mat1 = malloc(rows * sizeof(int*)); for (i = 0; i < rows; ++i) { mat1[i] = &mat1Blok[i * cols]; } */ mat1 = malloc(cols * sizeof(int*)); for(i = 0; i < cols; i++) { mat1[i] = malloc(rows * sizeof(int)); } printf("\nEnter elements row by row:\n"); for (i = 0; i < rows; i++) for(j = 0; j < cols; j++) scanf("%d",&mat1[i][j]); /* free (mat1); free (mat1Blok); */ for (i = 0; i < cols; i++) { free (mat1[i]); } free (mat1); system("PAUSE"); return 0; }
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.
•
•
Join Date: Aug 2005
Posts: 148
Reputation:
Solved Threads: 6
•
•
•
•
> mat1 = malloc(cols * sizeof(int*));
You've got the rows and cols mixed up in a couple of places.
C Syntax (Toggle Plain Text)
mat1 = malloc(cols * sizeof(int*)); for(i = 0; i < cols; i++) { mat1[i] = malloc(rows * sizeof(int)); }
Still don't understand, why this code works on windows machine and fails on linux.
You're mixing up the meaning of a row and a column. The following allocates using column major order (a[cols][rows]):
And this prints using row major order (a[rows][cols]):
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.
C Syntax (Toggle Plain Text)
mat1 = malloc(cols * sizeof(int*)); for(i = 0; i < cols; i++) { mat1[i] = malloc(rows * sizeof(int)); }
C Syntax (Toggle Plain Text)
for (i = 0; i < rows; i++) for(j = 0; j < cols; j++) scanf("%d",&mat1[i][j]);
New members chased away this month: 3
![]() |
Similar Threads
- array/funtion problem, please help (C++)
- Array Index Problem in Quick Sort (C#)
- Is there a simplest way to work this array problem? (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the C Forum
- Previous Thread: Product Of Even numbers [HELP]
- Next Thread: Is the
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






