Understanding how to convert void* and int* in C - c

I have written the following code:
typedef struct List {
struct List* next;
void *value;
} List;
void freeList(List* list, void destroyElement(void*)) {
while(list != NULL) {
destroyElement(list->value);
struct List* n = list;
list = list->next;
free(n);
}
}
struct List* arr2list(void** array, int length, void* cpyElement(void*), void (*destroyElement)(void*)) {
struct List* head = NULL;
struct List** tail = &head;
for(int i = 0; i < length; i++) {
*tail = calloc(1, sizeof(struct List));
printf("array[%d] = %d\n",i,*(((int*)array)+i));
if (*tail == NULL) {
freeList(head, destroyElement);
return NULL;
}
tail[0]->value = cpyElement(array[i]);
tail = &(tail[0]->next);
}
*tail = NULL;
return head;
}
void printList(List* list, void echoElement(void*)) {
while (list != NULL) {
echoElement(list->value);
list = list->next;
}
}
void destroyElement(void* el) {
if (el != NULL) {
struct List* node = el;
node->next = NULL;
free(node);
}
}
void* cpyElement(void* el) {
int *p = malloc(sizeof(*p));
*p = *(int *) el;
return p;
}
void echoElement(void* el) {
if (el != NULL) {
printf("%d ", *(int *) el);
}
}
int main(int argc, char** argv) {
int array_length = argc - 1;
int* array = (int*) malloc(sizeof(*array) * array_length);
for (int i = 0; i < array_length; i++){
*(array + i) = atoi(argv[i + 1]);
}
struct List* root = arr2list((void*) array,array_length,cpyElement, destroyElement);
printList(root,echoElement);
freeList(root,destroyElement);
free(array);
return 0;
}
The problem is with tail[0]->value = cpyElement(array[i]);. I get a segmentation fault error for this part. If I write it cpyElement(((int*)array)+i); it works but I want the function arr2list to be generic and not to mention int. How can I solve it? I think that I understand it's impossible to convert void* to int* because it does not know which size to use so is it possible to hear some suggestions on how to approach this issue so it will work? Maybe change the array argument?

