Problem with Generic Pointers

C

codergem

Helo !!!
I was trying to implement linked list in which the data field is
Generic...
the structure details are..

typedef struct node
{
void *data;
struct node *next;
}gnode;

Now to add a node into it, and i have written this function.

void insert(gnode **head,void *data,unsigned int size)
{
gnode *temp;
int i=0;
temp=(gnode *)malloc(sizeof(gnode));
temp->data=malloc(size);
for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

temp->next= (*head);
(*head)=temp;
}

Well i dont know how to copy the data into the temp->data.
But I have written the following code...

for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

But it is giving the error : ' void * ' : Unknown size .

If anyone knows any other way of copying it please tell me. And please
help me out in finding what is happening wrong in this code.
Thanks
 
V

Victor Bazarov

I was trying to implement linked list in which the data field is
Generic...
the structure details are..

typedef struct node
{
void *data;
struct node *next;
}gnode;

Now to add a node into it, and i have written this function.

void insert(gnode **head,void *data,unsigned int size)
{
gnode *temp;
int i=0;
temp=(gnode *)malloc(sizeof(gnode));
temp->data=malloc(size);
for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

temp->next= (*head);
(*head)=temp;
}

Why are you programming in C? Who is teaching you that?
Well i dont know how to copy the data into the temp->data.

Use memcpy.
But I have written the following code...

for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

But it is giving the error : ' void * ' : Unknown size .

Of course. Drop the casts. Use memcpy.
If anyone knows any other way of copying it please tell me. And please
help me out in finding what is happening wrong in this code.

Please get yourself a good book on C++ and learn it. What you have
here is C (and not C++). It cannot be "fixed" to become C++, you are
much better off rewriting it in C++.

V
 
A

Andre Kostur

Helo !!!
I was trying to implement linked list in which the data field is
Generic...
the structure details are..

typedef struct node
{
void *data;
struct node *next;
}gnode;

Now to add a node into it, and i have written this function.

void insert(gnode **head,void *data,unsigned int size) {
gnode *temp;
int i=0;
temp=(gnode *)malloc(sizeof(gnode));

why not "new gnode;" ?
temp->data=malloc(size);
for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

why not memcpy?
temp->next= (*head);
(*head)=temp;
}

Well i dont know how to copy the data into the temp->data. But I have
written the following code...

for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

But it is giving the error : ' void * ' : Unknown size .

If anyone knows any other way of copying it please tell me. And please
help me out in finding what is happening wrong in this code. Thanks

And you'll have much bigger problems as soon as you attempt to use
anything other than PODs (Plain Old Data types) in your list. It's not
safe to memcpy (or mem-anything) a full class. As an example, frequently
std::strings do not contain their data directly, but hold a pointer to the
data. If one attempted to use your list to store std::strings, very bad
things will happen. Your code will attempt to do a shallow copy of the
std::string, which means that the contained pointer in the string would be
copied, but not the data. Then when your original string goes out of
scope, it will deallocate the memory it had allocated for its data. But
your container's "copy" of the string will still have a pointer to this
deallocated data, which means that if you try to use that string later on,
you'll be invoking Undefined Behaviour.


Now, out of curiosity: why aren't you using std::list<> which has already
solved all of these problems?
 
C

codergem

Thanks .
But I was looking for a way to do it using some logic, without using
any memcpy function.
Could you please help me in rectifying that error,but still using that
logic only.
Thanks in advance.
 
K

Kai-Uwe Bux

Helo !!!
I was trying to implement linked list in which the data field is
Generic...
the structure details are..

typedef struct node
{
void *data;
struct node *next;
}gnode;

That looks like a pretty useless thing to me: you cannot safely store items
of different types in this list (i.e., you cannot use it as a heterogenous
container) because there is no way to retrieve the type for an individual
item wherefore casting back the data to a correct type becomes impossible.
Now, for a homogenous container, you could just use std::list.
Now to add a node into it, and i have written this function.

void insert(gnode **head,void *data,unsigned int size)
{
gnode *temp;
int i=0;
temp=(gnode *)malloc(sizeof(gnode));
temp->data=malloc(size);
for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

temp->next= (*head);
(*head)=temp;
}

Well i dont know how to copy the data into the temp->data.
But I have written the following code...

for(int i=0;i<size;i++)
*(char *)(temp->data+i) = *(char *)(data + i);

But it is giving the error : ' void * ' : Unknown size .

The problem is that you cannot use pointer arithmetic on void*. Replace all
occurrences of void* with unsigned char* and you will be fine, i.e., the
code will be horrible but it should compile:

struct gnode
{
unsigned char * data;
struct gnode * next;
};

void insert( gnode* & head, unsigned char* data, unsigned int size )
{
gnode* temp = new gnode;
temp->data = new unsigned char [ size ];
for( unsigned int i=0; i < size; ++i ) {
temp->data = data;
}
temp->next = head;
head = temp;
}

As you can see, I did a few other changes to make it look more like C++.

Also, please note that this code is not exception safe: if the second new
throws, the memory allocated by the first new leaks.


Best

Kai-Uwe Bux
 
R

red floyd

Thanks .
> But I was looking for a way to do it using some logic, without using
> any memcpy function.
> Could you please help me in rectifying that error,but still using that
> logic only.
> Thanks in advance.
>

1. Reply moved to the bottom (Top posting is frowned upon in c.l.c++).
2. Why do you want to avoid memcpy? It's probably going to be better
than anything you can write on your own, as the compiler vendor knows
what's going on internally, while you don't.
3. As Victor said, your code is C. You've posted to comp.lang.c++.
4. If you're going to write C and use C-style malloc, then don't cast
the result from malloc, it can mask an error from a missing #include file.
5. Crossposted to comp.lang.c, followup set to comp.lang.c, since this
is apparently C code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top