dynamic allocation of array of pointers

无人久伴 提交于 2021-02-08 15:44:08

问题


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.


回答1:


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



回答2:


#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;
}



回答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.. ;).




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/12579719/dynamic-allocation-of-array-of-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!