简体   繁体   English

正确使用typedef结构的malloc / realloc

[英]Correct usage of malloc/realloc for typedef struct

I am having a completely functional program*, but I do have a question about allocating memory, because something is still unclear for me. 我有一个功能齐全的程序*,但是我确实对分配内存有疑问,因为对我来说尚不清楚。 (*completely functional means, it's having the results I want to have, which can be luck as well). (*完全功能性的手段,它具有我想要的结果,也可能很幸运)。

typedef struct Field {
    long index;
    long x;
    long y;
    struct Cell *N;
    struct Cell *S;
    struct Cell *E;
    struct Cell *W;
    int count;
    bool used;
} Field;

typedef struct Construct {
    Field *fs1;
    Field *fs2;
} Construct;

Field *fields;
long countFields = 1;

Construct *constructs;
long countStructs = 1;

There are two structs, one called Cell, one called Stone, the Cell has coordinates, an index, a count, a bool and pointers to four other cells. 有两种结构,一种称为Cell,一种称为Stone,该Cell具有坐标,索引,计数,布尔和指向其他四个单元的指针。 The Stone just has two pointers to Cells. 石头只有两个指向细胞的指针。 Both are typedefed for the same name, just appending Td. 两者都使用相同的名称进行类型定义,只需附加Td。

So my question is about allocating memory for it. 所以我的问题是为它分配内存。 I am using the following four statements for the first malloc and later on to append more elements. 我将以下四个语句用于第一个malloc,随后再添加更多元素。

That is how I have learned it, I guess a can also substitute "CellTd" with "struct Cell" and so on. 这就是我学到的方法,我猜a也可以用“ struct Cell”代替“ CellTd”等。 x is just a factor to talk about my problem. x只是谈论我的问题的一个因素。

My current problem is, that I am not sure if this is definitely correct, because I am having different results when increasing x. 我当前的问题是,我不确定这是否绝对正确,因为增加x时会得到不同的结果。 So when I am working with it, there is sometimes no serious pointer to some elements when the factor x is 1. I am using x=50 then everything is working, but this should not be, how it's working. 因此,当我使用它时,当因子x为1时,有时并没有严重指向某些元素的指针。我使用x = 50时,一切都在工作,但这不应该是它如何工作。 (Just to say that, for sure, countStones and countCells both in create with the number of elements in the array as well...) (肯定地说,在创建时countStones和countCells以及数组中元素的数量。)

I am doing something wrong with the reallocation? 我在重新分配时做错了什么?

First of all, if you want to allocate an array of things, use calloc . 首先,如果要分配事物数组,请使用calloc It checks for multiplication overflows and nicely initializes all your values with zeros. 它检查乘法溢出,并用零很好地初始化所有值。

Secondly, make sure you're checking your calloc and realloc calls for success. 其次,请确保您正在检查calloc并重新realloc成功调用。 If they fail, they'll return null pointers. 如果失败,它们将返回空指针。 Then you can use perror to determine why they failed, print nice error messages, and quit. 然后,您可以使用perror来确定它们失败的原因,打印出正确的错误消息并退出。

For your logic, here's what a quick gdb session finds with your minimal code (I only changed the type of i on line 75 to be unsigned int ): 为了您的逻辑,这是快速的gdb会话使用最少的代码即可找到的内容(我仅在第75行将i的类型更改为unsigned int ):

At the segfault point: 在段故障点:

(gdb) bt
#0  0x0000000000401112 in evaluateNeighbourNum (original=0x6034b0)
    at cells.c:153
#1  0x0000000000401350 in evaluateNeighbourNum (original=0x612f20)
    at cells.c:201
#2  0x0000000000401509 in calculate () at cells.c:229
#3  0x00000000004016c8 in main () at cells.c:250

(gdb) p *original
$14 = {
  index = 2, 
  x = 1, 
  y = 0, 
  N = 0x411, 
  S = 0x6e20666f206d754e, 
  E = 0x72756f6268676965, 
  W = 0x2029302c31282073, 
  count = 925966394, 
  used = 48
}

As you can see, N is getting filled in with garbage (0x411 is not a valid pointer). 如您所见, N正在被垃圾填充(0x411不是有效的指针)。 Take a look at how you're populating your nodes. 看一下如何填充节点。

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

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