sizeof operator's return type
sizeof operator's return type
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?
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
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:
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>
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;
}
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
... 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.
Edit: I'm sorry if I posted in the wrong subforum, in which case please move this thread.
Re: sizeof operator's return type
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.
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.
Every good solution is obvious once you've found it.
Re: sizeof operator's return type
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?
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
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.
(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.
Every good solution is obvious once you've found it.
Re: sizeof operator's return type
Code: Select all
typedef __SIZE_TYPE__ size_t;
Re: sizeof operator's return type
Good point. I'll backpedal this time. At least I'm no longer confused, so thanks.Solar wrote:What makes you think that the compiler's choice for the type of size_t is wrong?
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
Not in a compiler-independent way, no.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?
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.
Every good solution is obvious once you've found it.
Re: sizeof operator's return type
This can demonstrate the problem if you redefine the size_t to something smaller than the internal
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.
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
Re: sizeof operator's return type
Code: Select all
#define SIZE_MIN (-__SIZE_MAX__ - 1)
#define SIZE_MAX __SIZE_MAX__
Re: sizeof operator's return type
@ Hobbes:
You are aware that size_t is an unsigned type?
You are aware that size_t is an unsigned type?
Every good solution is obvious once you've found it.
Re: sizeof operator's return type
I guess I didn't search hard enough, thanks Hobbes.
The true goldmine is here: http://gcc.gnu.org/onlinedocs/cpp/Commo ... acros.html
The true goldmine is here: http://gcc.gnu.org/onlinedocs/cpp/Commo ... acros.html
What exactly do you mean by this? I already have custom versions of the first two.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.
Re: sizeof operator's return type
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".
Every good solution is obvious once you've found it.
Re: sizeof operator's return type
Dang! Don't know what hit me when I wrote thatSolar wrote:@ Hobbes:
You are aware that size_t is an unsigned type?
Civillian, you're welcome.