8

The following code gives a segmentation fault. I am not able to figure out as to why. Please see..

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int **ptr;
    int *val;
    int x = 7;
    val = &x;
    *ptr = (int *)malloc(10 * sizeof (*val));
    *ptr[0] = *val;
    printf("%d\n", *ptr[0] );

    return 0;
}

on debugging with gdb, it says:

Program received signal SIGSEGV, Segmentation fault.

0x0804843f in main () at temp.c:10

*ptr = (int *)malloc(10 * sizeof (*val));

Any help regarding the matter is appreciated.

6
  • I basically want to dynamically allocate an array of pointers. Commented Sep 25, 2012 at 9:13
  • You can't do *ptr until you have first given ptr a value. That's why it segfaults. Commented Sep 25, 2012 at 9:23
  • 2
    *ptr = (int *)malloc(10 * sizeof (*val)); is just allocating 10*intsize. Dont know what you are trying to do Commented Sep 25, 2012 at 9:23
  • @ams, r u getting ant seg-fault??? Commented Sep 25, 2012 at 9:24
  • 1
    @rjayavrp I get one in my head when I read the code. It hurts. If you don't get one then you're just lucky. Commented Sep 25, 2012 at 9:25

5 Answers 5

15
int **ptr; 
*ptr = (int *)malloc(10 * sizeof (*val));

First statement declares a double pointer.
Second dereferences the pointer. In order that you are able to dereference it the pointer should point to some valid memory. it does not hence the seg fault.

If you need to allocate enough memory for array of pointers you need:

ptr = malloc(sizeof(int *) * 10); 

Now ptr points to a memory big enough to hold 10 pointers to int.
Each of the array elements which itself is a pointer can now be accessed using ptr[i] where,

i < 10
Sign up to request clarification or add additional context in comments.

3 Comments

But how come it does not show seg fault in some other machine and for me it does?
@VivekSethi When you make mistakes like this you are invoking undefined behaviour. It is not guaranteed that you will get a segmentation fault; that is just the result of dereferencing some invalid memory locations. It is essentially pot luck as to which invalid memory location you are dereferencing since your pointer is uninitialised and holds some arbitrary, unknown underlying value (usually whatever was left there by some other function or program). So you see, it's completely non-deterministic that this will be "caught".
When you declare the pointer it receives some value that was in the piece of memory that the computer gave it. There are two options then: 1. If that value is a valid pointer, within the memory your program had allocated, it will not die with Segmentation Fault. You may be writing into other data though. 2. If the value is not within the program's memory, then you are trying to use memory that doesn't belong to your process (or is invalid) hence you get segmentation fault. This is the same a Lightness said above, just with other words.
4
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int **ptr;
    int x;

    x = 5;

    ptr = malloc(sizeof(int *) * 10);
    ptr[0] = &x;
    /* etc */

    printf("%d\n", *ptr[0]);

    free(ptr);
    return 0;
}

Comments

3

See the below program, perhaps, it helps to understand better.

#include<stdio.h>
#include <stdlib.h>
int main(){

/* Single Dimention */

int *sdimen,i;
sdimen = malloc ( 10 * sizeof (int));
/* Access elements like single diminution. */
sdimen[0] = 10;
sdimen[1] = 20;

printf ("\n.. %d... %d ", sdimen[0], sdimen[1]);

/* Two dimention ie: **Array of pointers.**  */

int **twodimen;

twodimen = malloc ( sizeof ( int *) * 10);

for (i=0; i<10; i++) {
  twodimen[i] = malloc (sizeof(int) * 5);

}

/* Access array of pointers */

twodimen[0][0] = 10;
twodimen[0][3] = 30;
twodimen[2][3] = 50;

printf ("\n %d ... %d.... %d ", twodimen[0][0], twodimen[0][3], twodimen[2][3]);
return 0;
}

Hope this helps.. ;).

1 Comment

well, this was not exactly what I was looking for. Anyways, it did clear things out in my mind. Thank you.
0

Conceptually if you are using **ptr, then you need to alloacte memory for ptr & *ptr to defrence **ptr.

But in you case you are alloacting memory only for *ptr,if your compiler is smart enough its alloacting memory for ptr(one pointer location) to link *ptr,hence it could able to link ptr->ptr->*ptr.Hence you are not getting Seg Fault.

Comments

0

include

include

int main()    
{    
    int **ptr;    
    int *val;    
    int x = 7;    
    val = &x;    
    ptr = (int**)malloc(sizeof(int**));    
    *ptr = (int *)malloc(10 * sizeof (*val));    
    *ptr[0] = *val;    
    printf("%d\n", *ptr[0] );    
    return 0;    
}

Above would work. You can find the difference and understand the reason as well.

Bottom line you were de-referencing **ptr without allocating memory to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.