I am new to C and have a question about initializing a structure. I am using a structure that I have not created and so I don't know what is inside of it. Therefore, I did not initialize it but the compiler complained. So I set it equal to NULL but I got a segmentation fault. Then I looked up how to set everything to 0 and it said to set it equal to {0}. That too gave me a segmentation fault. Since I know the function I am calling is correct, and that the array I pass it is of the correct size, I am almost certain it has something to do with the way I initialize the structure. The initialization is
struct aes_ctx *aes_struct = {0}
The header for the function is
void aes_setkey(aes_ctx *aes, const void *key, u_int len);
The way that I am calling it is
aes_setkey(aes_struct, aes, CCA_STRENGTH);
where aes is a buffer of size 16, CCA_STRENGTH is a constant int of 16.
To sum up the problem, I think that the way that I initialize the structure causes it to be unusable later on. Any help that can be given to me on this would be so appreciated!
Thanks!
You cannot initialize a pointer that way (well, you can, it just doesn't point to anythign valid). Something like this is what you are after:
struct aes_ctx aes_struct = {0};
You can then pass the address of aes_struct to the function, but that depends on whether or not you need to dynamically allocate this thing (required scope and the size of the type will dictate this).
So...
struct aes_ctx aes_struct = {0};
aes_setkey(&aes_struct, aes, CCA_STRENGTH);
Or
struct aes_ctx *aes_struct = malloc(sizeof(struct aes_ctx));
/* you may want to initialize the structure via memset or some init function */
aes_setkey(aes_struct, aes, CCA_STRENGTH);
The key is that you're not initializing a structure, but a pointer. This means that what you're doing is setting the pointer to be an invalid address, which causes a seg fault when it tries to access that address. You either need to allocate memory:
struct aes_ctx* aes_struct = malloc(sizeof(struct aes_ctx));
or create the actual struct (not a pointer) and pass the address:
struct aes_ctx aes_struct = {0};
aes_setkey(&aes_struct, aes, CCA_STRENGTH);
You're not allocating memory for the pointer to point to. You can use either
struct aes_ctx aes_struct = {0}; // Declare the struct, not a pointer to it
aes_setkey(&aes_struct, aes, CCA_STRENGTH);
Or, if you want to allocate the memory dynamically,
struct aes_ctx *aes_struct = malloc(sizeof(struct aes_ctx));
memset(aes_struct, 0, sizeof(struct aes_ctx); // Only if you need to zero the data
aes_setkey(aes_struct, aes, CCA_STRENGTH);
struct aes_ctx *aes_struct = {0};
By doing so you are actually initializing the aes_struct pointer to 0.
The two initializations:
struct aes_ctx *aes_struct = {0};
and
struct aes_ctx *aes_struct = 0;
are equivalent. C says you can add optional {} when initializing a scalar. So basically you are not initializing your pointer to a structure with members initialized to zero but you are initializing the pointer with a null pointer constant.
Although, you can perform the former in one shot using compound literals:
struct aes_ctx *aes_struct = (struct aes_ctx) {0};
Related
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
I have a little problem with using struct array in C. It returns me segmentation fault
struct Yset{
char *unit;
char *name;
char *showValue;
char *fillColor;
char *yData;
};
struct Yset *svg_ysets;
int ySetCounter = 0;
svg_ysets[ySetCounter].unit = malloc(strlen((char*)attribute)+1);
strcpy(svg_ysets[ySetCounter].unit,(char*)attribute);
printf("%s\n", svg_ysets[ySetCounter].unit);
ySetCounter++;
What's wrong with this using? I dont know the number of Ysets so it must be dynamic. Thanks
svg_ysets[ySetCounter].unit = malloc(strlen((char*)attribute)+1);
You are dereferencing pointer to structure while the pointer doesn't point to meaningful place. In other words you didn't allocate space to hold the structure object itself.
// allocate five objects
struct Yset *svg_ysets = malloc (5 * sizeof(struct Yset));
Don't forget to free if you are using dynamically allocated memory somewhere.
I'm working my way through the learn c the hard way book and have run into a few issues on Exercise 19. The author said that ex19 was intended for the learners to get to know the macro in c. I have no problem in understanding the concept of that, but I just don't understand everything else. I can't understand how the object prototype is created.
Especilly,what does the following sentense mean?
Since C puts the Room.proto field first, that means the el pointer is
really only pointing at enough of the block of memory to see a full
Object struct. It has no idea that it's even called proto.
the relevant code is this:
// this seems weird, but we can make a struct of one size,
// then point a different pointer at it to "cast" it
Object *el = calloc(1, size);
*el = proto;
can anyone tell me how on earth malloc/calloc exactly works? As far as i know, it just allocate the required number of memory and return the first address. If so, how can the computer know the data struct of the allocated memory? like in the code, after Room *arena = NEW(Room, "The arena, with the minotaur");,you can do this directly arena->bad_guy = NEW(Monster, "The evil minotaur"); how does the computer know there is a bad_guy??
what on earth is the content of *el after the above two statements(Object *el = calloc(1, size); and *el = proto;)?
Any help will be appreciated!!
the link to the exercise: http://c.learncodethehardway.org/book/ex19.html
calloc has the additional feature that it fills the allocated memory with zero bytes, whereas using the equivalent malloc call would require an additional step if all or some of the allocation needs to be zero initially.
In the code
arena->bad_guy = NEW(Monster, "The evil minotaur");
the compiler knows the layout of the struct because the access is through the arena variable, which is declared as a pointer to Room, which is presumably a typedef of a struct.
For the other part, the guarantee of ordering within structs allows a limited form of inheritance in composite structs, or extended structs.
struct A {
int x;
};
struct B {
int foo;
double baloney;
};
struct B (or a pointer to it) can be cast to a (pointer to a) struct A because they both begin with an int. Of course, if you cast the other way, the struct A must have been originally a struct B or access to the baloney field will be undefined. In other words, struct B essentially begins with a struct A.
This may be easier to see if I rewrite my example like this:
struct A {
int x;
};
struct B {
struct A foo;
double baloney;
};
Now you can get a struct A out of struct B in different ways.
struct A a;
struct B b;
a = b.foo; // regular member variable access
struct A *ap = &a;
struct B *bp = &b;
ap = (struct A *)bp; // cast the pointer
ap = & b.foo; // take a pointer from the member variable
ap = & bp->foo; // take a pointer from the member variable via a pointer
All it does is to alloc 1*size bytes. There's nothing magic with malloc/calloc. He is passing the sizeof(T) to the function through that NEW macro and putting it in Object_new's size parameter. So all the function knows is the size in bytes.
I'm trying to use memset on a struct element like so:
memset( &targs[i]->cs, 0, sizeof( xcpu ) );
However, doing so gives me a segmentation fault. I neither understand why this is failing, nor how I can make it work. What is the proper way to use memset on an element of a struct, and why does my method not work?
Line which allocates memory for targs:
eargs **targs = (eargs **) malloc(p * sizeof(eargs *));
Struct definitions for struct element cs (xcpu_context) and struct targs (execute_args):
typedef struct xcpu_context {
unsigned char *memory;
unsigned short regs[X_MAX_REGS];
unsigned short pc;
unsigned short state;
unsigned short itr;
unsigned short id;
unsigned short num;
} xcpu;
typedef struct execute_args {
int ticks;
int quantum;
xcpu cs;
} eargs;
You have allocated an array of pointers in the line
eargs **targs = (eargs **) malloc(p * sizeof(eargs *));
but you haven't initialized the elements themselves. So this segfault has nothing to do with properly using memset on the fields of a struct, but instead derives from using uininitialized memory (assuming that you don't have a loop to initialize each eargs object after you allocate the array of pointers).
Instead, if you wanted to allocate a dynamic array of p eargs objects (I'm using the term "objects" loosely here), you would write
eargs *args = malloc(p * sizeof(eargs));
if (!args) {
/* Exit with an error message */
}
memset(&(args[i].cs), 0, sizeof(xcpu));
instead. Note that args is a dynamically allocated array of eargs objects, not a dynamically allocated array of pointers, so it's of type eargs * rather than eargs **.
Your memory allocation line doesn't allocate any memory for any structures, only for pointers to structures. If you want to allocate memory for that whole array, you need to add a loop to allocate memory for the structures themselves:
for (i = 0; i < p; i++)
targs[i] = malloc(sizeof(eargs));
Once you actually have structures to operate on, your memset() call should be fine.
struct myStruct
{
short int myarr[1000];//want to initialize all elements to 0
}
How do I initialize the array?
I tried doing short int* myarr[1000]={0} inside the struct but it's wrong. How can I do this? I don't mind doing it in the implementation file. This struct is contained in a header file.
Use the universal initializer: {0}.
The universal initializer works for anything and initializes the elements to the proper 0 (NULL for pointers, 0 for ints, 0.0 for doubles, ...):
struct myStruct example1 = {0};
struct myStruct example2[42] = {0};
struct myStruct *example3 = {0};
Edit for dynamically allocated objects.
If you're allocating memory dynamically use calloc rather than malloc.
p = malloc(nelems * sizeof *p); /* uninitialized objects; p[2] is indeterminate */
q = calloc(nelems, sizeof *q); /* initialized to zero; q[2] is all zeros */
With realloc (and possibly other situations) you need to memset.
If it is declared out of a function (not on the stack), the whole struct will be zeroed at compile time.
Otherwise, you can use memset after declaring it.
Just initialize an instance of the struct with {0}, this will zero your array as well. Alternatively, use memset as NKCSS demonstrates.
int's arn't reference types, don't they get initialized after allocating memory for your structure?
You could just do this:
memset(&myStruct, 0, sizeof(myStruct));