Do you need to allocate memory when you have struct inside another struct?

Asked

Viewed 154 times

0

I want to save and recover data from a struct, but this struct has other struct inside it, I don’t know if I did it correctly, I don’t know if I should use typedef or just struct.

I don’t know if I should wear one malloc() for each sub struct, or the first already calculates all the required space.

Mesas = malloc(sizeof(stTable));
ou 
Mesas->PlayersList[0] = malloc(9*sizeof(stPlayer));
...

I also need that struct stTable *Mesas; be a array with 100 positions. to use type:

Mesas[99]->Flop->Card01 = 10;
Mesas[1]->Flop->Card01 = 2;
ou (qual a diferença)
Mesas[99].Flop.Card01 = 10;
Mesas[1].Flop.Card01 = 2;

typedef struct stCard {
    BYTE carta;
    BYTE pinta;
}stCard;


typedef struct stHand {
    stCard* Card01;
    stCard* Card02;
}stHand;

typedef struct stFlop {
    stCard* Card01;
    stCard* Card02;
    stCard* Card03;
}stFlop;

typedef struct stTurn {
    stCard* Card01;
}stTurn;

typedef struct stRiver {
    stCard* Card01;
}stRiver;

typedef struct stPlayer {
    DWORD Id;
    char Nome[250];
    DWORD BaseAddr;
    DWORD Fichas;
    DWORD Bet;
    DWORD LastBet;
    stHand* Mao;

}stPlayer;

typedef struct stTable {
    BYTE        Id;
    DWORD       BaseAddr;
    DWORD       LastHand;
    DWORD       CurrentHand;
    DWORD       Ante;
    DWORD       SmallBlind;
    DWORD       BigBlind;
    stPlayer*   PlayersList[9];
    stFlop*     Flop;
    stTurn*     Turn;
    stRiver*    River;
}stTable;

struct stTable *Mesas;


void init_t() 
{
    Mesas = malloc(sizeof(stTable));

    Mesas->Id = 100;
}

1 answer

2


That’s not the point. I think it’s dangerous to make such a complex code without understanding what you’re doing, I would reverse it.

I don’t even know if you need one malloc() there. Could be, I’m just saying that the question does not make clear what you want. The way it is needs, but maybe it shouldn’t. But the question says you want to have one array with 100 positions, if you want a array, why not create one in place of the malloc(). If you can’t decide about it, you’re actually doing something far more complex than you can at the moment. But you’re going to insist on malloc() then reserve the space for 100 elements of this type and not just 1 as did. Something like this:

Mesas = malloc(sizeof(stTable) * 100);

One misconception is that it has structs within a struct, and there isn’t, if there were her statement in there. You have several members inside a struct, no matter what these data are, should not have to know what they are and all the data declared there has its space reserved there.

Turns out some of those limbs are pointers, so I guess you get this. I don’t know if you know that a pointer is a data of fixed size (4 bytes on 32-bit or 8-bit architectures in 64-bit) and that you will have this space inside the struct. What you don’t seem to know is that this pointer will be pointing to an object somewhere. This place is probably a space in the heap, after all the struct is already allocated in the heap, then it would be complicated, though not impossible, that they were somewhere else, after all the chance of their life time outside the heap are smaller than the lifespan of struct is great.

So it’s almost certain that for every pointer of these you’ll have to give a malloc() with the size of the type of the object it points to, for example:

Mesas->Flop = malloc(sizeofFlop);

I put in the Github for future reference.

Only then could you put values on this object.

Be careful because you will have to release all this data as soon as they are no longer needed and can’t do before the appropriate time because you could access a location that has already been released but is still in use, yet may have a memory leak or dangling Pointer. This is not simple to do even more without completely mastering all these concepts. Your question is the easy part, managing memory is far more complicated than that.

It is not wrong, but I found the nomenclature adopts bad. And I think the modeling is also not very suitable. Probably not even need all these structs. And I got the impression that maybe you wouldn’t need to have as many pointers as you put in. But again I can’t say anything without knowing the problem in depth. At least to do something without so much knowledge I would make it as simple as possible.

Read about the typedef in What is the correct way to declare a struct in C? and What is the typedef function in struct in C? Can I use struct without it?. The use there is quite inconsistent.

  • I’m used to programming in java POO, now I have a job to do in C and I’m having difficulty understanding this, well, let me try to explain to you better what I’m trying to do.

  • in java if you create a Table class, within this class I can put my variables, gets, sets, even these variables can be another class, that’s where my idea of creating a struct inside another struct comes from, I understand the pointer concept, I don’t think I need these pointers, I’ll rewrite the code and edit the question, if possible read it again and try to help me. i just want to save my data inside a struct like:(sets) table[0]. Playerslist[2]. Name = "Joao"; table[1]. Playerlist[3]. Name = "Maria"; and retrieve values (gets) table[0]. Playerlist[2]. Name;

  • should declare the structure typedef struct stPlayer and use stPlayer , or declare the structure struct stPlayer and use struct stPlayer

  • 2

    Even in Java this modeling would be wrong, but in C more wrong still, you can’t try to do Java in C. And I’m not even going to say that Java already induces people to do a lot of wrong and the most wrong of all is to do things without even understanding why they’re doing it. You can’t rephrase the question, I answered what was asked, change the question invalidates my answer. You asked a question and I answered it. If you have another question you have to ask another question, it is answered. If it doesn’t solve the problem completely it’s just because the question isn’t complete, I can only answer it.

  • already marked as correct, you can read again and help me:

  • 1

    If you’re doing this project to learn, go ahead. If it’s a program to use, better to use a higher-level language than C - it could be Java. You have to take care of a lot more detail - and all critical, than with a language that can already at least create and decrypt objects on its own.

  • 1

    @Victorcosta as I said, this question is answered, if there are others ask new question with your problem now, about the use of malloc(). I repeat, this problem is poorly defined even for Java, it does not seem that it should be so, in C it makes less sense yet. Looks like it should be at most 3 structs (in Java would be 3 classes) and should allocate nothing but the table, and look there (remembering that I am speculating). When you model wrong the entire application is doomed to make one mistake after another. And that’s not how you learn C, it’s not skipping steps because you "know" Java.

  • I have already solved, thanks, and yes I need to allocate memory, because I am working with hook in another application, I used calloc, which already allocates the amount I need for the array, and really did not need more pointers, only the one on the table.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.