[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Getting the callers environment in c
- From: Patrick Rapin <toupie300@...>
- Date: Wed, 9 Mar 2011 21:45:24 +0100
> Btw, I keep this code around for a quick check of machine sizes, in
> case anybody else finds it useful:
Interesting code, although not directly to related to Lua.
However, I think it could be completed, as below.
I have added the following sizes:
- various enum types (which could be of different sizes depending on
the compiler options, see for example -fshort-enums on GCC)
- an empty struct (on GCC it is 0 for C but 1 for C++!)
- a bad aligned struct (typically 16, but can be 13 if structures are packed)
And if compiled in C++, the following sizes are also output:
- bool native type (usually 1)
- an empty class with just one virtual function (normally the same
size as a pointer)
- a pointer to a member function. Hey, look: this is different to the
size of a regular function pointer !
C++ in fact forbids cast from a regular pointer to a member
selector (or the other way).
My conclusion is, you can nearly always assume the size of a data
pointer is the same as a function pointer, but you cannot take the
same assumption for pointer to class members in C++.
_______________
#include<inttypes.h>
#include<stdio.h>
#include<stddef.h>
#include<stdlib.h>
#define SZ(x) printf("%2zd bytes " #x "%s\n", sizeof(x), (0 > (x) -1)
? "" : " (unsigned)")
#define SZ2(x) printf("%2zd bytes " #x "\n", sizeof(x))
typedef enum { spe1 = 0xFF } small_positive_enum;
typedef enum { mpe1=0xFFFF } medium_positive_enum;
typedef enum { bpe1 = 0xFFFFFFFF } big_positive_enum;
typedef enum { sne1 = -0x80 } small_negative_enum;
typedef enum { mne1=-0x8000 } medium_negative_enum;
typedef enum { bne1 = -0x80000000 } big_negative_enum;
typedef void function(void);
typedef struct {} empty_struct;
typedef struct { char c; int s; double i; } sparse_struct;
#ifdef __cplusplus
class empty_abstract_class { virtual int member(); };
typedef void (empty_struct::*member_function)();
#endif
int main()
{
SZ(void*);
SZ(function*);
SZ(char);
SZ(short);
SZ(int);
SZ(long);
SZ(long long);
SZ(float);
SZ(double);
SZ(long double);
SZ(time_t);
SZ(suseconds_t);
SZ(pid_t);
SZ(wchar_t);
SZ(size_t);
SZ(ptrdiff_t);
SZ(ssize_t);
SZ(intmax_t);
SZ(uintmax_t);
SZ(small_positive_enum);
SZ(medium_positive_enum);
SZ(big_positive_enum);
SZ(small_negative_enum);
SZ(medium_negative_enum);
SZ(big_negative_enum);
SZ2(empty_struct);
SZ2(sparse_struct);
#ifdef __cplusplus
SZ(bool);
SZ2(empty_abstract_class);
SZ2(member_function);
#endif
return 0;
}