Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C why would a pointer be larger than an integer

Tags:

c

memory

embedded

I am playing around with sizeof() in GCC on a linux machine right now and I found something very surprising.

printf ("\nsize of int = %lu\n", sizeof(int));
printf ("\nsize of int* = %lu\n", sizeof(int *));

yields

size of int = 4
size of int* = 8

I thought the size of a pointer to an integer would be much smaller than the actual integer itself!

I am researching embedded software right now and I was under the understanding that passing by reference was more efficient ( in terms of power ) than passing by value.

Could someone please clarify why it is more efficient to pass by reference than by value if the size of the pointer is larger than the actual value.

Thanks!

like image 389
icedTea Avatar asked Nov 28 '22 17:11

icedTea


1 Answers

Integer can be any size the compiler writer likes, the only rules (in standard C) are: a) int isn't smaller than a short or bigger than a long, and b) int has at least 16 bit.

It's not uncommon on a 64bit platform to keep int as 32bits for compatibility.

like image 163
Martin Beckett Avatar answered Dec 01 '22 08:12

Martin Beckett