danger/u/
This thread is permanently archived
noob coding ask: how does Short work?

| is it possible for one variable to take half a memory address? or is the benefit purely long term-storage-wise?


| >>688436
Short is just a data type representing an integer; a short is, as the name says, a 'short' int. A short is always guaranteed to be smaller or equal to (but never bigger) in size than an int (in 32-bit compilers, short is 16 bits and int is 32 bits — but in 16-bit compilers both are the same size).

Knowing this, I don't know how it relates to your question.


| >>688437 so a short is a purely logical concept? does it have an advantage over int other than overflow/bitwise operations?


| >>688442
Most integer representations are. To answer this, I raise the question: what differentiates a char from a long aside from its size?

Short is in a weird zone wherein an int is oftentimes enough. There is no real advantage (aside from micro-optimisation).

Integer types in C is just that: numbers. What differs is how much they represent.


| >>688436
yes it is possible for a variable to take half of a memory address!
in a 32 bit cpu, the bus width is 32 bit. this means memory addresses are like a 32 bit number. each memory address points to a single byte in memory. if we want to read memory address x as a char, we read address x. if we want to read mem address x as SHORT, we read address x and the next adress x+1 because a short is 2 bytes! if we want an int we read from x to x+4, etc.


| >>689045
cpus usually have instrucions for reading a memory address as different data types.
this info is simmplified but i think it was cool to have the hardware point of view too.


| >>689045
I don't it's correct to say a variable can take "half a memory address"? After all, a short isn't half a char (which is the smallest addressable type). So, assuming that char is the smallest in C, I don't think you can go to a 1/2bytes length (4 bites).

Otherwise I agree with the (albeit oversimplified) look. To OP, I recommend learning about pointers in general. They will give you a good insight to this subject.


| >>689049
it makes sense in my head :) i think you are talking about the content of a memory adress and i'm referring to just the adress (like when you do &var in c). this address would be 32 bit and the short would be 16 bit.

pointers are cool and it's worth learning about them. and there's also super secret stuff like pointers to functions.


Total number of posts: 8, last modified on: Mon Jan 1 00:00:00 1597531674

This thread is permanently archived