You need to create an array of pointers to ints and then pass that. Yes, it's a lot of malloc calls, but it's necessary (since you're using void *).
int main(int argc, char** argv) {
struct List *root;
int i, array_length = argc - 1;
int** array = malloc(sizeof(*array) * array_length);
for (i = 0; i < array_length; i++){
array[i] = malloc(sizeof(*array[i]));
*array[i] = atoi(argv[i + 1]);
}
root = arr2list((void **)array,array_length,cpyElement, destroyElement);
printList(root,echoElement);
freeList(root,destroyElement);
free(array);
return 0;
}
This code:
void destroyElement(void* el) {
if (el != NULL) {
struct List* node = el;
node->next = NULL;
free(node);
}
}
will then need to be changed to (in fact, it only worked before due to a platform-specific bug):
void destroyElement(void* el) {
free(el);
}
Also, do not cast the result of malloc. That means no (int *)malloc(...). Just use malloc(...), it's safer and doesn't cover up errors.

The problem with void * is that while you can freely convert other pointer types to void * and back again and get the original pointer back, you need to do that directly -- you can't pass a void ** where it points at anything other than void *'s and expect it to work.
Even worse, in your case, you're passing an array of int where an array of void * are expected. You can deal with this by casting yout ints to intptr_t and thence to void * to store in your list -- you'll have to do that double-cast back again to get them out again:
void echoElement(void* el) {
printf("%d ", (int)(intptr_t)el);
}
int main(int argc, char** argv) {
int array_length = argc - 1;
void *array = malloc(sizeof(*array) * array_length);
for (int i = 0; i < array_length; i++) {
array[i] = (void *)(intptr_t)atoi(argv[i + 1]);
}
struct List* root = arr2list((void*) array,array_length,cpyElement, destroyElement);
printList(root,echoElement);

Related

How to insert nodes into list by looping?

How to Implement in right way to store values into linked list? In this example the last element will be "0" . Is there a possible to write the content of while loop that allows me don't create the last node which will be "0" after allocating in while loop?
void store(Stack *a, t_important *data)
{
int i;
Stack *tmp;
tmp = a;
i = 0;
while(i < data->length)
{
tmp->n = data->collection_of_ints[i];
tmp->next = malloc(sizeof(Stack));
tmp = tmp->next;
i++;
}
}
Input:
2->6->0->1->3->5->4
Output:
2->6->0->1->3->5->4->0
It is my main function in where i call the store function.
int main(int ac, char **av)
{
Actions action;
Stack *a;
Stack *b;
t_important *data;
if(ac < 2)
return (-1);
data = malloc(sizeof(*data));
stack_nums_counter(av, data);
collect(av, data);
__check__collection(data);
__collecting_ints(data);
action = init();
a = NULL;
b = NULL;
store(&a, data);
__sort_a__(&a, &b, data, action);
return (0);
}
I would make store take a Stack** instead:
void store(Stack **a, t_important *data) {
// find last `next`
while(*a) a = &(*a)->next;
// insert values
for(int i = 0; i < data->length; ++i)
{
*a = malloc(sizeof **a);
(*a)->n = data->collection_of_ints[i];
a = &(*a)->next;
}
*a = NULL; // terminate the linked list
}
and then call it like so
Stack *my_stack = NULL;
store(&my_stack, &some_t_important_instance);
If you instead want to insert the important data first in the linked list, you skip the first while loop to find the last next member:
void store(Stack **a, t_important *data) {
Stack *old_first = *a;
// insert values
for(int i = 0; i < data->length; ++i)
{
*a = malloc(sizeof **a);
(*a)->n = data->collection_of_ints[i];
a = &(*a)->next;
}
// link the last inserted node to the old first node
*a = old_first;
}
You always have uninitialized last node of the stack
tmp->next = malloc(sizeof(Stack));
tmp = tmp->next;
So when the stack is outputted then the program invokes undefined behavior.
Also it seems within the caller instead of declaring a pointer of the type Stack * you declared an object of the type Stack and are passing a pointer to it. It is a bad approach.
Nevertheless using your approach the function can be defined the following way
void store(Stack *a, t_important *data)
{
int i;
Stack *tmp;
tmp = a;
i = 0;
while(i < data->length)
{
if ( i != 0 )
{
tmp->next = malloc(sizeof(Stack));
tmp = tmp->next;
}
tmp->n = data->collection_of_ints[i];
tmp->next = NULL;
i++;
}
}

C Language - Tried to make an array stack data structure to implement a undo/redo "prototype", why isn't it working? :(

Basically I made a create_app() function to allocate 2 nodes in the stack, each having a pointer to an array[max]; undo() pops the last element, and before returning it, it adds it into the REDO node's array. redo() does the opposite, pops the last element in it's array, putting it into Undo's array before returning it. What did I do wrong ?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)
typedef struct node *node_ptr;
struct node
{
int arr_size;
int tos;
int *arr_stack;
node_ptr next;
};
typedef node_ptr STACK;
STACK
create_app(int max)
{
STACK UNDO = (STACK) malloc(sizeof(struct node));
STACK REDO = (STACK) malloc(sizeof(struct node));
{
UNDO->arr_stack == (int *) malloc(max * sizeof(int));
REDO->arr_stack == (int *) malloc(max * sizeof(int));
if(UNDO->arr_stack != NULL){printf("Out of space!");}
else
{
UNDO->tos = EMPTY_TOS;
REDO->tos = EMPTY_TOS;
UNDO->arr_size = max;
REDO->arr_size = max;
UNDO->next = REDO;
REDO->next = UNDO;
return UNDO;
}
}
}
int
isEmpty(STACK S)
{
return(S->tos==-1);
}
int
isFull(STACK S)
{
return(S->tos>=S->arr_size-1);
}
void
push(int x, STACK S)
{
if(isFull(S)){printf("Stack full!");}
else
{
S->arr_stack[++S->tos] = x;
}
}
int
undo(STACK S)
{
if(isEmpty(S)){printf("Nothing to undo!");}
else
{
S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
printf("%d",S->arr_stack[S->tos--]);
}
}
int
redo(STACK S)
{
if(isEmpty(S->next)){printf("Nothing to redo!");}
else
{
int temp = S->next->arr_stack[S->next->tos];
push(S->next->arr_stack[S->next->tos], S);
S->next->tos--;
printf("%d",temp);
}
}
int main()
{
STACK app = create_app(5);
push(1,app);
push(2,app);
push(3,app);
undo(app);
undo(app);
redo(app);
redo(app);
/* Expected output: 3223 */
return 0;
}
Some small errors were in your code, like these ones in create_app() which seem like typos.
UNDO->arr_stack == (int *) malloc(max * sizeof(int));
REDO->arr_stack == (int *) malloc(max * sizeof(int));
^
|
if(UNDO->arr_stack != NULL){printf("Out of space!");}
^
|
...
and some int returning functions did not return anything in the else part which gave some warnings.
Here is the modified code, which worked fine for me
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)
typedef struct node* node_ptr;
struct node
{
int arr_size;
int tos;
int *arr_stack;
node_ptr next;
};
typedef node_ptr STACK;
STACK
create_app(int max)
{
STACK UNDO = (STACK) malloc(sizeof(struct node));
STACK REDO = (STACK) malloc(sizeof(struct node));
{
UNDO->arr_stack = (int *) malloc(max * sizeof(int));
REDO->arr_stack = (int *) malloc(max * sizeof(int));
if(UNDO->arr_stack == NULL){printf("Out of space!");
return NULL;}
else
{
UNDO->tos = EMPTY_TOS;
REDO->tos = EMPTY_TOS;
UNDO->arr_size = max;
REDO->arr_size = max;
UNDO->next = REDO;
REDO->next = UNDO;
return UNDO;
}
}
}
int
isEmpty(STACK S)
{
return (S->tos == -1);
}
int
isFull(STACK S)
{
return (S->tos >= S->arr_size-1);
}
void
push(int x, STACK S)
{
if(isFull(S)){printf("Stack full!");}
else
{
S->arr_stack[++S->tos] = x;
}
}
void
undo(STACK S)
{
if(isEmpty(S)){printf("Nothing to undo!");}
else
{
S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
printf("%d",S->arr_stack[S->tos--]);
}
}
void
redo(STACK S)
{
if(isEmpty(S->next)){printf("Nothing to redo!");}
else
{
int temp = S->next->arr_stack[S->next->tos];
push(S->next->arr_stack[S->next->tos], S);
S->next->tos--;
printf("%d",temp);
}
}
int main()
{
STACK app = create_app(5);
push(1,app);
push(2,app);
push(3,app);
undo(app);
undo(app);
redo(app);
redo(app);
/* Expected output: 3223 */
return 0;
}
Result:
3223
However, always take precaution in deallocating the memory malloced using free().

"Pointer being freed not allocated error"

I'm trying to run a function that deletes the nth element of the linked list (using zero-based indexing). Even though I don't have to necessarily malloc anything, I'm getting this error: "ev(10676,0x7fff73f9d300) malloc: * error for object 0x7fddc2404c10: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug"
Here is the code:
typedef struct intlist intlist;
struct intlist {
int val;
intlist* next;
intlist* intlist_remove_nth(intlist *xs, unsigned int n)
{
int i = 0;
if (n == 1)
{
intlist *tempremovefirst = xs->next;
free(xs);
return tempremovefirst;
}
intlist *temp = xs;
for (temp = xs; i != n - 1; i++)
{
temp = temp->next;
}
intlist *temp2 = temp->next;
temp->next = temp->next->next;
free(temp2);
return xs;
}
void evidence_intlist_remove_nth()
{
intlist *il1 = intlist_cons(1, NULL);
intlist *il2 = intlist_cons(4, il1);
intlist *il3 = intlist_cons(6, il2);
intlist *il4 = intlist_cons(8, il3);
intlist *il5 = intlist_cons(19, il4);
intlist *il6 = intlist_cons(24, il5);
intlist *il7 = intlist_cons(101, il6);
printf("expecting 101 24 19 6 4 1: ");
intlist_print(intlist_remove_nth(il7, 4));
printf("\n");
free(il1);
free(il2);
free(il3);
free(il4);
free(il5);
free(il6);
free(il7);
}
int main(int argc, char *argv[])
{
evidence_intlist_remove_nth();
return 0;
}
If you're not mallocing the items on the list, you shouldn't be freeing them. free() is only used to free memory that has been returned by malloc() or one of its relatives.

SegFault when passing functions in C

I'm getting a SegFault when passing a function pointer through a couple of structs and I can't figure out what I'm doing wrong. Here's the code:
typedef int (*CompareFuncT)( void *, void * );
typedef void (*DestructFuncT)( void * );
struct AVL
{
void * obj;
struct AVL * parent;
struct AVL * leftChild;
struct AVL * rightChild;
};
typedef struct AVL * AVLPtr;
struct SortedList
{
AVLPtr root;
CompareFuncT comp;
DestructFuncT dest;
};
typedef struct SortedList * SortedListPtr;
SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df){
SortedListPtr slp = malloc(sizeof(struct SortedList));
if(slp == NULL){
printf("Not enough space for list\n");
return NULL;
}
slp->root = NULL;
slp->comp = cf;
slp->dest = df;
return slp;
}
AVLPtr avl_insert(AVLPtr root, AVLPtr parent, void * obj, int (*compare)( void *, void * )){
int s = 5;
int k = 6;
compare(&s, &k);
if(root == NULL){
root = malloc(sizeof(struct AVL));
if(root == NULL){
printf ("Out of memory - creating AVL node\n");
return NULL;
}
root->obj = obj;
root->parent = parent;
root->leftChild = NULL;
root->rightChild = NULL;
return root;
}
else if (compare(obj, root->obj) < 0){
root->leftChild = avl_insert(root->leftChild, root, obj, compare);
root = balance(root);
}
else if (compare(obj, root->obj) >= 0){
root->rightChild = avl_insert(root->rightChild, root, obj, compare);
root = balance(root);
}
return root;
}
int SLInsert(SortedListPtr list, void * newObj){
list->root = avl_insert(list->root, newObj, list->comp);
if(list->root == NULL)
return 0;
return 1;
}
int compareInts(void *p1, void *p2)
{
int i1 = *(int*)p1;
int i2 = *(int*)p2;
return i1 - i2;
}
void destroyBasicTypeNoAlloc(void *p) {
return;
}
int main(int argc, char **argv) {
int s = 9;
SortedListPtr list = SLCreate(compareInts, destroyBasicTypeNoAlloc);
SLInsert(list, &s);
return 0;
}
There's obviously more parameters going through the function, but this is the propagation of my compare function. I'm getting a SegFault on the compare in avl_insert. I have a feeling I'm just not passing a pointer where I should be, but I just can't find it.
The error is your call of malloc:
SortedListPtr slp = malloc(sizeof(SortedListPtr));
You are allocating the number of bytes that a pointer takes up, which is incorrect. It should be:
SortedListPtr slp = malloc(sizeof(struct SortedList));

Realloc in C for structs containing dynamic array of structs

Issues with reallocing the items list. I am trying to add items into the testList structs items, but i am getting memory address errors when trying to add or print the values for the individual ListItems. Any help would be greatly appreciated.
struct ListItems
{
int id;
int name;
};
struct testList
{
struct ListItems** items;
int count;
int size;
};
struct Test
{
struct testList* list;
};
void printArray(struct testList* list)
{
for (int i = 0; i < list->count; i++)
{
printf("id=%i, name= %i \n", list->items[i]->id, list->items[i]->name);
fflush(stdout);
}
printf("printing accomplished \n");
fflush(stdout);
}
void growArray(struct testList* list, struct ListItems* item)
{
int size = list->size;
list->items[list->count++] = item;
struct ListItems** user_array = list->items;
//printf("array count %i, array size %i \n", list->count, size);
if (list->size == list->count)
{
struct ListItems* temp = realloc(*user_array, (size * 2) * sizeof (struct ListItems));
if (temp == NULL)
{
printf("it's all falling apart! \n");
}
else
{
*user_array = temp;
list->size = size * 2;
}
}
}
/*
*
*/
int main(int argc, char** argv)
{
struct Test* test = (struct Test*) malloc(sizeof (struct Test));
test->list = (struct testList*) malloc(sizeof (struct testList));
test->list->count = 0;
test->list->size = 1;
test->list->items = (struct ListItems**) malloc(sizeof (struct ListItems*));
for (int i = 0; i < 32; i++)
{
struct ListItems* item = (struct ListItems*) malloc(sizeof (struct ListItems));
item->id = i;
item->name = i;
growArray(test->list, item);
}
printArray(test->list);
for (int j = 0; j < sizeof (test->list->items); j++)
{
free(test->list->items[j]);
}
free(test->list->items);
free(test->list);
free(test);
}
The problem starts with the declaration of struct testList. A pointer to an array of items should have just one *:
struct testList
{
struct ListItems* items; // Changed from pointer-to-pointer
int count;
int size;
};
This will force some other changes in the code.
Your growArray() needs to update list->items. In current code, it will point forever to an 1-element sized area only.
EDIT:
your realloc() allocates for sizeof (struct ListItems)) but pointer holds pointers, not elements.
I would write:
void growArray(struct testList* list, struct ListItems* item)
{
if (list->size <= list->count) {
size_t new_size = 2 * list->size;
struct ListItems** temp = realloc(list->items, new_size * sizeof temp[0]);
assert(temp);
list->size = new_size;
list->items = temp;
}
list->items[list->count] = item;
++list->count;
}
With this, you do not need the initial list->items = malloc(...) in main() but can assign NULL.
EDIT:
for (int j = 0; j < sizeof (test->list->items); j++)
does not make sense; you probably want to j < test->list->count.

Resources