Page 1 of 1
sizeof operator's return type
Posted: Fri Mar 09, 2012 12:40 pm
by Civillian
To my knowledge, (in C) the sizeof operator will return a size_t value.
But what happens if I use a custom typedef for size_t, or if I #define it as a macro?
Will the compiler automagically use my own type for the sizeof operator?
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 1:26 pm
by bluemoon
The sizeof will return the original size_t type since keywords are wired logic they don't affect by programmer's code.
A simple experiment show the proof:
Code: Select all
int main() {
typedef char size_t;
size_t a = sizeof(size_t);
printf ("size_t = %d, %d\n", a, sizeof(a) );
return 0;
}
gcc gives warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long unsigned int’
This suggest sizeof is unaffected by my typedef and is still 64-bit.
However, as soon as you cast the sizeof returned value to your own size_t you doing a casting there.
Bottom line, by redefining size_t you are going to shoot your own feet, and the compiler may or may not warn you for type conflict.
Anyway, don't do it. You should always use <stdint.h>
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 2:26 pm
by Civillian
... but I am using stdint.h. My own. That's where my dilemma arose from.
Edit: I'm sorry if I posted in the wrong subforum, in which case please move this thread.
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 2:54 pm
by Solar
Actually, this is a good way to check if your compiler and your library agree on the size of size_t.
You either have to adapt your library to your compiler, or configure the compiler so that it uses your desired size for your platform. Unless you really really
really know what you're doing, I recommend the former.
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 3:32 pm
by Civillian
Compiling with -nostdinc -nostdlib -m32, and linking with -nostdlib, the size of size_t is fixed at 32 bits, disobeying my typedef.
I'm not surprised that the operator spits out a 32-bit int, but then why does size_t act as if it's a builtin type, when it isn't?
I'd like to pursue your latter suggestion Solar, if possible. Have you anything to add to it?
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 3:47 pm
by Solar
Well, size_t is something where the compiler and the library have to meet halfway. sizeof() is an operator, i.e. the return type is hardcoded into the compiler - which doesn't know anything about what you wrote in your library headers. But in order to
handle the type correctly, the library must declare it to be whatever the compiler uses.
(And that's most likely
unsigned long, not
int.)
As for changing the type... that's part of what I meant when I said, "only do this if you really know what you're doing". You would have to create a new config for a new compiler target. That's a somewhat involved operation, and chances are that you want to achieve the wrong thing.
Let me put it differently: What makes you think that the compiler's choice for the type of size_t is
wrong?
If you
really want to do this, check out "GCC Internals", section "Machine Description" - but don't expect any help from me or say that I didn't warn you.
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 4:10 pm
by qw
Re: sizeof operator's return type
Posted: Fri Mar 09, 2012 4:40 pm
by Civillian
Solar wrote:What makes you think that the compiler's choice for the type of size_t is wrong?
Good point. I'll backpedal this time. At least I'm no longer confused, so thanks.
Hobbes... that's nice. But I don't think it comes with similar macros for
size_t's limits, to be put in
limits.h?
Re: sizeof operator's return type
Posted: Sat Mar 10, 2012 1:44 am
by Solar
Civillian wrote:Hobbes... that's nice. But I don't think it comes with similar macros for size_t's limits, to be put in limits.h?
Not in a compiler-independent way, no.
But if you
are targeting GCC specifically, be informed that it already provides all headers from the freestanding environment on its own, including <stdint.h>, <limits.h>, <float.h> etc.
Re: sizeof operator's return type
Posted: Sat Mar 10, 2012 4:26 am
by bluemoon
This can demonstrate the problem if you redefine the size_t to something smaller than the internal
Code: Select all
> cat a.c
#include <stdio.h>
int main() {
typedef char size_t;
char foo[4096+16];
size_t a = sizeof(foo);
printf ("size of foo = %d, %ld\n", a, sizeof(foo) );
return 0;
}
> gcc a.c -o a
a.c: In function ‘main’:
a.c:6: warning: overflow in implicit constant conversion
> ./a
size of foo = 16, 4112
Note that gcc may not give you overflow warning if the size somehow fits into
size_t a, however this will bite you later when you expand the size of
foo later.
Re: sizeof operator's return type
Posted: Mon Mar 12, 2012 8:49 am
by qw
Code: Select all
#define SIZE_MIN (-__SIZE_MAX__ - 1)
#define SIZE_MAX __SIZE_MAX__
Re: sizeof operator's return type
Posted: Mon Mar 12, 2012 8:51 am
by Solar
@ Hobbes:
You are aware that size_t is an unsigned type?
Re: sizeof operator's return type
Posted: Mon Mar 12, 2012 1:10 pm
by Civillian
I guess I didn't search hard enough, thanks Hobbes.
The true goldmine is here:
http://gcc.gnu.org/onlinedocs/cpp/Commo ... acros.html
Solar wrote:But if you are targeting GCC specifically, be informed that it already provides all headers from the freestanding environment on its own, including <stdint.h>, <limits.h>, <float.h> etc.
What exactly do you mean by this? I already have custom versions of the first two.
Re: sizeof operator's return type
Posted: Mon Mar 12, 2012 11:49 pm
by Solar
Look into ${PREFIX}/lib/gcc/${TARGET}/${VERSION}/include. Since the freestanding environmen does not include any executable code, but only definitions and macros depending on the compiler / target machine anyway, GCC provides these "for free".
Re: sizeof operator's return type
Posted: Tue Mar 13, 2012 3:20 am
by qw
Solar wrote:@ Hobbes:
You are aware that size_t is an unsigned type?
Dang! Don't know what hit me when I wrote that
Civillian, you're welcome.