>>1
I assume you only want to check pointers to memory (anything else seems stupid to me), in this case you can store the type of the memory in an extra byte:
int* pi = malloc(sizeof(int) + 1);
char* pc = pi;
*pc = 'i'; /* i for int */
Then just increment the pointer by one byte when accessing it (I'd use a temporary variable for this).
int* tpi = (((char*)pi)+1);
Name:
Anonymous2008-03-19 18:13
>>8
Hello and welcome to Bus Error. lern2alignment
Name:
Anonymous2008-03-19 18:27
>>1
Just cast it to an int and check if it's a valid value, e.g. for a pointer:
if (INT_MIN <= *(*int)foo <= INT_MAX) {
/* *foo points to an int */
}
Your coding style is horrible, by the way. You really should see an orthodontist about those misplaced braces.
Name:
Anonymous2008-03-19 18:29
>>10
I normally code K&R style, but /prog/ doesn't see the difference since the faggets moved in from /b/, so I didn't bother.
Could you give me some more background on what exactly your code does? I don't like to use something unless I fully understand it.
Name:
Anonymous2008-03-19 18:31
Welcome to GNU
Name:
lol!8mQB/2odm62008-03-19 18:32
>>4,7,12
You are not programmers, stop trying to fit in.
>>30
exactly, checking what type a variable is can be done in perl, php or any other scripted language but in c it's quite useless as the types cannot change, you can have void pointers and cast them but technically that's not checking what type they are
Simply, you cannot do it.
sizeof won't work, because sizeof (short) might be 1, or sizeof (float) == sizeof (long).
It's simply impossible just with the size.
Also, sizeof (size_t) might be 4 in your system but 128 in others.
one thing you can do if you know you work with integers and pointers only is to use intmax_t, but I'm not sure if that is what you were looking for.
Name:
Anonymous2008-03-20 2:51
A signed long can hold all the values between LONG_MIN and LONG_MAX inclusive. LONG_MIN is required to be -2147483647 or less, LONG_MAX must be at least 2147483647. Again, many 2's complement implementations will define LONG_MIN to be -2147483648 but this is not required.
An unsigned long can hold all the values between 0 and ULONG_MAX inclusive. ULONG_MAX must be at least 4294967295. The long types must contain at least 32 bits to hold the required range of values.
Stop trying to do dynamic typing in C, the language wasn't meant to work that way and I'm sure you can find a better solution to your problem that doesn't need it.
>>25
Thanks, fixed it so it doesn't segfault: char*
allocate_type(type, length)
char type;
unsigned int length;
{
int* ret = malloc(length);
*(ret - 3) = type; /* block size is stored at -2 and -1 */
return ret;
}