0
#include <stdlib.h>

typedef struct stackObject
{
  void* obj;
  struct stackObject *next;
} StackObject_t;

typedef struct stackMeta
{
  StackObject_t *stack;
  size_t objsize;
  int numelem;
} StackMeta_t;

//CREATE
StackMeta_t *mystack_create(size_t objsize)
{
  StackMeta_t *elem;
  elem = (StackMeta_t*)malloc(sizeof(StackMeta_t));
    if(elem == NULL)
    {
      return NULL;
    }
    else
    {
      elem->stack = NULL; // my actual stack basically the first elem(the top)
      elem->objsize = objsize;  // size of the datatype
      elem->numelem = 0;        // total count of elem inside the stack
    }
    return elem;
}

//PUSH
int mystack_push(StackMeta_t *data_stack, void* obj)
{
    if(data_stack == NULL)
    {
      return -1;
    }
    
    StackObject_t *nodeObject = NULL;
    nodeObject = (StackObject_t*)malloc(sizeof(StackObject_t));
    if(nodeObject == NULL)
    {
      return -1;
    }
    
    if(data_stack->stack == NULL)
    {
      nodeObject->next = NULL;
      memcpy(nodeObject->obj, obj, data_stack->objsize);
      data_stack->stack = nodeObject;
      data_stack->numelem++;
    }
    else
    {
      nodeObject->next = data_stack->stack;
      memcpy(nodeObject->obj, obj, data_stack->objsize);
      data_stack->stack = nodeObject;
      data_stack->numelem++;
    }

    return 0;
}

int main() {
  StackMeta_t *METADATA = NULL;
  int obj = 1;
  METADATA = mystack_create(sizeof(int));
  mystack_push(METADATA, &obj);
  return 0;
}

This code is Stack with Linked List inside.

So I am trying to copy int obj value to void* obj. from my understanding objcan be any data type so i chose it to be an int type. I am using a visual online tool to see my heap memory and I saw that the value of the 3rd parameter is size_t objsize = 4.

I cannot pin point where my problem is and I have not tested if this has memory leaks. can someone explain this to me with an example on how to copy void pointers?

trincot
  • 317,000
  • 35
  • 244
  • 286
Heroking18
  • 77
  • 6
  • It seems kind of inside-out that your stack object has a `next` pointer in it. Why don't you have a stack, as in an array of these and an index, or put the `next` property in `stackMeta` directly? There's a lot of code here working against itself. A cleaner design would help considerably. – tadman Oct 15 '22 at 19:12
  • 1
    When you do `memcpy(nodeObject->obj, obj, data_stack->objsize);`, where is `nodeObject->obj` pointing? You're right about something being uninitialized, but it's not a `size_t`. There's no difference between pointers to specific types (like e.g. `int *`) and generic any-pointers like `void *`, they still need to point somewhere valid. – Some programmer dude Oct 15 '22 at 19:12
  • I'd expect to see a function like `stack_add(void* value, size_t size)`. Instead you allocate, then...just shoehorn the value in after? – tadman Oct 15 '22 at 19:12
  • `memcpy(nodeObject->obj, obj, data_stack->objsize);` should do the trick. What problems are you having? – tadman Oct 15 '22 at 19:14
  • im having `ERROR: Conditional jump or move depends on uninitialised value(s)` but yeah I see that `nodeObject->obj` is not pointing to anything but isn't it suppose to point to the `const void* source` using memcpy()? – Heroking18 Oct 15 '22 at 19:19
  • 2
    A `void *` pointer can point to any kind of data, but it must be made to actually point to something before you use it. `memcpy` doesn't allocate memory, and it can't make the destination pointer point anywhere. You must make it point somewhere valid first before you call `memcpy`: `nodeObject->obj = malloc(data_stack->objsize);` – Some programmer dude Oct 15 '22 at 19:50
  • Also note that in my example above, I [don't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858). It's not needed, and it shouldn't really be done. – Some programmer dude Oct 15 '22 at 19:50
  • Also to simplify things a bit, I would probably use a [flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member) so that only a single allocation is needed for the whole node structure, including the data. – Some programmer dude Oct 15 '22 at 19:59

0 Answers0