473,385 Members | 1,523 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,385 software developers and data experts.

Generic ADT

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello, I have a simple question, and after a *very long* google search
I still can't get it.

It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I cannot
decide when doing it in C. This is about the actual data storing in a
node. I've seen lots of examples where what is stored is the pointer to
data, but I think it would be better to store the actual object (of
course, you need the element size). For example, this can be the node:

struct node {
void *object;
struct node *next;
}

struct ADT {
struct node *head;
size_t obj_size;
}

and a function could be, for example:

int myfunction (struct ADT *list, void *elem)
{
struct node *tmp;
...
tmp = malloc (sizeof (struct node));
/* Option 1: examples seen googling, store pointer */
tmp->object = elem;

/* Option 2: Seen 1 or 2 times, copying the object itself */
memcpy (&(tmp->object), elem, list->obj_size);
...

I personally think Option 2 is more useful, because you can mess with
stored items by modifying the element in the caller function (please,
correct me :)

The implementation can also change, without using memcpy, by passing a
user-supplied object copy function, this is trivial (I think).

Just looking for some advice, how could you do it? TIA

- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GnuPG ID: 0x3BAABDE1
Don't compare Linux with Windows. There's no colour (except blue).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAlCo20keCtzuqveERAgBOAJ9OUDqLSOE+7ooKdkoxzF Toh0mJdwCfXENC
57YRs3DhVy2+NzxN0829v7k=
=Cs6G
-----END PGP SIGNATURE-----
Nov 14 '05 #1
5 5571

"Alberto Giménez" <al****@teleline.es> wrote in message
It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I
cannot decide when doing it in C. This is about the actual data
storing in a node. I've seen lots of examples where what is stored is
the pointer to data, but I think it would be better to store the actual
object (of course, you need the element size). For example, this can
be the node:

struct node {
void *object;
struct node *next;
}

struct ADT {
struct node *head;
size_t obj_size;
}

If you want to go heavily into abstaract data types then you will find that
C++ has far more support.

If you use C, you will find that the langauge works nicely as long as
everything is passed around as a void * to dynamically-allocated memory.
Unfortunately for heavily-used small objects the overhead can be
considerable, in allocation, storing the pointer itself, and dereferencing
to do anything usual. The normal way round this is to abandon ADTs and write
hard-coded functions for your performance-critical types.
You can of course retain some abstraction at the cost of messing about with
the langauge trying to force it to define extensible structures, copy them
without a call to memcpy(), etc. This isn't generally good programming.
Nov 14 '05 #2
Alberto Giménez wrote:

It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I
cannot decide when doing it in C. This is about the actual data
storing in a node. I've seen lots of examples where what is stored
is the pointer to data, but I think it would be better to store
the actual object (of course, you need the element size). For
example, this can be the node:

.... snip ...

You might take a look at the techniques used in hashlib, available
at:

<http://cbfalconer.home.att.net/download/hashlib.zip>

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

El Sun, 02 May 2004 01:11:37 GMT, CBFalconer escribió:

<http://cbfalconer.home.att.net/download/hashlib.zip>


Hey! I've seen it before. I was just investigating webpages from this
group's members :)

I think hashlib is great, but it uses a user-defined copy function
(or duplication). I wonder if my memcpy option can be as portable as
your approximation, or there any problem with the standard or
portability issues. Thanks a lot.

- --
Alberto Giménez, SimManiac en el IRC
http://www.almorranasozial.es.vg
GnuPG ID: 0x3BAABDE1
Don't compare Linux with Windows. There's no colour (except blue).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFAlN/+0keCtzuqveERAvGYAJwOyp7U8701vcJl7iQyR9UlQjeEngCeO JhZ
Aty9ckDBmGClfx94xEjUWa8=
=sSlS
-----END PGP SIGNATURE-----
Nov 14 '05 #4
Alberto Giménez <al****@teleline.es> wrote in message news:<mn***********@127.0.0.1>...
It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I cannot
decide when doing it in C.


Here's a link to a generic container type for C, that can store a list
of anything that's all the same size - strings, structs, what have
you.

http://www.planet-source-code.com/vb...=4550&lngWId=3
Nov 14 '05 #5

On Sun, 2 May 2004, Kamilche wrote:

Alberto Giménez <al****@teleline.es> wrote:
It's about generic abstract data types (for example, a list). I've
coded it in ADA, and its quite easy to do, but there is a thing I cannot
decide when doing it in C.


Here's a link to a generic container type for C, that can store a list
of anything that's all the same size - strings, structs, what have
you.

http://www.planet-source-code.com/vb.../ShowCode.asp?
txtCodeId=4550&lngWId=3


The example 'main' program is very nice and clean. Unfortunately,
it gets to be so clean by pushing all the dirty bits into "container.c".
Comments:
Style points: "container.c" ought to #include "container.h"; the
inner parentheses in 'malloc(sizeof(*p))' are just visual clutter;
and there ought to be comments in the header describing what the
functions do. Particularly 'Container_Remove' is confusing unless
you have the code right in front of you.
Inconsistency: Why does every function 'assert(p)' except for
'Container_Add', which handles null pointers silently? You should be
consistent: either 'assert' all the time (IMHO a bad idea), or check
for the null pointer every time (IMHO a better idea). Also, as long
as you're checking for NULL, I think it would be nice if
'Container_Add(NULL, foo)' behaved the same way as
'Container_Add(Container_Create(sizeof foo), foo)'. Of course, you
can't do that in a standards-conforming way without macro magic;
but read on.
Big big problem: 'Container_Add' is majorly broken. It tries to
'memcpy' out of a 'va_list', which first of all isn't even possible
if 'va_list' is not a pointer or array type, and secondly is probably
going to crash on a *lot* of systems. There is no portable way to
do what you're trying to do; the simplest solution involves the client
creating a temporary and passing its address as a 'void *' to
'Container_Add'.
Also problem: 'Container_Add' never checks or asserts for NULL on
'Argument'. But since 'Argument' is not needed, you can fix this
pretty easily.
Design bug: 'Container_DupeData' is all well and good, but you have
provided no way for the client to duplicate an entire container (i.e.,
a copy constructor). Rule of Three applies to C ADTs as well, y'know!

[Also, there's an empty 'New Folder' in the ZIP file. Is that
something Planet Source Code sticks in there, or an error?]

HTH,
-Arthur
Nov 14 '05 #6

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

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: Boris | last post by:
I have a class which should like this ideally: generic <typename T> public ref class ManagedClass { T ^managedMember; UnmanagedClass<U*unmanagedMember; }; I actually would like to specify...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
15
by: Lloyd Dupont | last post by:
Don't mistake generic type for what you would like them to be!! IFoo<Ahas nothing in common with IFoo<B>! They are completely different type create dynamically at runtime. What you ask is a...
2
by: ADN | last post by:
Hi, I have a method which calls my service factory: class person { public int ID { get; set: } public string Name {get; set;} } IList<Personmypeople = __serviceFactory.Fetch(new Person())
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.