Since size_t
can be 32-bit or 64-bit depending on the current system, would it be best to always pass size_t to a function as a reference or const reference so it is always 4 bytes? (if it is 8 bytes you would have to make a copy) Lots of open source code I've looked at do not do this, however if their compiler supports 64-bit integers, those 64-bit integers are always passed as references. Why don't they do this for size_t? I'm wondering what is your opinion.

- 208,859
- 35
- 376
- 711

- 19,924
- 12
- 70
- 101
-
6Why do you believe that const references are 4 bytes on 64 bit systems? And what sense would it make to have 4 byte references and 64 bit size_t? – Nordic Mainframe Feb 04 '11 at 07:11
-
oh wow, I cant believe i forgot about that. Now I want to delete my question – Marlon Feb 04 '11 at 07:12
-
On what systems will `size_t` be 64-bit and a pointer be 32-bit? – Mikel Feb 04 '11 at 07:12
-
@templatetypedef, I thought that was `ptrdiff_t`? `size_t` could in principle be 32-bit on a 64-bit system, but then all memory allocation would have to, individually, be smaller than 4G. This isn't actually done, however, because your registers are 64 bits anyway, so it doesn't really buy you anything. – bdonlan Feb 04 '11 at 07:18
-
@bdonlan- I stand corrected! I was confusing the nested type size_type inside of the STL containers, which must be large enough to hold the absolute value of any difference_type, with size_t and ptrdiff_t. Thanks for bringing this to my attention! @Mikel, I retract my previous statement on the grounds that it's incorrect. – templatetypedef Feb 04 '11 at 07:26
-
Note that a reference and a value are distinct concepts (even if const). If you think you should always use references instead of values for big things then don't forget to always check about lifetime and aliasing problems. Fast but incorrect code is not that useful. See http://stackoverflow.com/questions/4705593/int-vs-const-int/4705871#4705871 – 6502 Feb 04 '11 at 07:32
5 Answers
It's customary to pass all primitive types by value because the operations necessary to copy them are typically just a single assembly instruction. Passing size_t
s by value is therefore preferable over passing size_t
s by reference.

- 362,284
- 104
- 897
- 1,065
On most implementations size_t
, pointers to objects and references to objects are exactly of the same size.
Think of it this way: size_t
can hold size of any object and you can use char*
to address any byte in any object, so it implies that size_t
and char*
must have closely related sizes. Thus your idea makes no sense on most implementations.

- 167,383
- 100
- 513
- 979
-
Note that `sizeof(char*)` could be bigger than `sizeof(size_t)`, e.g. on a segmented memory system where a single object cannot be bigger than the size of a segment but a pointer should be able to point to multiple segments. – musiphil Jul 31 '12 at 23:50
I didn't quite follow your logic. If you pass by reference then the address will be either 32-bit or 64-bit, depending on the current system.
At any rate, I see no advantage to passing it by reference.

- 65,341
- 71
- 269
- 466
-
I guess I went brain dead while asking this question. Anyways, you made me come to this realization. I think I'll just delete this question in a few minutes. Just kidding, I guess I cant. – Marlon Feb 04 '11 at 07:13
-
4Don't delete it, others may have the same question :) And anyway deletion by original poster's not possible after there are answers. – bdonlan Feb 04 '11 at 07:15
-
You can't delete it. Just as well since people have taken time to answer it. – Jonathan Wood Feb 04 '11 at 07:16
size_t
is guarenteed to be able to hold the size in bytes of any object you can allocate in memory. This usually tends to imply that it is the same size as a pointer, which in turn is typically the size of a CPU register.
Passing by reference doesn't help; a pointer is almost certainly at least as large as a size_t (if not, the size_t could be made smaller without problems). And in any case, most 64-bit ABIs pass integer arguments in 64-bit registers anyway, so there's no difference in stack footprint.

- 224,562
- 31
- 268
- 324
The problem with passing by reference is that it would require the compiler to store the value in memory and pass the address of that stored value as a reference. On a 64-bit architecture the calling conventions allow to pass much more information in registers (6 registers) without having to store values in memory, so you would inhibit optimizations by passing small values by reference.
There is more to this question, you might like to start at:
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
http://en.wikipedia.org/wiki/X86_calling_conventions#x86-64_Calling_Conventions

- 131,725
- 17
- 180
- 271