Checking the type of a variable with equality operator

S

sathya_me

Dear clc,
I have a variable void *a; Since variable "a" can be assigned (point
to) any type and also
any type can be assigned to "a" (i.e means "a" = any typed variable;
any typed variable = "a".
Considering the above I have a function, which is declared and defined
to take any type
of parameter with void*
return-type foo (void *a);
In the processes of assignment of value to the variable "a" I want to
check the *type*
of variable "a" at each of assignment with the equality operator.
Like
if(a == INT_TYPE)
some action
if(a == FLOAT_TYPE)
some action

I thought of using sizeof(type). But it varies according to the
system.
<limits.h> #define values are defined to make a range for a variable
but
it does not checks the type. Those macro constants are used only
after we
declare a variable's type.

How can I do this?
Am I missing something basic?
Please explain.
Thanks

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
J

Jens.Toerring

sathya_me said:
Dear clc,
u> I have a variable void *a; Since variable "a" can be assigned (point
to) any type and also
any type can be assigned to "a" (i.e means "a" = any typed variable;
any typed variable = "a".
Considering the above I have a function, which is declared and defined
to take any type
of parameter with void*
return-type foo (void *a);
In the processes of assignment of value to the variable "a" I want to
check the *type*
of variable "a" at each of assignment with the equality operator.
Like
if(a == INT_TYPE)
some action
if(a == FLOAT_TYPE)
some action
How can I do this?

The answer is simple - you can't. When you pass a void pointer to
the function you have _no_ information about the type of what the
pointer you have cast to void is pointing to (it doesn't even have
to point to anything that may have had a type - if it's a pointer
you got from malloc() it's just a pointer to some memory that can
be used to store data of arbitrary type). That's also why you can't
dereference a void pointer or do pointer arithmetic with void
pointers - both that would require type information.
Am I missing something basic?

The basic problem is that type information is a compile time only
concept. Once the program has been compiled there's nothing left
of this information, a pointer is just a variable that can hold an
address with no type information attached to it. The type informa-
tion has been used by the compiler to pick the correct machine
instructions when the value of what the pointer points to is used,
to do type checking and to determine by how many bytes a pointer
has to be changed when it e.g. gets incremented.

If you need type information within the program at run time all you
can do is to associate another variable with the pointer that tells
you what type it is supposed to point to, e.g. you could have a
structure

struct void_pointer_with_type_information {
void *a;
int type_of_a;
};

where 'type_of_a' gets set to a unique value describing the type
of the variable 'a' is pointing to and then work with that instead
of 'naked' void pointers.
Regards, Jens
 
S

sathya_me

u> I have a variable void *a; Since variable "a" can be assigned (point


The answer is simple - you can't. When you pass a void pointer to
the function you have _no_ information about the type of what the
pointer you have cast to void is pointing to (it doesn't even have
to point to anything that may have had a type - if it's a pointer
you got from malloc() it's just a pointer to some memory that can
be used to store data of arbitrary type). That's also why you can't
dereference a void pointer or do pointer arithmetic with void
pointers - both that would require type information.

Thanks for the information which I have read in my C reference. But
my mind
got stilled when I thought about the idea of using the == to
determine the type
of a variable.

The basic problem is that type information is a compile time only
concept. Once the program has been compiled there's nothing left
of this information, a pointer is just a variable that can hold an
address with no type information attached to it. The type informa-
tion has been used by the compiler to pick the correct machine
instructions when the value of what the pointer points to is used,
to do type checking and to determine by how many bytes a pointer
has to be changed when it e.g. gets incremented.

Thanks again
If you need type information within the program at run time all you
can do is to associate another variable with the pointer that tells
you what type it is supposed to point to, e.g. you could have a
structure

struct void_pointer_with_type_information {
void *a;
int type_of_a;
};

where 'type_of_a' gets set to a unique value describing the type
of the variable 'a' is pointing to and then work with that instead
of 'naked' void pointers.
Regards, Jens

To be more precise:
After going through some of the old thread of clc under the topic
"overflow / underflow"
I thought of writing a function which could tackle all possible
overflow / underflow
with multiplication, division , divide by zero , giving more
(less)value to the input,
etc.(just a try from a beginner).
So the function declaration goes like this
void overflow_action (void *type , unsigned action)
if(type == int_type)
check *limit* for the variable by using #define constants from
<limits.h>;
action for overflow;
if(type == float_type)
check for limit
action for overflow;
All the same for other types too.
But I know it is different to deal with
multiplication (ie a = b * c where a > limit),
divide by zero error etc.

*I come to know that the above idea can not be worked (thanks to
Jens)*

Is there any pointer to the above (dealing with exception in
standard C)
in the net where people has done some works.




--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
J

Jens.Toerring

sathya_me said:
To be more precise:
After going through some of the old thread of clc under the topic
"overflow / underflow"
I thought of writing a function which could tackle all possible
overflow / underflow
with multiplication, division , divide by zero , giving more
(less)value to the input,
etc.(just a try from a beginner).
So the function declaration goes like this
void overflow_action (void *type , unsigned action)
if(type == int_type)
check *limit* for the variable by using #define constants from
<limits.h>;
action for overflow;
if(type == float_type)
check for limit
action for overflow;
All the same for other types too.
But I know it is different to deal with
multiplication (ie a = b * c where a > limit),
divide by zero error etc.

In what kind of situation would this function get called and how?
It looks a bit as if you're proposing some kind of "signaling"
mechanism for overflows and your function would be the "signal"
handler, but then the question is how the function would get
invoked at all (there's nothing that would emit such a "signal"
or would invoke suca function under these conditions - at least
nothing portable I am aware of). Or do you plan to write a "safe"
replacement for all arithmetic operations (and perhaps all math
functions) that would check the arguments before the operation
is actually done - but then the functions signature doesn't look
as if that could be done with it. Sorry, I am confused about your
intentions, could you try to explain it a bit less concise? An
example can sometimes be rather helpful;-)
Is there any pointer to the above (dealing with exception in
standard C)

That would hint at your looking for some "signaling" mechanism
for overflows etc. But I fear that there's no such "exception"
mechanism (especially not in the C++ sense) to start with - unless
you mean having errno set to EDOM or ERANGE for mathematical
functions under certain circumstances (but even if this is happens
is implementation-defined as far as I can see) and maybe on plat-
forms that support it having a a SIGFPE signal raised on certain
conditions for floating point operations (i.e. IEEE 754 exceptions).

Regards, Jens
 
S

sathya_me

In what kind of situation would this function get called and how?
It looks a bit as if you're proposing some kind of "signaling"
mechanism for overflows and your function would be the "signal"
handler, but then the question is how the function would get
invoked at all (there's nothing that would emit such a "signal"
or would invoke suca function under these conditions - at least
nothing portable I am aware of).

[Helping explanation sniped]

Sir,going too depth to understand for a beginner. I am just to
define a function
that will explain all the possible use of macros in <limits.h>. Just
a program
for practicing. Any links? Thanks


--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
J

Jens.Toerring

sathya_me said:
Sir,going too depth to understand for a beginner. I am just to

No reason to "Sir" me - I've never met the British queen (and don't
expect to;-)
define a function
that will explain all the possible use of macros in <limits.h>. Just
a program
for practicing. Any links? Thanks

Sorry, now you rather lost me completely. The only thing I can think of
at the moment is something like the following program:

---------------8<------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

enum Types {
SCHAR,
UCHAR,
CHAR,
SHRT,
USHRT,
INT,
UINT,
LONG,
ULONG
};

void print_limits( enum Types type )
{
if ( type == SCHAR )
printf( "Range of 'signed char': %d - %d (using %d bits)\n",
SCHAR_MIN, SCHAR_MAX, CHAR_BIT );
else if ( type == UCHAR )
printf( "Range of 'unsigned char': 0 - %d (using %d bits)\n",
UCHAR_MAX, CHAR_BIT );
else if ( type == CHAR )
printf( "Range of 'char': %d - %d (using %d bits)\n",
CHAR_MIN, CHAR_MAX, CHAR_BIT );
else if ( type == SHRT )
printf( "Range of 'short int': %d - %d\n", SHRT_MIN, SHRT_MAX );
else if ( type == USHRT )
printf( "Range of 'unsigned short int': 0 - %d\n", USHRT_MAX );
else if ( type == INT )
printf( "Range of 'int': %d - %d\n", INT_MIN, INT_MAX );
else if ( type == UINT )
printf( "Range of 'unsigned int': 0 - %u\n", UINT_MAX );
else if ( type == LONG )
printf( "Range of 'long int': %ld - %ld\n", LONG_MIN, LONG_MAX );
else if ( type == ULONG )
printf( "Range of 'unsigned long int': 0 - %lu\n", ULONG_MAX );
else
fprintf( stderr, "Unknown type passed to function\n" );
}

int main( void )
{
enum Types i;

for ( i = SCHAR; i <= ULONG; i++ )
print_limits( i );

return EXIT_SUCCESS;
}

---------------8<------------------------------------------------

But I don't know if that resembles anything you're interested in.

Regards, Jens
 
J

Joona I Palaste

(e-mail address removed)-berlin.de scribbled the following:
No reason to "Sir" me - I've never met the British queen (and don't
expect to;-)

