Reputation: 1451
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!
Upvotes: 1
Views: 1152
Reputation: 124632
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);
Upvotes: 5
Reputation: 145829
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};
Upvotes: 1
Reputation: 48280
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);
Upvotes: 2
Reputation: 96914
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);
Upvotes: 2