C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-27-2008, 12:30 AM   #1
Registered User
 
Join Date: Jul 2008
Posts: 16
Abstract data types in a queue

Hello - I've written a queue and I'm trying to keep it generic as far as the data it could hold. For that purpose, my node structure is defined as such:

Code:
struct node {
void* data; struct node* next;
}node;
The void* is because I don't want to restrict the data type in the node (int, char, struct). My problem is adding the data to the queue. In my enqueue method, I create a new node to add the data to:
Code:
void enqueue(queue* q, void* item)
{
//.... // Create a new node and assign data node* new_node = (node*)malloc(sizeof(node)); new_node->data = malloc(sizeof(item)); memcpy(new_node->data, item,strlen((char *)item)); //...
}
My problem is using the memcpy function. I don't know how to copy exactly the size of the data type I intend to pass because of the generality of the code. In the above example I had to use strlen when I wanted to test it with a char*, but I would like to make it work for every data type without having to do a specific cast every time. I'd really appreciate help on this - thanks!
sa125 is offline   Reply With Quote
Old 07-27-2008, 12:42 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
You could add another parameter to specify the size of the data.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 07-27-2008, 08:46 AM   #3
*
 
noops's Avatar
 
Join Date: Jun 2008
Posts: 108
Do you need memcpy? Couldn't you just set new_node->data = item?

I am actually working on the same thing as you right now. And that is the approach I am taking.
noops is offline   Reply With Quote
Old 07-27-2008, 10:37 AM   #4
Registered User
 
Join Date: Jul 2008
Posts: 16
I tried that but it referenced the same item in memory for all nodes. So, in my tester.c file I inserted the string "node %d" for i = 0; i < 5, and got:
node 4
node 4
node 4
node 4
node 4

Which drove me crazy. So this is how I'm working around it.
sa125 is offline   Reply With Quote
Old 07-27-2008, 11:22 AM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by sa125 View Post
I tried that but it referenced the same item in memory for all nodes. So, in my tester.c file I inserted the string "node %d" for i = 0; i < 5, and got:
node 4
node 4
node 4
node 4
node 4

Which drove me crazy. So this is how I'm working around it.
Did you have something like this?
Code:
int i, temp;
for (i = 0; i < 5; i++) {
    printf("Enter number for queue: ");
    scanf("%d", &temp);
    enqueue(queue_ptr, &temp);
}
If you're reusing the same variable when making the queue, then you'll need to malloc and memcpy inside enqueue; if all the data is already distinct in your main program, then you can just assign pointers. (And "passing sizeof data" is already an established idiom in C; that's how the library functions that use void *, like bsearch and qsort, work.)
tabstop is offline   Reply With Quote
Old 07-27-2008, 02:51 PM   #6
*
 
noops's Avatar
 
Join Date: Jun 2008
Posts: 108
I did have the same problem as you probably for the reason tabstop mentioned. I was reusing the same variable to add data. Alloc new memory for each piece of data that you add to the queue.
noops is offline   Reply With Quote
Reply

Tags
adt, casting, memcpy, queue

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
reading binary file with different data types larne C Programming 8 07-29-2008 10:12 AM
data structure design for data aggregation George2 C# Programming 0 05-20-2008 06:43 AM
Need help with ADT (abstract data types) ortegac C Programming 1 03-30-2006 02:23 AM
Possible to pack smaller data types into larger ones? Heraclitus C Programming 3 02-09-2003 03:22 PM
C Programming Question TK A Brief History of Cprogramming.com 13 07-04-2002 07:11 PM


All times are GMT -6. The time now is 12:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22