*I* expect to. How exactly is this done?
 
J

Jens.Toerring

Joona I Palaste said:
(e-mail address removed)-berlin.de scribbled the following:
*I* expect to. How exactly is this done?

I don't really know. Maybe you just call her? The only thing I am rather
sure about is that you are not supposed to call her "Lizzy" the first
time you meet her (and be very afraid of the Corgis;-)

My best wishes to the Finish king, Jens
 
J

Joona I Palaste

(e-mail address removed)-berlin.de scribbled the following:
I don't really know. Maybe you just call her? The only thing I am rather
sure about is that you are not supposed to call her "Lizzy" the first
time you meet her (and be very afraid of the Corgis;-)

Would you happen to know her telephone number? =)
My best wishes to the Finish king, Jens

There *is* no Finnish king. We're a republic, and our current president
is Tarja Halonen, so she can't exactly be called a "king" of any
country.
 
E

Emmanuel Delahaye

void print_limits( enum Types type )
{
if ( type == SCHAR )
printf( "Range of 'signed char': %d - %d (using %d bits)\n",
SCHAR_MIN, SCHAR_MAX, CHAR_BIT );
else if ( type == UCHAR )

What about a switch-case ?
 
S

sathya_me

No reason to "Sir" me - I've never met the British queen (and don't
expect to;-)

We (in India) call experienced persons as sir.
I have to be careful with cross cultured group.
Sorry, now you rather lost me completely. The only thing I can think of
at the moment is something like the following program:

---------------8<------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

enum Types {
SCHAR,
UCHAR,
CHAR,
SHRT,
USHRT,
INT,
UINT,
LONG,
ULONG
};

void print_limits( enum Types type )
{
if ( type == SCHAR )
printf( "Range of 'signed char': %d - %d (using %d bits)\n",
SCHAR_MIN, SCHAR_MAX, CHAR_BIT );
else if ( type == UCHAR )
printf( "Range of 'unsigned char': 0 - %d (using %d bits)\n",
UCHAR_MAX, CHAR_BIT );
else if ( type == CHAR )
printf( "Range of 'char': %d - %d (using %d bits)\n",
CHAR_MIN, CHAR_MAX, CHAR_BIT );
else if ( type == SHRT )
printf( "Range of 'short int': %d - %d\n", SHRT_MIN, SHRT_MAX );
else if ( type == USHRT )
printf( "Range of 'unsigned short int': 0 - %d\n", USHRT_MAX );
else if ( type == INT )
printf( "Range of 'int': %d - %d\n", INT_MIN, INT_MAX );
else if ( type == UINT )
printf( "Range of 'unsigned int': 0 - %u\n", UINT_MAX );
else if ( type == LONG )
printf( "Range of 'long int': %ld - %ld\n", LONG_MIN, LONG_MAX );
else if ( type == ULONG )
printf( "Range of 'unsigned long int': 0 - %lu\n", ULONG_MAX );
else
fprintf( stderr, "Unknown type passed to function\n" );
}

int main( void )
{
enum Types i;

for ( i = SCHAR; i <= ULONG; i++ )
print_limits( i );

return EXIT_SUCCESS;
}

---------------8<------------------------------------------------

But I don't know if that resembles anything you're interested in.

Thanks for the code. I just downloaded Kazlib which has excecp.c that
deals with ANSI way of exception handling in C. I am going through that.


--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
J

Jens.Toerring

sathya_me said:
Thanks for the code. I just downloaded Kazlib which has excecp.c that
deals with ANSI way of exception handling in C. I am going through that.

Ok, that was what you looking for, something like C++ exceptions
for C. But be aware that no exceptions get thrown automatically
for e.g. overflows, devision by zero etc. It's just meant for cases
where you throw an exception explicitely in your code. And another
point you have to remember is that the use of setjmp/longjmp, which
get used in the kazlib, might lead to some strange effects under
some conditions: Since setjmp() isn't guaranteed to store the
contents of all CPU registers it might happen that variables that
the compiler put into processor registers (which is quit common
when you switch on optimization) may have become "clobbered" after
throwing an exception, i.e. they may have a different value from
what they had before the try block started. I would recommend
that you set your compiler to a high warning level - it might
give you some hints about such possible problems and in that
case you will have to declare the affected variables as volatile
(or use some other method that guarantees that the compiler won't
put them into CPU registers).
Regards, Jens
 
M

Mark McIntyre

(e-mail address removed)-berlin.de scribbled the following:


*I* expect to. How exactly is this done?

If you're not fussy about *which* British queen, walk into any pub in Soho
on a friday night.... (g,d&r).
 
C

CBFalconer

Ok, that was what you looking for, something like C++ exceptions
for C. But be aware that no exceptions get thrown automatically
for e.g. overflows, devision by zero etc. It's just meant for cases
.... snip ...

They may very well be, on the better hardware. Not PCs. It is a
quite allowable form of undefined behavior.
 
J

Jens.Toerring

CBFalconer said:
... snip ...
They may very well be, on the better hardware. Not PCs. It is a
quite allowable form of undefined behavior.

I didn't made myself clear enough - I meant "exceptions won't get
thrown automatically for e.g. overflows etc." by the "kazlib" (by
Kaz Kylheku), which the OP said she downloaded - at least a quick
glance at the code didn't show me anything that would do that. Of
course, if your hardware signals such conditions I guess you could
throw that kind of exception from the handler for the condition.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top