简体   繁体   中英

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. The Stone just has two pointers to Cells. Both are typedefed for the same name, just appending 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.

That is how I have learned it, I guess a can also substitute "CellTd" with "struct Cell" and so on. x is just a factor to talk about my problem.

My current problem is, that I am not sure if this is definitely correct, because I am having different results when increasing 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. (Just to say that, for sure, countStones and countCells both in create with the number of elements in the array as well...)

I am doing something wrong with the reallocation?

First of all, if you want to allocate an array of things, use 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. If they fail, they'll return null pointers. Then you can use perror to determine why they failed, print nice error messages, and quit.

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 ):

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). Take a look at how you're populating your nodes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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