开发者

add value to struct to pointer segmentation error in C

people, i've an issue now..

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

typedef struct a
{
 int *aa;
 int *bb;
 struct b *wakata;
}a;

typedef struct b
{
 int *you;
 int *me;
}b;

int main()
{
a *aq;
aq = (a*)malloc(sizeof(a*));

*aq->wakata->you = 1;
*aq->wakata->me = 2;

free(aq);


return 0;
}

and compiled, then debugged :

gc开发者_运维百科c -o tes tes.c --debug
sapajabole@cintajangankaupergi:/tmp$ gdb -q ./tes
Reading symbols from /tmp/tes...done.
(gdb) r
Starting program: /tmp/tes 

Program received signal SIGSEGV, Segmentation fault.
0x08048414 in main () at tes.c:22
22  *aq->wakata->you = 1;

well, the question is, how to set the value to variable inside struct 'b' through struct 'a' ?

anyone ?


The initial allocation of a is only allocating 4 bytes (in a 32-bit architecture). It should be:

aq = (a*)malloc(sizeof(a));

And wakata has not been initialized: Maybe this:

aq->wakata = (b*)malloc(sizeof(b));

And it will need a corresponding free as well prior to the free of aq.

free(aq->wakata);

And since you have pointers to the integers, those would also need to be allocated (you and me). But it is not clear if that is your goal. You probably should remove the * from the int declarations so that they are simply int members rather than the pointers to int.


Looks like you have a few mistakes here. See the code below. In general a few things to keep in mind. You can't access memory before you malloc it. Also, there is a difference between memory and pointers e.g. int and int *

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

typedef struct a
{
 int aa;
 int bb;
 struct b *wakata;
}a;

typedef struct b
{
 int you;
 int me;
}b;

int main()
{
 a * aq = malloc(sizeof(a));

 aq->wakata = malloc(sizeof(b))
 aq->wakata->you = 1;
 aq->wakata->me = 2;

 free(aq->wakata)
 free(aq);

 return 0;
}


wakata isn't pointing to any valid memory. You have to malloc memory for it, and then also for wakata->you and wakata->me


Pointers do not contain data. They point at data. That is why they are called pointers.

When you malloc enough space to store an a instance named aq, you allocate space for the pointers contained in that structure. You do not cause them to point at anything, nor do you allocate space to contain the things that they would point at.


You're not allocating space for b in struct a. You have defined 'a' as holding pointers, not structs. Also, I think malloc(sizeof(a*)) should be malloc(sizeof(a))

aq = (a*)malloc(sizeof(a)); // You should probably use calloc here
aq->wakata = (b*)malloc(sizeof(b));

you and me don't seem to need to be pointers, just normal ints


You have some problems with your code.

  1. When you allocate memory for the struct a, you should do

    aq = (a*)malloc(sizeof(a));

  2. You now allocated memory for the struct a, but not for the struct b pointed by the wakata member, so you need to do

    aq->wakata = (b*)malloc(sizeof(b));

  3. Finally, in the struct b there should not be int* members, but int members. This way, you'll be able to correctly assign a value to them.

Remember that you should check for the correct allocation of memory by checking if the malloc return value is not NULL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