简体   繁体   English

C程序中的异常行为,分配内存后变量损坏

[英]Strange behaviour in C program, variable corruption after allocating memory

I have typdef of string in my C program, it looks like that: 我的C程序中有字符串的typdef,它看起来像这样:

#define WRD_LEN 100
typedef char cstring[WRD_LEN];

then at some point I declare dynamic array of this type: 然后在某个时候我声明这种类型的动态数组:

int pcount = 1;
cstring *options = malloc(sizeof(cstring*)*pcount);

I add new strings to this array with use of realloc: 我使用realloc向该数组添加新字符串:

options = realloc(options, sizeof(cstring*)*pcount);
strcpy(options[pcount-1], //some string//);
pcount++;

and then show all entries to user, user choses one of them and that one is passed to another function: 然后向用户显示所有条目,用户选择其中一个并将其传递给另一个函数:

highestof(mode, l, options[btn]);

mode is an integer, l is struct but those are irrelevant now. mode是一个整数,l是struct,但现在不相关了。 btn is number (int) of entry chosed by user. btn是用户选择的条目的编号(整数)。 Up to this point everything works just fine, problem shows up inside highestof function: 到目前为止,一切正常,问题出现在highestof函数内部:

void highestof(const int mode, List l, const cstring cat) {
List *p = malloc(sizeof(List*));

here is definition of List : 这是List的定义:

struct W {
    //some data
    struct W *next;
};
struct List {
    struct W *first;
};

So if highestof function is called with options[1] , cat variable will get corrupted (it will became a set of few random symbols, like "#^?" or "^K^?") right after malloc is called ie before creating dynamic array of List I can use cat as much as I want, but after calling malloc it gets corrupted. 因此,如果使用options [1]调用了Supremeof函数,则在调用malloc之后(即在创建之前), cat变量将被损坏(它将变成一些随机符号的集合,例如“#^?”或“ ^ K ^?”)我可以根据需要使用cat的动态数组,但是在调用malloc之后,它将损坏。 Most strange thing about this is that it happens only if variable passed down to this function was in options array under index of 1 ( options[btn] where btn = 1) For any other value of btn it works no problem. 最奇怪的是,只有当传递给该函数的变量位于索引为1的options数组中( options [btn] ,其中btn = 1)时,它才会发生。对于btn的任何其他值,它都没有问题。

I found a workaround for this, I can create a string (char s[100]) before calling malloc , copy cat value into it and use that variable instead, but it's really not resolving initial problem and it really bothers me. 我找到了解决方法,我可以在调用malloc之前创建一个字符串(char s [100]),将cat值复制到其中,然后使用该变量,但这实际上并不能解决最初的问题,这确实困扰着我。

sizeof(cstring*)*pcount is too small. sizeof(cstring*)*pcount太小。 The size calculation is amiss. 尺寸计算不正确。


Avoid allocation errors. 避免分配错误。 Use this idiom for code that is easier to write correctly, review and maintain. 将此惯用法用于易于正确编写,查看和维护的代码。
Notice no type is used. 注意没有使用任何类型

pointer = malloc(sizeof *pointer * n);

Then code becomes: 然后代码变成:

// options = malloc(sizeof(cstring*)*pcount);
options = malloc(sizeof *options * pcount);`

cstring* is just a pointer, usually four or eight bytes. cstring*只是一个指针,通常为四个或八个字节。 sizeof (cstring*) is therefore a small number, usually four or eight. sizeof (cstring*)是一个很小的数字,通常为4或8。

You are not allocating enough memory for the data, just enough memory to hold pointers to the data. 您没有为数据分配足够的内存,只是为容纳指向数据的指针提供了足够的内存。

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

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