segmentation fault : ?????

This is a discussion on segmentation fault : ????? within the C Programming forums, part of the General Programming Boards category; Hi ! I recently switched to linux and started compiling my c programs on linux, I am using red hat ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    segmentation fault : ?????

    Hi !

    I recently switched to linux and started compiling my c programs on linux, I am using red hat 9.0 which has a gcc compiler. These are very simple programs that i tried to compile, htey compile fine but on running them I get a segmentation fault, which I don't really understand why. I read up on segmentation faults and it said it was due memory allocation error. I don't understand how, since these programs work fine in windows ????

    The programs are:

    program 1 : This prints a pascal triangle

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    
    int main()
    
    {
    
    	int **pastriang, row=10, column=10, i, j;
    
    	pastriang = (int**) malloc(row);
    
    	for(i=0; i<row; i++)
    
    		pastriang[i] = (int*) malloc(column);
    
    	for(i=0; i<row; i++)
    
    		for(j=0; j<column; j++)
    
    			pastriang[i][j] = 0;
    
    	for(i=0; i<row; i++)
    
    	{
    
    		for(j=0; j<column; j++)
    
    		{
    
    			if((i==j) || (j==0) )
    
    				pastriang[i][j] = 1;
    
    			else if(j>i)
    
    				pastriang[i][j] = 0;
    
    			else 
    
    				pastriang[i][j] = pastriang[i-1][j-1] + pastriang[i-1][j];   
    
    		}
    
    	} 
    
    	for(i=0; i<row; i++)
    
    	{
    
    		for(j=0; j<column, j<=i; j++)
    
    			printf("%3d", pastriang[i][j]);
    
    		printf("\n");
    
    	}
    	return 0;
    
    }
    program 2: Selection sort ( works 3 and smaller size arrays and segmentation fault at bigger arrays)

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    
    
    
    
    
    int main()
    
    {
    
    	int *A, d, i, j;
    
    	printf("enter the number of elements in the array\n");
    
    	scanf("%d", &d);
    
    	A = (int*) malloc(d);
    
    	for(i=0; i<d; i++)
    
    		scanf("%d", &A[i]);
    
    	int size= d;
    
    
    
    // -------------------------------- SELECTION SORT ------------------------------------------------//
    
    
    
    	for(i=0; i<d; i++)
    
    	{
    
    		int position =0;
    
    		int temp1 = A[0];
    
    		int temp2 =0;
    
    		for(j=0; j<size; j++)
    
    		{
    
    			if(j == 0)
    
    				continue;
    
    			else
    
    			{
    
    				if(A[j]> temp1)
    
    				{
    
    					position =j;
    
    					temp1 = A[j];
    
    				}
    
    			}
    
    		}
    
    		temp2 = A[size-1];
    
    		A[size-1] = temp1;
    
    		A[position] = temp2;
    
    		size = size-1;
    
    	}
    
    
    
    
    
    for(i=0; i<d; i++)
    
    		printf("%d ", A[i]);
    
    
    	return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where to begin...

    1) Don't typecast the return of malloc. Read the FAQ if you want to know why it's incorrect in C.
    2) Just what exactly do you think you're allocating here:
    Code:
    pastriang = (int**) malloc(row);
    It's not what you think it is. It's not ten pointers-to-pointers-to-ints. It's simply 10 bytes. (You have 'row' defined as the number 10.)

    3) You have the same problem here:
    Code:
    pastriang[i] = (int*) malloc(column);
    Even if that was the right amount, you'd still be wrong. This is how you allocate a dynamic two dimensional array:
    Code:
    int x, y,z;
    int **array;
    
    y = 20;
    x = 10;
    
    array = malloc( sizeof( int *) * y ); /* Allocate Y rows. */
    for( z = 0; z < y; z++ )
        array[ z ] = malloc( sizeof( int ) * x ); /* Allocate X integers per row. */
    You free that in the reverse order. Free the rows, then free the initial allocation.

    At any rate, your main problem, or one of them anyway, is you're allocating the wrong amount of space in all of your examples.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    Thanks for the help quzah, I understand my mistake !!!!

    I initially wrote these programs in VC++ and they worked fine and just recently switched to linux so I am testing some oldies to get a ground up base in C in linux......

    I am still surprised that these compiled fine in VC++ but they give errors in linux, is it the compiler ..... does anyone have any hints on why this happened ???

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    They still shouldnt work on VC++.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by sand_man
    They still shouldnt work on VC++.
    memory on linux (better gcc) is allocated byte by byte..
    on windows I assume after some experience and other's opinion, that memory is allocated page by page, which could be 1 kb, 2 kb whatever long. further calls to malloc return a pointer inside that page.. that's why working with memory in windows is less erroneous.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's "less erroneous"? Don't you mean it lets isn't as good a platform to learn under, because stupid things that shouldn't work sometimes seem to? That code should always segfault.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21