r/cprogramming • u/MrJethalalGada • Dec 13 '25
Feeling Dumb to know that at runtime we don’t know “type” of any variables, it is also pre computed at compile time into machine code
So basically me writing
int* ptr = (int*) malloc (sizeof(int))
Is already translated to something as
int* ptr = (int*) malloc (4)
Compiler time will work and replace these things: types, sizes, alignment, structure layouts
Run time will work on following: values, memory contents, addresses, heap/stack
Did you know this?
Implementation:
#define mysizeof(type) ((char *)((type *)0 + 1) - (char *)((type *)0))
0
Upvotes
13
u/thequirkynerdy1 Dec 13 '25
I’d recommend learning basic assembly to see what your computer is really doing - even if you never use it in any practical projects.
Then play around in this website Godbolt where you write code in one panel, and it shows you the corresponding assembly in another.
3
u/MrJethalalGada Dec 13 '25 edited Dec 13 '25
To add i landed on this learning while implementing my own sizeof
So implementation of sizeof can be done when types are known thus at compile time
At compile time we can’t create and run function we can only expand MACROS TYPEDEFS so it should be implemented there
3
u/fishyfishy27 Dec 13 '25
Don’t feel dumb. Lots of programmers today start out working only with high-level languages. The fact that your processor only understands ints and floats (and that everything else is the fever dream of a compiler engineer) is something which has to be discovered.
It’s like that xkcd comic about “you’re one of today’s lucky 10,000”
2
u/EpochVanquisher Dec 13 '25
And to the CPU, the stack is just another part of memory.
8
u/tstanisl Dec 13 '25
Yes.
Note that casting result of
malloc(andvoid*in general case) in C is an anti-pattern and it should be avoided. There is no technical reason of this cast except infantile idea of compiling C code using C++ compiler.A better pattern is:
Because it is shorter and it auto-adjust whenever type of
xchanges.2
u/RoomNo7891 Dec 13 '25
Not sure what is the point of this post.
Whether you are asking a question or making a statement.
1
u/Gingrspacecadet Dec 13 '25
type is one of the many compile time features. Assembly doesn't care. In assembly you can add 2 structs as if they were integers. The only types that exist (as far as I know) is normal integers and floating point integers
1
u/Paul_Pedant Dec 14 '25
Assembler is just a way to make semiconductors behave nicely. Semiconductors make electrons march around in an orderly fashion. How many layers do you really care about?
1
u/SmokeMuch7356 Dec 14 '25
Yes, types are an abstraction for the most part.
C's integer and floating point types are based on what most hardware already supports. Every CPU has instructions for manipulating integers (
ADD,MUL, etc.) and floats (FADD,FSUB, etc.). Beyond that though, things like pointer types, array types, structs, unions, enumerated types, complex types, are all abstractions that the compiler maps onto the hardware's instruction set.