User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,557 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,680 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2164 | Replies: 4 | Solved
Reply
Join Date: Aug 2005
Location: Bosnia and Herzegovina
Posts: 147
Reputation: Micko is on a distinguished road 
Rep Power: 4
Solved Threads: 1
Micko Micko is offline Offline
Junior Poster

2D array allocation problem

  #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:
#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 1:28 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,257
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: 2D array allocation problem

  #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.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2005
Location: Bosnia and Herzegovina
Posts: 147
Reputation: Micko is on a distinguished road 
Rep Power: 4
Solved Threads: 1
Micko Micko is offline Offline
Junior Poster

Re: 2D array allocation problem

  #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:
mat1 = malloc(cols * sizeof(int*));
  for(i = 0; i < cols; i++)
  {
		mat1[i] = malloc(rows * sizeof(int));
  }
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  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: 2D array allocation problem

  #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]):
mat1 = malloc(cols * sizeof(int*));
for(i = 0; i < cols; i++)
{
  mat1[i] = malloc(rows * sizeof(int));
}
And this prints using row major order (a[rows][cols]):
for (i = 0; i < rows; i++)
  for(j = 0; j < cols; j++)
    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.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Aug 2005
Location: Bosnia and Herzegovina
Posts: 147
Reputation: Micko is on a distinguished road 
Rep Power: 4
Solved Threads: 1
Micko Micko is offline Offline
Junior Poster

Re: 2D array allocation problem

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

Thanks Narue
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC