简体   繁体   English

将结构内使用的 void * 指针类型转换为 int * 指针时出现问题!

[英]Problem when typecasting a void * pointer used inside a structure to an int * pointer!

My code is as follows,我的代码如下,

#include<stdio.h>
struct data
{
    int a ;
    void *b;
};

int main()
{
    struct data *d;
    int *ptr;

    int key=10000;
    d->b=&key;

    ptr=(int *)d->b;
    printf("%d\n",*ptr);
}

And i get a segmentation fault??我得到一个分段错误? Any idea why??知道为什么吗? Thanks in advance for any help提前感谢您的帮助

struct data *d merely declares a pointer. struct data *d仅仅声明了一个指针。 You have not allocated this struct anywhere.您尚未在任何地方分配此结构。 You need to either malloc it or declare it just as struct data d on the stack or globally.您需要malloc它或将其声明为堆栈或全局的struct data d

The former can be done like this:前者可以这样做:

d = malloc(sizeof(struct data));

If you choose the latter, accessing b has to be written as db .如果选择后者,访问b必须写为db

You are not allocating any memory for d .您没有为d分配任何 memory 。 It likely points to an invalid memory area and so - segmentation fault.它可能指向无效的 memory 区域等等 - 分段错误。

You can solve this like so:你可以像这样解决这个问题:

struct data *d = malloc(sizeof(*d));

You are getting segmentation fault at the line d->b=&key;您在d->b=&key;行遇到分段错误。 Note that you have not allocated any memory location to the structure variable d .请注意,您尚未将任何 memory 位置分配给结构变量d So d contains some garbage value, and d->b it trying to use that garbage address to dereference the pointer and get the component b .所以d包含一些垃圾值,并且d->b它试图使用该垃圾地址来取消引用指针并获取组件b Here is where you get the segfault.这是你得到段错误的地方。 Either statically allocate the struct variable, or use malloc to dynamically allocate it.要么静态分配结构变量,要么使用malloc动态分配。

int main()
{
    struct data *d;
    int *ptr;

    /* Here you are allocating memory to the
     * pointer variable, which will be used to
     * point to the structure type data
     */
    d = malloc (sizeof (struct data)); 
    int key=10000;

    /* Now you can dereference the pointer 
     * and get any of the components of the
     * structure, because 'd' contains a valid
     * address.
     */ 
    d->b=&key;

    ptr=(int *)d->b;
    printf("%d\n",*ptr);

    /* Good practice to free the memory location
     * you have allocated. Not freeing will lead to
     * memory leak in larger applications. After you 
     * free the memory location denoted by the address
     * stored in 'd', you will not be anymore access 
     * the contents of it.
     */
    free (d);

    /* d->b; or d->a; is no more possible at this point
     * as we have freed the memory pointed by 'd'
     */
}

Or you can use:或者您可以使用:

int main()
{
    /* Not a pointer, statically allocated */
    struct data d;
    int *ptr;

    int key=10000;
    d.b=&key;

    ptr=(int *)d.b;
    printf("%d\n",*ptr);
}

So, it is not the typecasting of void * to int * that causes the segfault.因此,导致段错误的不是void *int *的类型转换。 Its the illegal memory reference of the pointer variable which you have used but not allocated/initialized.它是您使用但未分配/初始化的指针变量的非法 memory 引用。

The problem is that you didn't allocate memory for ad pointer: struct data *d;问题是您没有为广告指针分配 memory: struct data *d; . . This lines only creates a pointer, it doesn't alloc memory for it.此行仅创建一个指针,它不会为它分配 memory。 Please try the following code:请尝试以下代码:

int main()
{
    struct data *d = (struct data*)malloc(sizeof(struct data));
    int *ptr;
    int key=10000;
    d->b=&key;
    ptr=(int *)d->b;
    printf("%d\n",*ptr);
    free(d);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM