LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2008, 05:27 AM   #1
KareemSedki
LQ Newbie
 
Registered: Oct 2007
Posts: 9

Rep: Reputation: 0
How to Use Type Identifiers as Function Parameters in C99?


Hello, all

I would appreciate if someone tells me how to allow type identifiers as function parameters in C99. I am interested in built-in types as well as my own defined types. I am trying to implement a container for a programme I am working on. This container needs to know the type of elements it will store, so the type of elements should be passed to the container's init() function. The container is much like an array but its length and size are to be encapsulated in the container itself. I am working with SunC99 and GNU C99. I would like to know about any workaround be it in C or assembler, be it Linux-based or a general one, because I can't use "void" as the only type.

I guess va_arg implementation may be similar although I know it is constrained to self-promoting types. But I don't know how va_arg may implemented. I thought of C++ templates but I am not sure how to implement such behaviour in C99.

Thank you in advance.
 
Old 05-01-2008, 08:56 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Is it possible you could explain "I would like to know about any workaround be it in C or assembler, be it Linux-based or a general one, because I can't use "void" as the only type." as this is the normal method in C, are the contained types the same in an instance of the container?
 
Old 05-02-2008, 05:28 AM   #3
KareemSedki
LQ Newbie
 
Registered: Oct 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for your interest. What I want to do is something like that:

Container * cContainer(type); // getting a container.

store(Container * container, Type * instance);
or
store(Container * container, Type type, Instance * instance);


The type of instance should be known to the container in either case. All instances should be of the same type.

Each user-defined type contained in the programme I am working on is obtained by calling a constructor that allocates memory for and initialises the requested instance. So, I thought that I could use numerical or textual representations of types and embed them in instances as appropriate when their constructor is called. But this method has two drawbacks:
1) built-in types cannot be handled, int and the like
2) if the store function is to be called on types that were not constructed to encapsulate and return their own types, most probably the programme will crash.

If I know how to pass type identifiers to functions this can resolved. This sort of functionality is important because parts of the system we are building are shared libraries. Those I am working one will be used again in the future in other systems. So, the "typing" issue should be automatic without programmer's interference.

I hope it is clearer by now and again thanks for your interest.
 
Old 05-02-2008, 07:15 AM   #4
seraphim172
Member
 
Registered: May 2008
Posts: 101

Rep: Reputation: 15
argv enum switch

The implementation of va_arg is a pointer to a pointer list, like:

Code:
char argv[][] = {
    "string1", "string2", "string3" };
or
Code:
char argv[3][16] = {
    "string1", "string2", "string3" };
Your current concept of containers and instances seems much too complicated for very little gain. It is possible to do object-oriented stuff in C, but it is certainly more straightforward to do it in C++.

If it has to be C, I would rather use an enumeration for a list of types and pass it as an integer opcode to the container function.

Code:
enum my_enumeration {
    type_a = 0, type_b, type_c };
To avoid crashes for unknown types use a switch statement with doing nothing as the default branch for unknown type opcodes.

My suggestions might not be what you're looking for. I admit that your posts still do not make it clear what you want to achieve exactly.

Linux Archive

Last edited by seraphim172; 06-05-2008 at 10:32 AM.
 
Old 05-02-2008, 11:45 AM   #5
KareemSedki
LQ Newbie
 
Registered: Oct 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for the hint about va_arg. I hope you give me more details. One benefit of such a container is that it can be used in place of an array or a dynamically allocated pointer. This eliminates the potential of memory segmentation or memory access faults due to length miscalculation or a mistaken one. This is so because a function having this container as an argument instead of an array can calculate the size and length of the container without programmer's intervention.

For example:

typedef struct type_t{
size_t length;
double * vector;
}Type;

fillVector(Type * instance, double v []); // prototype

double vector[30];

fillVector(Type * t, vector);

Now, if t->length > 30 this will result in segmentation fault. But if fillVector() can determine the length of v, it can return before executing the loop, say, or it can throw an exception. This is one benefit.

I am sticking with C instead of C++ because performance is really important to the programme. For example, I am using an exception handling mechanism coded in C, if I were to use that of C++, about 8,600 more instructions were to be fetched per execution of 3 functions. The programme is really a mathematical nightmare as it takes about 9 hours in average to complete.

The use of object-orientation is not really necessary as you said, but it really helps a lot in simplifying abstractions related to the programme. This is important because the problem is already complicated so any way of simplifying it is welcome.

Enumeration is a good idea. But there are some cons as well. This will require the enumeration to be manually updated for each programme that needs to use the shared library. Because it will probably be the case that each programme will define new types. The container can avoid this if it is there because once the type is defined all that needs to be done is passing the type identifier to the container and it will handle the type. If I am mistaken please correct me.

Another problem with such a container is dimensionality. I am not sure how this container may be used instead of a multi-dimensional array for example.

Do you know how to implement the C++ vector type in C? This is will be higly appreciated as well.

I really appreciate your suggestions and the time it took you to make your reply. I hope you give me more details about the implementation of va_arg.

Thank you.
 
  


Reply

Tags
programming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me ... explain parameters of cacheflush() function minhstone Red Hat 2 01-22-2008 09:41 PM
C++: How to have predefined function parameters in source and header files... RHLinuxGUY Programming 22 03-12-2007 10:14 AM
JS: assigning a function call with parameters to an event eantoranz Programming 1 10-09-2006 03:05 PM
C programming: Variable number of parameters to function kenneho Programming 8 03-21-2006 06:21 AM
Question about function with variable number of parameters xailer Programming 4 01-13-2004 11:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration