how to use flexible array in C to keep several values? - c

I have the following code:
typedef struct
{
int name;
int info[1];
} Data;
then I have five variables:
int a, b, c, d, e;
how can I use this as a flexible array to keep all the values of the five variables?

To do this properly, you should declare the flexible array member as an incomplete type:
typedef struct
{
int name;
int info[];
} Data;
Then allocate memory for it dynamically with
Data* data = malloc(sizeof(Data) + sizeof(int[N]));
for(int i=0; i<N; i++)
{
data->info[i] = something; // now use it just as any other array
}
EDIT
Ensure that you are using a C99 compiler for this to work, otherwise you will encounter various problems:
If you allocate an array of length 1, then you will malloc 1 item for the first element of the array together with the struct, and then append N bytes after that. Meaning you are actually allocating N+1 bytes. This is perhaps not what one intended to do, and it makes things needlessly complicated.
(To solve the above problem, GCC had a pre-C99 extension that allowed zero-length arrays, which isn't allowed in standard C.)
Pre-C99, or in any other context than as a flexible array member, C doesn't allow incomplete array types as the one shown in my code.
C99 guarantees that your program is well-defined when using a flexible array member. If you don't use C99, then the compiler might append "struct padding" bytes between the other struct members and the array at the end. Meaning that data->info[0] could point at a struct padding byte and not at the first item in your allocated array. This can cause all kinds of weird, unexpected behavior.
This is why flexible array members were called "struct hack" before C99. They weren't reliable, just a dirty hack which may or may not work.

That kind of structure is a somewhat common idiom in C; the idea is that you allocate extra space at the end of the struct, where the elements of info after the first are actually stored. The size-1 array member at the end of the struct then allows you to use array syntax to access this data.
If you want to store 5 elements you'll have to do:
Data * data=malloc(sizeof(Data)+sizeof(int)*4); /* 4 because the first element is
already included in the size of
the struct */
/* error checking omitted ... */
data->info[0]=a;
data->info[1]=b;
data->info[2]=c;
data->info[3]=d;
data->info[4]=e;
/* ... */
/* when you don't need d anymore remember to deallocate */
free(data);
You may also write a helper function to ease the allocation:
Data * AllocateData(size_t elements)
{
if(elements==0)
return NULL;
return malloc(sizeof(Data)+sizeof(int)*(elements-1));
}
and the example above would be
Data * data=AllocateData(5);
/* then as above */

This is called flexible arrays and was introduced in C99. Often called a struct hack too.
In C99, the flexible array member should be declared without a size.
You need to dynamically allocate memory that can hold more memory than the size of the struct.
As the array is the last member in the struct, you can index it past its size, provided you allocated enough memory for it.
typedef struct
{
int name;
int info[1];
} Data;
Data *d = malloc(sizeof(*d) + (5 * sizeof(int)); //enough for the struct and 5 more ints.
//we have enough room for 6 elements in the info array now
//since the struct has room for 1 element, and we allocated room for another 5 ints
d->info[0] = 1;
d->info[1] = 2;
d->info[2] = 3;
d->info[3] = 4;
d->info[4] = 5;
d->info[5] = 6;
Using an array member with 1 size int info[1]; in this manner is technically undefined behavior - but will work fine on many popular compilers. With a C99 compiler this is supported by a flexible array member declared as int info[];. Read more here

Related

How do we create an array of structure, which has the flexible array as its last field?

Let's say we have:
struct A {
int i;
char c[1];
};
Usually I would use malloc() to create an instance of A, like:
#define LEN 10
struct A *my_A = malloc(sizeof(A) + LEN * sizeof(char));
But this would not work if I try to create an array of A
A struct with a flexible array member cannot be a member of an array. This is explicitly stated in section 6.7.2.1p3 of the C standard:
A structure or union shall not contain a member with incomplete or
function type (hence, a structure shall not contain an instance of
itself, but may contain a pointer to an instance of itself), except
that the last member of a structure with more than one
named member may have incomplete array type; such a structure
(and any union containing, possibly recursively, a member that is
such a structure) shall not be a member of a structure or an element
of an array.
What you would need to do instead is declare the struct with a pointer instead of a flexible array member, and allocate space for each instance.
For example:
struct A {
int i;
char *c;
};
struct A arr[100];
for (int i=0; i<100; i++) {
arr[i].c = malloc(LEN);
}
We don't.
One of the key characteristics of an array is that you know the offset between one element and the next, and you can't do that if the elements are variably-sized.
What you can create is an array of pointers to your flexibly-sized type. How each of the pointed-to objects is allocated is up to you.
It is possible to do what you have shown in your code just using a custom allocation / array iteration mechanism (you won't be able to use the default [] operator, though, because it determines the offset based on the size of the member), but I think that you don't want to do that.
In C/C++, a "flexible" array is just a pointer to an allocated piece of memory in the heap. What you would want to do in this case is this:
struct A {
int i;
char* c; // A pointer to an array
};
#define LEN 10
#define FLEX_ARRAY_LEN 20
struct A* my_A = malloc(sizeof(A) * LEN);
// initialize each array member
for (int i = 0; i < LEN; ++i) {
// allocating new memory chunk for the flexible array of ith member
my_A[i].c = malloc(sizeof(char) * FLEX_ARRAY_LEN);
}

How to allocate memory dynamically for a struct [duplicate]

I have looked around but have been unable to find a solution to what must be a well asked question.
Here is the code I have:
#include <stdlib.h>
struct my_struct {
int n;
char s[]
};
int main()
{
struct my_struct ms;
ms.s = malloc(sizeof(char*)*50);
}
and here is the error gcc gives me:
error: invalid use of flexible array member
I can get it to compile if i declare the declaration of s inside the struct to be
char* s
and this is probably a superior implementation (pointer arithmetic is faster than arrays, yes?)
but I thought in c a declaration of
char s[]
is the same as
char* s
The way you have it written now , used to be called the "struct hack", until C99 blessed it as a "flexible array member". The reason you're getting an error (probably anyway) is that it needs to be followed by a semicolon:
#include <stdlib.h>
struct my_struct {
int n;
char s[];
};
When you allocate space for this, you want to allocate the size of the struct plus the amount of space you want for the array:
struct my_struct *s = malloc(sizeof(struct my_struct) + 50);
In this case, the flexible array member is an array of char, and sizeof(char)==1, so you don't need to multiply by its size, but just like any other malloc you'd need to if it was an array of some other type:
struct dyn_array {
int size;
int data[];
};
struct dyn_array* my_array = malloc(sizeof(struct dyn_array) + 100 * sizeof(int));
Edit: This gives a different result from changing the member to a pointer. In that case, you (normally) need two separate allocations, one for the struct itself, and one for the "extra" data to be pointed to by the pointer. Using a flexible array member you can allocate all the data in a single block.
You need to decide what it is you are trying to do first.
If you want to have a struct with a pointer to an [independent] array inside, you have to declare it as
struct my_struct {
int n;
char *s;
};
In this case you can create the actual struct object in any way you please (like an automatic variable, for example)
struct my_struct ms;
and then allocate the memory for the array independently
ms.s = malloc(50 * sizeof *ms.s);
In fact, there's no general need to allocate the array memory dynamically
struct my_struct ms;
char s[50];
ms.s = s;
It all depends on what kind of lifetime you need from these objects. If your struct is automatic, then in most cases the array would also be automatic. If the struct object owns the array memory, there's simply no point in doing otherwise. If the struct itself is dynamic, then the array should also normally be dynamic.
Note that in this case you have two independent memory blocks: the struct and the array.
A completely different approach would be to use the "struct hack" idiom. In this case the array becomes an integral part of the struct. Both reside in a single block of memory. In C99 the struct would be declared as
struct my_struct {
int n;
char s[];
};
and to create an object you'd have to allocate the whole thing dynamically
struct my_struct *ms = malloc(sizeof *ms + 50 * sizeof *ms->s);
The size of memory block in this case is calculated to accommodate the struct members and the trailing array of run-time size.
Note that in this case you have no option to create such struct objects as static or automatic objects. Structs with flexible array members at the end can only be allocated dynamically in C.
Your assumption about pointer aritmetics being faster then arrays is absolutely incorrect. Arrays work through pointer arithmetics by definition, so they are basically the same. Moreover, a genuine array (not decayed to a pointer) is generally a bit faster than a pointer object. Pointer value has to be read from memory, while the array's location in memory is "known" (or "calculated") from the array object itself.
The use of an array of unspecified size is only allowed at the end of a structure, and only works in some compilers. It is a non-standard compiler extension. (Although I think I remember C++0x will be allowing this.)
The array will not be a separate allocation for from the structure though. So you need to allocate all of my_struct, not just the array part.
What I do is simply give the array a small but non-zero size. Usually 4 for character arrays and 2 for wchar_t arrays to preserve 32 bit alignment.
Then you can take the declared size of the array into account, when you do the allocating. I often don't on the theory that the slop is smaller than the granularity that the heap manager works in in any case.
Also, I think you should not be using sizeof(char*) in your allocation.
This is what I would do.
struct my_struct {
int nAllocated;
char s[4]; // waste 32 bits to guarantee alignment and room for a null-terminator
};
int main()
{
struct my_struct * pms;
int cb = sizeof(*pms) + sizeof(pms->s[0])*50;
pms = (struct my_struct*) malloc(cb);
pms->nAllocated = (cb - sizoef(*pms) + sizeof(pms->s)) / sizeof(pms->s[0]);
}
I suspect the compiler doesn't know how much space it will need to allocate for s[], should you choose to declare an automatic variable with it.
I concur with what Ben said, declare your struct
struct my_struct {
int n;
char s[1];
};
Also, to clarify his comment about storage, declaring char *s won't put the struct on the stack (since it is dynamically allocated) and allocate s in the heap, what it will do is interpret the first sizeof(char *) bytes of your array as a pointer, so you won't be operating on the data you think you are, and probably will be fatal.
It is vital to remember that although the operations on pointers and arrays may be implemented the same way, they are not the same thing.
Arrays will resolve to pointers, and here you must define s as char *s. The struct basically is a container, and must (IIRC) be fixed size, so having a dynamically sized array inside of it simply isn't possible. Since you're mallocing the memory anyway, this shouldn't make any difference in what you're after.
Basically you're saying, s will indicate a memory location. Note that you can still access this later using notation like s[0].
pointer arithmetic is faster than arrays, yes?
Not at all - they're actually the same. arrays translate to pointer arithmetics at compile-time.
char test[100];
test[40] = 12;
// translates to: (test now indicates the starting address of the array)
*(test+40) = 12;
Working code of storing array inside a structure in a c, and how to store value in the array elements Please leave comment if you have any doubts, i will clarify at my best
Structure Define:
struct process{
int process_id;
int tau;
double alpha;
int* process_time;
};
Memory Allocation for process structure:
struct process* process_mem_aloc = (struct process*) malloc(temp_number_of_process * sizeof(struct process));
Looping through multiple process and for each process updating process_time dyanamic array
int process_count = 0;
int tick_count = 0;
while(process_count < number_of_process){
//Memory allocation for each array of the process, will be containting size equal to number_of_ticks: can hold any value
(process_mem_aloc + process_count)->process_time = (int*) malloc(number_of_ticks* sizeof(int));
reading data from line by line from a file, storing into process_time array and then printing it from the stored value, next while loop is inside the process while loop
while(tick_count < number_of_ticks){
fgets(line, LINE_LENGTH, file);
*((process_mem_aloc + process_count)->process_time + tick_count) = convertToInteger(line);;
printf("tick_count : %d , number_of_ticks %d\n",tick_count,*((process_mem_aloc + process_count)->process_time + tick_count));
tick_count++;
}
tick_count = 0;
the code generated will be identical (array and ptr). Apart from the fact that the array one wont compile that is
and BTW - do it c++ and use vector

Is there a way to initialize an array of strings in a struct when you don't know how many elements you will put in the string?

I have this struct:
typedef struct SomeStruct {
char someString[];
} SomeStruct;
This produces an error since someString's size is not defined when initialized.
I want to make someString an array of strings, but I will not know the size of the array at the time of initialization. (The elements that will be in the array will depend on user input later in the program).
Is it possible to initialize this as an array of strings without knowing the size of the array?
Yes, the C standard talks about this in 7.2.18-26. What you are describing is known as a flexible array member of a struct. From the standard:
As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member.
Essentially what it is saying is, if the last member of the struct is an array of undefined size (as might be the case for runtime sizes), then when using the struct, you would allocate the appropriate size of your struct including how large you want the string to be. For example:
typedef struct SomeStruct {
char someString[];
} SomeStruct;
has the flexible array member someString. A common way to use this is:
SomeStruct *p = malloc(sizeof (SomeStruct) + str_size);
Assuming that the call to malloc succeeds, the object pointed to by p behaves, for most purposes, as if p had been declared as:
struct {char someString[str_size]; } *p;
Read the standard for more detail. The buzzword flexible array member will show up a lot of information too. The wikipedia is a good place to start.
You can use a structure with flexible array. For example
typedef struct SomeStruct
{
size_t n;
char someString[];
} SomeStruct;
where n is used to store the number of elements in the array.
Then you can create objects of the structure the following way
SomeStruct *s = malloc( sizeof( SomeStruct ) + 10 * sizeof( char[100] ) );
s->n = 10;
If you can't use a dynamic array (it sounds like this, if you get a compile error for it), you can actually overrun the array, as long as it's at the end of the struct, and as long as you can actually access that memory. Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct SomeStruct {
char someString[10];
} SomeStruct;
int main (void)
{
// Allocate 4x space, so we have room to overrun
SomeStruct *p = malloc(sizeof(SomeStruct) * 4);
p->someString[38] = 'a';
printf("%c\n", p->someString[38]);
}
Of course, you still have to actually allocate the space, so it may not be so useful to you depending on your case.

multiple flexible array in a struct in C?

I have multiple flexible arrays in s struct in c:
typedef struct
{
int a;
float b;
} Name;
typedef struct
{
int c;
int d;
int e;
} NIM;
typedef struct
{
int age;
int time;
Name name[1];
NIM nim[1];
} EthgenSig;
if we have need to allocate memory like this:
malloc(sizeof(EthgenSig) + sizeof(Name) * 10);
how the memory will be allocated and if we operator name array first and then we operate nim array later, then the nim[1] will overwrite the name array, then how to solve it?
You can't have more than one flexible-array member for the exact reason you pointed out.
At most, if you need your data to be kept all in the same memory block, you can make name and nim pointers and set where they point to the correct locations after allocation (making sure not to break any alignment constraint), but the simplest (and most sensible) thing is to just allocate separately the arrays for name and nim.
This is not so hard to do... the key is to realize that arrays and pointers in C both have very similar properties. In fact, array accessor notation has a direct correspondence to pointer notation:
a[b] == *(a + b);
Note that this has the effect of making the name of the array and the index interchangeable, so this is also true:
a[b] == b[a];
You can use this to achieve the result you want. First, declare a structure with two pointer elements. This provides two pointers that will store the base address of the two arrays:
struct two_blocks {
int *x;
int *y;
}
When you allocate this structure, you'll need to allocate extra space for the bodies of the two arrays:
#define X_SIZE 3
#define Y_SIZE 4
two_blocks *data = (two_blocks *)malloc(sizeof(two_blocks)
+ (sizeof(int) * X_SIZE)
+ (sizeof(int) * Y_SIZE));
And then the final step is to initialize the two array pointers. (These expressions use a lot of pointer type casting to ensure that the pointer arithmetic is done in single bytes. Pointer arithmetic is usually done in units of the size of the object being pointed to, to support the array/pointer equivalence I mentioned above.)
data->x = (int *)(((char *)data) + sizeof(two_blocks));
data->y = (int *)(((char *)data) + sizeof(two_blocks) + X_SIZE * sizeof(int));
From there, the arrays can be used like you'd expect:
data->x[2] = 42;
data->x[2] = 42;
A couple observations
Like Matteo said, be careful with alignment. Using this technique is taking memory layout over from the compiler, which can cause unexpected problems. If this caveat makes no sense to you, then you probably shouldn't use this technique.
One of the rationales for using this technique is that it can simplify memory management by reducing the number of frees you need to manage. If you know that your two arrays, x and y, both have the same lifecycle as their enclosing structure, then this removes one potential type of memory leak. (As well as reduces the chance of memory fragmentation by reducing the number of memory blocks.)
Having an array of size 1 is the same as not having an array at all when it comes to the memory layout of this struct.
You may as well have this:
typedef struct
{
int age;
int time;
Name name;
NIM nim;
} EthgenSig;
But I'm assuming that is not what you want. It is pretty hard to tell what you actually want. But I'm assuming that you actually want this:
typedef struct
{
int age;
int time;
Name* name;
NIM* nim;
} EthgenSig;
foo = malloc(sizeof(EthgenSig);
foo.name = malloc(sizeof(Name)*10);
foo.nim = malloc(sizeof(Nim) * 10);

A Struct with an Array of Structs with Arrays inside Them (and an Array) inside It: How would I malloc this?

I currently have no code, because I don't know how to do this at all. Could I just, by myself, calculate how many bytes is needed for each lower-level struct and malloc it to it? That's really terrible coding, isn't it. Here's the two structs I'm trying to mash together:
struct property {
int d;
char name [111]; // I just malloc this like I would a normal array, right?
char descr [1025]; // Ditto.
}
struct category {
int d [413]; // The d's of all the child structs sorted highest to lowest.
char name [111];
struct property property [413]; // This. How do I allocate this?
}</code>
Do I have to do struct property* property = (struct property*) malloc(sizeof(struct property) * 413);? Will the malloc of the array within remain intact? How do mallocs in structs behave in general?
You don't have a pointer member inside your structure property so you don't need to malloc any of your structure members.
When you malloc for the structure it will give you enough memory to hold all the structure members including arrays, exceptions are pointer structure members(You don't have any).
Your malloc without the cast would do fine. It allocates contiguous memory for the whole array. The arrays inside the struct's are all allocated along with it, they are proper arrays and not pointers.
Sizeof will give you the size of your entire structure. It properly accounts for the size of arrays and structures.
However, 413 items seems arbitrary. Would a variable sized structure work better for you?
In that case, calculating the size ahead of time to avoid mallocs is a good performance idea. Malloc can be slow, it can require locks, and the heap can fragment over time. This example shows you how to make a "variable length" structure with a pointer instead of an array or a variable length array at the end of your structure:
struct category
{
int cItems; // need this if handling variable # of items now.
int *d; // ptr instead of array
char *name; // ptr again
struct property property[0]; // var length array
}
int cItems = 413; // or whatever
// this is a nifty trick to get the size of a variable length struct:
int cbCategory = (size_t)(&((struct category*)0)->property[cItems]);
int cbD = sizeof(int)*cItems;
int cbName = sizeof(char)*cItems;
struct category *pCategory = (struct category*)malloc(cbCategory + cbD + cbName);
// wire up d:
pCategory->d = (int*)((char*)pCategory + cbCategory);
// or wire d up this way:
pCategory->d = (int*)&pCategory->property[cItems];
// wire up name
pCategory->name = (char*)pCategory->d + cbD;
// or wire up name this way
pCategory->name = (char*)&pCategory->d[cItems];
// set items
pCategory->cItems = cItems;
Note, I assumed that d had 413 elements to it. I could have just as easily left it a an array.

Resources