I want to assign a variable void* to an attribut void* from a structure.
My struct :
struct data{
int n;
void * cl;
}typedef data;
Then, I have this function :
void f(void *cl, int n){
struct data *data = malloc(sizeof(data));
data->cl = cl; //"Invalid write of size 8" from valgrind
data->n = n;
}
EDIT : "void *cl" is null, I don't know why (but not data)
And I call it like this :
f(args_f(2), 1);
About args_f :
struct st_args * args_f(int n)
{
struct st_args *args = malloc(sizeof(struct st_args));
if (args == NULL)
return NULL;
args->n = n;
return args;
}
The struct st_args :
struct st_args{
int n;
}typedef st_args;
Inside my function f, when I try the assignement "data->cl = cl;", valgrind says "Invalid write of size 8" on this line. why is that ?
I see a problem here: struct st_args *args is local, and you have returned it, without allocating proper memory.
struct data *data = malloc(sizeof(data));
At this instance, data is a pointer, and malloc would have allocated 4 (or 8) bytes of memory. It should have been,
struct data *data = malloc(sizeof(struct data));
This would allocate (if available) the required memory and segfault would go away.
Additionally, you should check if data is NULL after malloc, if yes then handle the error the way you have done in args_f function.
This is the exact reason why we should be avoiding variable naming convention where user-defined data types and variable names can be same.
For that matter, when I have a typedef, I typically have _t suffix to it to avoid any confusion.
For example,
This
struct data{
int n;
void * cl;
}typedef data;
would be something like this in my code
typedef struct stData {
int32_t i_N;
void *p_cl;
} data_t;
Related
I am having this function fun, where I am passing the nums structure as a parameter. The thing is that I need to convert this field into an integer inside the function.
How can I do this without changing the way I am passing the structure in the function?
Here is what i am trying to do:
struct node{
char *str;
struct node *next;
};
struct numbers{
struct node *head;
int *new_a;
};
void *fun(void *args);
int main(int argc , char *argv[])
{
int *new_a, num_a;
struct node *head=NULL;
struct numbers *args = (struct numbers *)malloc(sizeof(struct numbers));
num_a = returnNum();
pthread_t pthread;
new_a = malloc(1);
*new_a = num_a;
args->new_a=new_a;
if( pthread_create( &pthread , NULL , (void *) &fun , (void *) &args) < 0)
{
perror("could not create thread");
return 1;
}
}
void *fun(void *args){
//void *num_a = (int *) args->new_a;
//int num_a = *(int*)(args->new_a);
struct numbers *temp_str = (struct numbers *) (*args);
int num_a = (int) *(args->new_a);
...
}
Also, how can I do the casting for the head node?
Can anyone please advice?
Since a struct numbers * is being passed to fun, you need to assign the parameter to a variable of this type. Then you can use the struct.
void *fun(void *arg){
struct numbers *temp_str = arg; // no need to cast from void *
int num_a = temp_str->new_a;
...
}
There's also a problem with how you populate the struct:
int *new_a, num_a;
...
new_a = malloc(1);
*new_a = num_a;
args->new_a=new_a;
You're not allocating enough space for new_a. You only allocate 1 byte, but an int on most systems is 4 bytes. When you subsequently read and write from this memory location, you read/write past the end of the memory that was allocated. This invokes undefined behavior which in this case manifests as a crash..
You can fix this by allocating the proper amount of space:
new_a = malloc(sizeof(*new_a));
However, you don't need to use dynamic memory allocation for this field at all. Just declare new_a as int and write to it directly:
struct numbers{
struct node *head;
int new_a;
};
...
args->new_a = returnNum();
You also don't need to take the address of args. It's a pointer, so pass it to pthread_create directly:
if( pthread_create( &pthread , NULL , fun , args) < 0)
If I have several linked structures in C like:
struct structA {
int a;
int b;
struct structA *next;
}
struct structB {
char a;
int b;
struct structB *next;
}
and I dynamically allocate memory like this:
struct structA *mystructA = (struct structA*) malloc(sizeof(struct structA));
mystructA->next = (struct structA*) malloc(sizeof(struct structA));
struct structB *mystructB = (struct structB*) malloc(sizeof(struct structB));
mystructB->next = (struct structB*) malloc(sizeof(struct structB));
do I always have to free it for each struct type like this:
struct structA *p, *pNext;
for (p = mystructA; p != NULL; p = pNext) {
pNext = p->next;
free(p);
}
struct structB *p, *pNext;
for (p = mystructB; p != NULL; p = pNext) {
pNext = p->next;
free(p);
}
or is there any generic solution? I assume there is no other solution because the free() procedure must know how many bytes have to be freed. But maybe I'm wrong and someone can teach me better.
The standard way is to make the "list part" the first element of the structure, and let each derived struct share this same prefix. Since the first element is guaranteed to be placed at offset zero this wil work.
Example snippet:
#include <stdlib.h>
#include <string.h>
struct list {
struct list *next;
};
struct structA {
struct list list;
int a;
int b;
};
struct structB {
struct list list;
char a;
int b;
};
void *create_any(size_t size)
{
struct list *this;
this = malloc (size);
if (!this) return this;
memset(this, 0, size);
this->next = NULL;
return this;
}
void free_all_any(struct list **lp) {
struct list *tmp;
while ((tmp = *lp)) { *lp = tmp->next; free(tmp); }
}
#define CREATE_A() create_any(sizeof(struct structA))
#define CREATE_B() create_any(sizeof(struct structB))
#define FREE_A(pp) free_any((struct list **) pp)
#define FREE_B(pp) free_any((struct list **) pp)
int main(void)
{
struct structA *ap;
struct structB *bp;
ap = CREATE_A ();
bp = CREATE_B ();
// some code here ...
FREE_A( &ap);
FREE_B( &bp);
return 0;
}
This is more or less the method used in the linux kernel, but a lot more preprocessor magic is used there. (and there is no malloc there, obviously)
Since free() accepts pointers to void * and structA and structB both have the same size, you can pass both pointer types.
This is, however, not optimal in terms of elegance. You should think about the following questions:
Why do you have two different structs with the same members?
Why do you not have a generic list item type, such as the following:
struct list_node {
void *data;
struct list_node *next;
}
Actually, this is a very interesting question. The part is true that you have to free() each struct type individually, as they have been malloc()-ed individually, and each memory block has been allocated specifically for that type.Also, on some systems char and int have different storage sizes, but you can try a solution like Phillip provided. For more info, read about the doom memory engine. On a side note, please don't cast malloc() in C. The funny thing is that once the program is terminated, the operating system will reclaim the memory, so if you only deallocate the structures near the end of the program, when you don't need them anymore, it may not be necessary to free() them
I'm working with a binary search tree data structure to sort a series of structs with the type definitions:
typedef struct {
char c;
int index;
} data_t;
typedef struct node node_t;
typedef node {
void *data;
node_t *left;
node_t *right;
}
The node_t typedef is from a library provided to me for this purpose, presumably with a void* pointer to ensure polymorphism. node will be passed into the function:
static void
*recursive_search_tree(node_t *root,
void *key, int cmp(void*,void*))
Within the recursive_search_tree function, I want to be able to modify the code to use the index element as a condition to find the match closest to the index of the linear pass over an array of characters, which would ultimately involve a data_t being passed into *key and key->index being accessed within the function.
The Question
Is it possible to access key->index where key is a void* pointing to a data_t struct, or would this only be possible if data_t was declared as the type for key? I have tried to do the latter, however even casting the pointer to an int doesn't seem to pass the compiler.
Sure it's possible, you'd cast key as type *data_t. (As long as that's really what key points to!)
key /* argument of type void* */
(data_t*)key /* cast as type data_t* */
((data_t*)key)->index /* dereferenced */
Here is a simple example:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char c;
int index;
} data_t;
typedef struct node {
void *data;
struct node *left;
struct node *right;
} node_t;
static int cmp(void *lhs, void *rhs)
{
return ((data_t *)lhs)->index - ((data_t *)rhs)->index;
}
int main(void)
{
data_t d0;
data_t d1;
d0.c = 'A';
d0.index = 1;
d1.c = 'B';
d1.index = 2;
printf("d0 < d1? %s\n", (cmp((void *)&d0, (void *)&d1) < 0 ? "yes" : "no"));
printf("d1 < d0? %s\n", (cmp((void *)&d1, (void *)&d0) < 0 ? "yes" : "no"));
return EXIT_SUCCESS;
}
This is type unsafe, as is any use of void. The use of void is generally because the intermediate is holding onto something it doesn't use for someone else's convenience.
This is a C function to let you hold whatever you want in a tree.
All it does is return whatever pointer you give it.
In your search function
int cmp(void* dt1, void* dt2)
{
data_t* data1 = (data_t*)dt1;
data_t* data2 = (data_t*)dt2;
/* Do what you need with data1 and data2 here */
}
Should let you do whatever you need. The problem you have is you need to cast your values inside the function. The parameters to cmp should exactly match the API for the library you are using, which says void* for the parameters.
I had written a program in C to implement a simple stack. But I am getting segmentation fault in my program and finding it hard to find out what is wrong. Can any one help,
#include<stdio.h>
#include<stdlib.h>
struct stack_structure{
int stack_array[10];
int stack_pointer;
};
void push_into_stack(struct stack_structure *,int);
int main(){
int no = 8;
struct stack_structure *st;
st->stack_pointer = -1;
push_into_stack(st,no);
return 0;
}
void push_into_stack(struct stack_structure *s,int no){
s -> stack_pointer++;
s -> stack_array[s -> stack_pointer] = no;
}
struct stack_structure *st;
This only creates a pointer to a struct stack_structure. It does not allocate memory for the struct stack_structure itself.
You can try with this:
struct stack_structure st;
st.stack_pointer = -1;
push_into_stack(&st,no);
The other option is to dynamically allocate (and free) that structure:
struct stack_structure *st = malloc(sizeof(struct stack_structure));
...
// when you're done with it
free(st);
See these lines:
struct stack_structure *st;
st->stack_pointer = -1;
You've declared a pointer variable but then you're using it uninitialized. A pointer has to point at something, and this one doesn't have anything to point to. The simplest fix would be to change these lines to:
struct stack_structure st1, *st=&st1;
st->stack_pointer = -1;
You need to malloc some space for the structure:
struct stack_structure *st = malloc(sizeof(struct stack_structure));
struct result {
int number;
int length;
};
struct result findLongestSeq(int intarray[], int size) {
result->number // undefined symbol
}
how to access the struct result inside the function findLongestSeq?
thanks
struct result {
int number;
int length;
};
struct result findLongestSeq(int intarray[], int size) {
struct result result;
result.number = 0;
result.length = 42;
return result;
}
If you are dealing with struct result foo then you access its members via foo.number.
If however you are dealing with a pointer to foo (struct result *foo) then you access its members via foo->number.
If you were to manually allocate your result struct via
struct result *result = (struct result *)malloc(sizeof (struct result));
Then you'd have to access its members via result->number (and would be responsible for freeing it once not used anymore).
Further more I'd rather use this for the sake of better readability:
typedef struct {
int number;
int length;
} ResultStruct;
This way you can then use ResultStruct result; instead of redundant and verbose struct result result;.
You have to keep in mind that by simply typing
struct result {
int number;
int length;
};
you only define how a struct with name result actually looks like, i.e. of what parts it is made up. This is a general definition, but you have variable of that type yet.
To access values of this struct you have to create a variable by
struct result myResult;
or however you want to call it. At this point you are able to access the members of this struct with myResult.number