Bobazonski
Bobazonski

Reputation: 1565

How to dynamically allocate memory within structs?

For this week's assignment in my CS class, we have to revise our current program to dynamically allocate memory for all arrays. We have to ensure that no memory is wasted on empty elements. We have to do this by assignment character arrays to temp arrays, size checking the temp, and then allocating an array of that size.

Anyway, I don't get how to do it with a struct. I know how to dynamically allocate memory, for example

int* pointer = new int(5);

But how do I do that for struct members? It seems like unless you define the size up front, the computer wouldn't know how much space to allocate when you declare a struct of that type.

Right now, my struct definition is like so:

struct card
   {
   char rank[10];
   char suit[10];
   char color;
   bool dealt;
   char location[12];
   };

How do I change all the arrays to be declared dynamically, without specifying size?

Upvotes: 1

Views: 9633

Answers (4)

Cem Karaca
Cem Karaca

Reputation: 17

In any case you don't need to change your struct variables, just change how you call it by using typedef:

typdef struct _CARD
   {
   char rank[10];
   char suit[10];
   char color;
   bool dealt;
   char location[12];
   } CARD;

To initialize an array of CARD's use:

CARD* card;
#define ARRSIZE 12

card = (CARD*)malloc(sizeof(CARD)*ARRSIZE);

For more dynamic struct allocation, use linked list by calling struct itself as a pointer: typdef struct _CARD

 typdef struct _CARD {
   char rank[10];
   char suit[10];
   char color;
   bool dealt;
   char location[12];
   struct _CARD  *_next;
   } CARD;

and initialize every struct as needed and bind them using _next pointer:

    CARD *card,*tmp,*final,*first,*etc;
    card = (CARD*)malloc(sizeof(CARD));
    tmp= (CARD*)malloc(sizeof(CARD));
    card->_next = tmp;
    final = (CARD*)malloc(sizeof(CARD));
    tmp->_next = final;
    final->_next = NULL;

and so on, you need to have additional functions to manage dynamically created structs and their bonds.

Upvotes: -1

Karthik T
Karthik T

Reputation: 31952

struct card
{
   char* rank;
   char* suit;
   char color;
   bool dealt;
   char* location;
};

The compiler doesnt need to know the size you are allocating, since all this struct would store is a pointer to the allocated memory.

The size of the pointers, and thus the struct, would be the same if you allocate 1 element each or 1000 elements each.

card c;
c.location = new char[10];

p.s. Just checking, are you aware that the below code allocated 1 element and sets that to 5, rather than allocate 5 elements? See wikipedia for the difference between the two uses.

int* pointer = new int(5);

Upvotes: 3

m.ding
m.ding

Reputation: 3182

using a char* rank? and when you use it, card->rank = malloc(sizeof(char)*size)?

Upvotes: 1

51k
51k

Reputation: 1443

struct card
   {
    char *rank;
    char *suit;
    char color;
    bool dealt;
    char *location;
   };

Just take pointers inside the structure and dynamically allocate memory to them, dont forget to free them after use..

Upvotes: 3

Related Questions