I've read and looked at some example of flexible array members but I am not exactly sure how to add and read elements of this variable length array.
typedef struct School {
char *name;
char *courses[]; //Flexible Array Member
} School;
1) Can someone please show me an example of how I can add an element to this Flexible Length Member and print it after it is stored.
2) I also would like to know how to malloc it correctly. Based on what I have read about Flexible Array Members, we would need to add more space for the flexible array member and can't just use sizeof(School);. My only issue is how do I know how much do add for that flexible member.
You should modify the struct to add the number of courses present in the allocated structure:
typedef struct School {
char *name;
int ncourses;
char *courses[]; //Flexible Array Member
} School;
Say you have 2 schools, one with 3 courses, one with 2. You would allocate the structures this way:
School *mc = malloc(offsetof(struct School, courses) + 3 * sizeof(char *));
mc->name = strdup("Math College");
mc->ncourses = 3;
mc->courses[0] = strdup("Math 101");
mc->courses[1] = strdup("Math 102");
mc->courses[2] = strdup("Math 103");
School *ps = malloc(offsetof(struct School, courses) + 2 * sizeof(char *));
ps->name = strdup("Psycho School");
ps->ncourses = 2;
ps->courses[0] = strdup("Psycho 101");
ps->courses[1] = strdup("Unknown 404");
As you can see, elements of the variable array are accessed like any other array elements. The malloc call allocates the appropriate size in bytes for the struct members and the array elements (here char * pointers), that are located at the end of the structure.
You could use a generic function to allocate and initialize such structures:
School create_school(const char *school_name, int ncourses, char *courses[]) {
School *sp = malloc(offsetof(struct School, courses) + ncourses * sizeof(char *));
sp->name = strdup(school_name);
sp->ncourses = ncourses;
for (int i = 0; i < ncourses; i++) {
sp->courses[i] = strdup(courses[i]);
}
return sp;
}
The exact formula for computing the required size of your structure is:
size_t need = offsetof(struct School, courses) + num_courses * sizeof(char *);
Note the use of offsetof. Some people use sizeof, but this can introduce memory overhead due to structure padding.
Essentially the technique is to dynamically allocate enough memory for the struct, plus the elements of the last array.
School *data = malloc(sizeof(*data) + number * sizeof(*(data->courses)));
for (i = 0; i < number; ++i)
{
const char hello[] = "Hello";
data->courses[i] = malloc(strlen(hello) + 1)); /* sizeof char is 1 by definition */
strcpy(courses[i], hello);
}
Related
I want to allocate a memory to an array of pointers in struct, but I receive the following error:
expression must be a modifiable lvalue
Here's struct code:
typedef struct {
int id;
char *entity[];
}entity;
Here's memory allocation in main function:
entity s;
s.entity= malloc(30 * sizeof(char *));
IDE underlines s.entity and pops the error I mentioned.
Please, help me out to solve this issue.
Your structure does not have a member called entity, only id and set.
You apparently want to allocate the whole structure. This type of struct member called flexible array member is useful if you want to allocate the whole structure in one malloc.
entity *s;
s = malloc(sizeof(*s) + 30 * sizeof(s -> set[0]));
This kind of struct members are very useful as you can realloc or free them in a single call.
Increase the size of the set array to 50
entity *tmp = realloc(s, sizeof(*s) + 50 * sizeof(s -> set[0]));
if(tmp) s = tmp;
Thats how you would allocate the pointers:
typedef struct {
int id;
char **set;
}entity;
int how_many_pointers = 30;
entity s;
s.set= malloc(how_many_pointers * sizeof(char *));
And for each pointer you would have to allocate the space for the corresponding string:
int i, string_size;
for(i = 0; i < how_many_pointers; i++)
{
printf("How many chars should have string number %d ?", i + 1);
scanf("%d", &string_size);
s.set[i] = malloc((string_size + 1) * sizeof(char)); // string + 1 due to space for '\0'
}
I'm having trouble accessing my double pointer struct within my structure.
typedef struct monster
{
char *name;
char *element;
int population;
} monster;
typedef struct region
{
char *name;
int nmonsters;
int total_population;
monster **monsters;
} region;
region **
readRegion (FILE * infile, int *regionCount)
{
region **temp;
char garbage[50];
char garbage2[50];
char rName[50];
int monsterNum;
fscanf (infile, "%d %s", regionCount, garbage);
temp = malloc (*regionCount * sizeof (region *));
for (int i = 0; i < *regionCount; i++)
{
fscanf (infile, "%s%d%s", rName, &monsterNum, garbage2);
temp[i] = createRegion (inFile, rName, monsterNum);
}
return temp;
}
region *
createRegion (FILE * inFile, char *rName, int nMonsters)
{
region *r = malloc (sizeof (region));
char rMonster[50];
int rLength;
r->name = malloc ((strlen (rName) + 1) * sizeof (char));
strcpy (r->name, rName);
r->nmonsters = nMonsters;
for (int i = 0; i < nMonsters; i++)
{
r->monsters.name = (nMonsters * sizeof (r->monsters.name));
fscanf (in, "%s", rMonster);
r->monsters.name = malloc ((strlen (rMonster) + 1) * sizeof (char));
strcpy (r->monsters.name, rMonster);
}
return r;
}
Hopefully my code is readable where you can get the jist of what im trying to do with the monster** monsters pointer in my region struct. Any explnation on how to access and use a double struct pointer within a structure would help.
I've tried to clean up and re-interpret your createRegion to read a lot more like traditional C:
region* createRegion(FILE * inFile, char *rName, int nMonsters) {
region *r = malloc(sizeof(region));
char buffer[1024];
r->name = strdup(rName);
r->nmonsters = nMonsters;
r->monsters = calloc(nMonsters, sizeof(monster*));
for (int i=0; i < nMonsters; i++) {
// Allocate a monster
monster *m = malloc(sizeof(monster));
fscanf(in,"%s", buffer);
m->name = strdup(buffer);
m->element = NULL; // TBD?
m->population = 1; // TBD?
// Put this monster in the monsters pointer array
r->monsters[i] = m;
}
return r;
}
Where the key here is you must allocate the monsters. Here it's done individually, but you could also allocate as a slab:
region* createRegion(FILE * inFile, char *rName, int nMonsters) {
region *r = malloc(sizeof(region));
char buffer[1024];
r->name = strdup(rName);
r->nmonsters = nMonsters;
// Make a single allocation, which is usually what's returned from
// C functions that allocate N of something
monsters* m = calloc(nMonsters, sizeof(monster));
// Normally you'd see a definition like m in the region struct, but
// that's not the case here because reasons.
r->monsters = calloc(nMonsters, sizeof(monster*));
for (int i=0; i < nMonsters; i++) {
fscanf(in,"%s", buffer);
m[i].name = strdup(buffer);
m[i].element = NULL; // TBD?
m[i].population = 1; // TBD?
// Put this monster in the monsters pointer array
r->monsters[i] = &m[i];
}
return r;
}
Note I've switched out the highly quirky strlen-based code with a simple strdup call. It's also very odd to see sizeof(char) used since on any computer you're likely to interface with, be it an embedded microcontroller or a fancy mainframe, that will be 1.
Inasmuch as you are asking about accessing a double pointer inside a structure, I think your issue is mostly about this function:
region *
createRegion (FILE * inFile, char *rName, int nMonsters)
{
region *r = malloc (sizeof (region));
char rMonster[50];
int rLength;
r->name = malloc ((strlen (rName) + 1) * sizeof (char));
strcpy (r->name, rName);
r->nmonsters = nMonsters;
[Point A]
So far, so good, but here you start to run off the rails.
for (int i = 0; i < nMonsters; i++)
{
r->monsters.name = (nMonsters * sizeof (r->monsters.name));
Hold on. r->monsters has type monster **, but you are trying to access it as if it were a monster. Moreover, r->monsters has never had a value assigned to it, so there's very little indeed that you can safely do with it.
I think the idea must be that r->monsters is to be made to point to a dynamically-allocated array of monster *, and that the loop allocates and initializes the monsters, and writes pointers to them into the array.
You need to allocate space for the array, then, but you only need or want to allocate the array once. Do that before the loop, at Point A, above, something like this:
r->monsters = malloc(nMonsters * sizeof(*r->monsters)); // a monster **
Then, inside the loop, you need to allocate space for one monster, and assign a pointer to that to your array:*
r->monsters[i] = malloc(sizeof(*r->monsters[i])); // a monster *
Then, to access the actual monster objects, you need to either dererference and use the direct member selection operator (.) ...
(*r->monsters[i]).name = /* ... */;
... or use the indirect member selection operator (->) ...
r->monsters[i]->name = /* ... */;
. The two are equivalent, but most C programmers seem to prefer the latter style.
At this point, however, I note that in the body of the loop, you seem to be trying to make two separate assignments to the monster's name member. That doesn't make sense, and the first attempt definitely doesn't make sense, because you seem to be trying to assign a number to a pointer.
fscanf (in, "%s", rMonster);
r->monsters.name = malloc ((strlen (rMonster) + 1) * sizeof (char));
strcpy (r->monsters.name, rMonster);
Using the above, then, and taking advantage of the fact that sizeof(char) is 1 by definition, it appears that what you want is
// ...
r->monsters[i]->name = malloc(strlen(rMonster) + 1);
strcpy (r->monsters[i]->name, rMonster);
And finally,
}
return r;
}
Note well that corresponding to the two levels of indirection in type monster **, each access to an individual monster property via r->members requires two levels of derferencing. In the expressions above, one is provided by the indexing operator, [], and the other is provided by the indirect member access operator, ->.
* Or you could allocate space for all of the monsters in one go, before the loop, and inside the loop just initialize them and the array of pointers to them. The use of a monster ** suggests the individual allocation approach, but which to choose depends somewhat on how these will be used. The two options are substantially interchangeable, but not wholly equivalent.
I have the following program that references array elements through a double pointer.
typedef struct {
int num1;
int num2;
int num3;
} DATA_SET;
typedef struct {
int structID;
DATA_SET *data_set_array; // Pointer to an array of DATA_SET structs
} MY_STRUCT;
int main () {
MY_STRUCT *Struct1;
DATA_SET **DataSetArray; // Array of pointers
Struct1 = malloc(sizeof(MY_STRUCT));
Struct1->data_set_array = malloc(sizeof(DATA_SET*)) //Allocate mem for the pointer to array of DATA_SETs
DataSetArray = malloc(sizeof(DATA_SET*) * 2) // Allocate mem for an array of 2 DATA_SET pointers
DataSetArray[0] = malloc(sizeof(DATA_SET)) // Allocate mem for the actual DATA_SET struct
DataSetArray[0]->num1 = 1;
DataSetArray[0]->num2 = 2;
DataSetArray[0]->num3 = 3;
DataSetArray[1] = malloc(sizeof(DATA_SET)) // Allocate mem for the actual DATA_SET struct
DataSetArray[1]->num1 = 1;
DataSetArray[1]->num2 = 2;
DataSetArray[1]->num3 = 3;
memcpy(Struct1->data_set_array, *DataSetArray, sizeof(DATA_SET*); //Copy data set array into Struct1
When I print all the data out in Struct1, i get:
Struct1->data_set_array[0].num1 = 1
Struct1->data_set_array[0].num2 = 2
Struct1->data_set_array[0].num3 = 3
Struct1->data_set_array[1].num1 = 50 //This should be 1
Struct1->data_set_array[1].num2 = 50 //This should be 2
Struct1->data_set_array[1].num3 = 65 //This should be 3
Seems to be misuse/data corruption for the 2nd element in the array.
I know there's probably different ways to do this, but I wanted to get familiar with referencing the array indices via double pointers. Am I allocating memory properly? I have a feeling the memcpy is incorrect.
Struct1->data_set_array = malloc(sizeof(DATA_SET*)
/* ^ wrong */
DataSetArray = malloc(sizeof(DATA_SET*) * 2)
/* ^ wrong */
You need to do sizeof(DATA_SET) because that is the actual size of a struct, not a pointer to a DATA_SET struct.
You're making the mistake of allocating only the size of a pointer times 2, which in many cases can be smaller than you'd hope for.
memcpy(Struct1->data_set_array, *DataSetArray, sizeof(DATA_SET*);
This is also wrong. You must copy the sizeof(DATA_SET). Keep in mind that sizeof(DATA_SET) is not equal to sizeof(DATA_SET*). A pointer's size in bytes is the same regardless of the type of pointer.
What is wrong with my dynamically allocated array of pointers to structs?
DataSetArray is an array of pointers to struct and there is not anything wrong with it.
The problem is 2 other things:
You can't use memcpy on an array of pointers to struct. The memory isn't consecutive.
Further, data_set_array is not an array of pointers to struct so you are trying to copy between incompatible types.
I guess you basically want to create a Double Dimension array. So basically you can do the code like this
struct abc {
int i;
};
struct abc **arr;
arr = (struct abc **) malloc(sizeof(struct abc) * 2);
arr[0] = (struct abc*)malloc(sizeof(struct abc));
arr[0]->i = 100;
arr[1] = (struct abc*)malloc(sizeof(struct abc));
arr[1]->i = 200;
One thing that you need to remember is that the outer array is just a pointer, so it doesn't need to be sizeof(abc) , it could very well be sizeof(int*) / sizeof (void*) etc.
so basically the above code could be written as
struct abc **arr;
arr = (struct abc **) malloc(sizeof(int *) * 2);
arr[0] = (struct abc*)malloc(sizeof(struct abc));
arr[0]->i = 100;
arr[1] = (struct abc*)malloc(sizeof(struct abc));
arr[1]->i = 200;
Hope this helps in underlying memory management you want to do with your code
Here I have a struct:
typedef struct Memo
{
// dynamically allocated HugeInteger array to store our Fibonacci numbers
struct HugeInteger *F;
// the current length (i.e., capacity) of this array
int length;
} Memo;
and this is the struct HugeInteger* within the Memo struct:
typedef struct HugeInteger
{
// a dynamically allocated array to hold the digits of a huge integer
int *digits;
// the length of the array (i.e., number of digits in the huge integer)
int length;
} HugeInteger;
My question is how can I access a member of the digits array within the Hugeinteger struct within the Memo struct?
I have malloced all three like so throughout my code:
Memo *newMemo = malloc(sizeof(Memo));
newMemo->F = malloc(sizeof(HugeInteger) * INIT_MEMO_SIZE); //in this case 44
for (i = 0; i < INIT_MEMO_SIZE; i++)
{
newMemo->F[i].digits = malloc(sizeof(int*) * 1); //creating an array of size 1 to test
newMemo->F[i].digits = NULL;
newMemo->F[i].length = 0;
}
I have tried for example...
newMemo->F[i].digits[0] = 1;
...which results in a segmentation fault. How can I implement the above line of code correctly? I really feel like i'm missing something important here. Thanks.
There's a problem right here:
newMemo->F[i].digits = malloc(sizeof(int) * 1); //creating an array of size 1 to test
newMemo->F[i].digits = NULL;
(Besides the syntax error that I fixed which I assume was a copy/paste error) The second line above replaces the memory address you just allocated with NULL. So that when you do this:
newMemo->F[i].digits[0] = 1;
You're writing to a NULL address.
You want to leave out the NULL assignment.
I have to implement game of life, it is almost complete, the last thing I want to do is to allocate my field dynamical. I'm working under Windows, got no Valgrind and I don't no what's the error in my code. Eclipse shows only that the process is not functional anymore.
Can anyone tell me, what's the problem in my code? Or maybe I don't need a 2 dim. array for game of life field?
struct game_field {
int length;
int **field;
};
static struct game_field *new_game_field(unsigned int l) {
struct game_field *pstField;
pstField = calloc(1, sizeof(struct game_field));
pstField->length = l;
pstField->field = malloc(l * sizeof(int*));
for( int i = 0; i < l; i++ ) {
pstField->field[i] = malloc(l * sizeof(int));
if(NULL == pstField->field[i]) {
printf("No memory for line %d\n",i);
}
}
return pstField;
}
You should think a little bit about the structures and what you are storing.
For the game of life you need to know the state of the cell on the board which is indicated by and integer so your struct should become:
struct game_field {
int length;
int *field;
};
And once you know the dimensions of the field you should allocate it once:
struct game_field *gf = calloc(1, sizeof(struct game_field));
gf->length = <blah>;
gf->field = malloc(gf->length*gf->length*sizeof(int));
This way you have an array of integers that you can use as your board.
The first malloc should be:
pstField->field = malloc(l * sizeof(int*));
Your array is int**, so the first level of allocation is an int*.
Edit: Well, I've tested your code and it does not crash for me. The problem might be somewhere else.
Here's a modification of your code that allocates the field in one block, but still lets you use array brackets for both dimensions:
struct game_field {
int length;
int **field;
};
static struct game_field *new_game_field(unsigned int len)
{
struct game_field *pstField;
pstField = malloc(sizeof(struct game_field));
pstField->length = len;
/* allocate enough space for all the row pointers + the row contents */
pstField->field = malloc((len * sizeof(int *)) + (len * len * sizeof(int)));
/* point the row pointers (at the start of the block) at the row contents
* (further into the block). */
for (int i = 0; i < len; i++)
pstField->field[i] = (int *)(&field[len]) + (i * len);
return pstField;
}
This way you can free the field in one shot:
void free_game_field(struct game_field *gf)
{
free(gf->field);
free(gf);
}
And you can keep the bracket notation to access the elements:
int row7col3 = gf->field[7][3];
Note that what you have (here as well as in your original code) is not exactly a two-dimensional array, but an array of pointers to arrays of integers
(there is a difference, but the arr[x][y] notation can work for either one).