简体   繁体   English

C结构:初始化有问题吗? Valgrind错误

[英]C Structs: Problem Initializing? Valgrind Errors

Coding in C: 用C编码:

typedef struct {
   char *string;
   int one;
   int two;
} Example;

... elsewhere: ...其他地方:

Example *new = (Example *)malloc(sizeof(Example*));
new->string = strdup(otherstring); //no problem here
new->one = 5; //valgrind says that there is an invalid write of size 4.

My program runs fine, I just can't make valgrind happy. 我的程序运行正常,我只是无法让valgrind开心。 And that probably means I will have some other error elsewhere. 这可能意味着我将在其他地方出现其他错误。

I guess I don't have to declare a pointer to the structure, (ie, I could call "Example new" instead), but I'm stuck finding out how to allocate memory on the heap because I will need to access "new" from other parts of the program. 我猜我不必声明指向该结构的指针(即,我可以调用“ Example new”),但是我一直在寻找如何在堆上分配内存的方法,因为我需要访问“ new” ”来自该计划的其他部分。

Am I making a mistake in the above few lines? 我在以上几行中犯了一个错误吗? Or is my error likely to be somewhere else? 还是我的错误可能在其他地方? This is the first memory error that valgrind reports. 这是valgrind报告的第一个内存错误。

EDIT: accidentally had int *s instead of ints in my struct. 编辑:在我的结构中意外有int * s而不是int。 Fixed. 固定。

I see various problems here. 我在这里看到各种问题。 First of all, this: 首先,这是:

Example *new = (Example *)malloc(sizeof(Example*));

doesn't allocate the right amount of memory (and you don't need the cast). 不会分配正确的内存量(并且您不需要强制转换)。 You want this: 你要这个:

Example *new = malloc(sizeof(Example));

Then, you say this: 然后,你这样说:

new->one = 5;

and that's assigning an int to an int* ; 这就是将一个int分配给一个int* ; that's not a good idea and valgrind rightly complains about it. 这不是一个好主意,Valgrind对此抱怨不已。 If your struct is properly declared, then you want this: 如果您的struct正确声明,那么您需要这样做:

new->one = malloc(sizeof(int)); /* The other malloc doesn't allocate this, you have to. */
*(new->one) = 5;

I suspect (as you say that everything works) that you really mean to declare your struct like this: 我怀疑(正如您所说的那样,一切正常)您真正要声明的是这样的struct

typedef struct {
   char *string;
   int one;
   int *two;
} Example;

but there isn't enough information to be certain. 但是没有足够的信息可以确定。 Then you probably still have similar issues with new->two . 那么您可能仍然对new->two有类似的问题。

Example *new = (Example )malloc(sizeof(Example )); 范例* new =(范例)malloc(sizeof(范例 ));

Should be: 应该:

Example *new = (Example *)malloc(sizeof(Example); 示例* new =(示例*)malloc(sizeof(Example);

You have to allocate the whole struct not just a reference to it. 您必须分配整个结构,而不仅仅是对其的引用。 Sometimes the program runs just because you get lucky. 有时程序运行只是因为您很幸运。

Try this instead (just cut, paste, and run): 请尝试以下操作(只需剪切,粘贴并运行):

Example *new = (Example *)malloc(sizeof(Example));  //This is the correct method as pointed out by Bob, otherwise you're allocating only enough space to fit a memory location vs the struct
new->string = strdup(otherstring);  //this is okay
new->one = (int*)malloc(sizeof(int));
*(new->one) = 5; //You want to assign the value 5 to the new->one memory location, not assign new->one pointer the value of 5!!!

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

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