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 5 1989 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
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 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 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.
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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*);
|
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...
|
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&...
|
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*...
|
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...
|
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...
|
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>
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |