473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Problem with Generic Pointers

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

Jun 2 '06 #1
5 1993
co******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 2 '06 #2
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.
Victor Bazarov wrote:
co******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jun 2 '06 #3
co******@gmail.com wrote:
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[i] = data[i];
}
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
Jun 2 '06 #4
co******@gmail.com wrote:


Victor Bazarov wrote:
co******@gmail.com wrote:
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++.

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.
Jun 2 '06 #5
On Fri, 02 Jun 2006 12:49:06 -0700, "codergem wrote:
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?
Jun 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
8
by: Cool Guy | last post by:
From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>: | As with the previous use of the Stack<T> class created with the Order | type, another instance of the specialized...
1
by: Albert | last post by:
Hello all, I've got several questions and I'm sure you'll find them easy to answer, but sadly I don't know the answers though: What do people mean by generic pointers Why is a pointer to void...
11
by: Michael | last post by:
Hi, I am trying to get an idea of how function pointers work. I have the following: #include <stdio.h> void do_stuff(int*,int,void*); void getInt(int*); void showInt(int*);
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
1
by: Bit Byte | last post by:
I was trying to put together a little utility generic collection class like so: template <class T> class Collection { public: Collection(); Collection(const Collection&); Collection&...
7
by: juerg.lemke | last post by:
Hi everyone I am interested in having multiple functions with different prototypes and deciding, by setting a pointer, which of them to use later in the program. Eg: int f1(void); char*...
0
by: inquisitive | last post by:
Is it possible to have a vector of generic function pointers? I am able to devise a generic function pointer, this way: template <typename elemType, elemType (*function)(std::string&)> struct...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.