L
L
Lesage2021-05-05 18:53:43
Computers
Lesage, 2021-05-05 18:53:43

How is data stored in computer memory?

I'm just starting to learn programming and it's not entirely clear to me how data (for example, char or int) is stored in computer memory.
I'm not talking about two's complement or how a number is represented as 0's and 1's. What I'm interested in is exactly how it's stored at the bit level.
Let's say we have the number 8 (let it be signed char) which will be represented as binary code as 0 0001 000 (if I'm not mistaken)
The number -8 should be represented as either 1 0001 000 or as two's complement code as 1 111 1000. My first question is related to this, because in the training materials everyone deals with some kind of ephemeral concepts of "more convenient" and "often used". Does the x86 architecture store negative numbers in two's complement or in normal?

The second question is related to writing (and, accordingly, reading) numbers to memory. I'm familiar with pointers, but all the resources say they "refer to a memory location". What is a cell, bit or byte? If a bit, does this mean that, for example, allocating memory for a dynamic array, and creating an array that occupies from 7 to 39 bits, then bits 0-6 will remain unused (after all, we do not have data types that are less than 8 bits)? In my view, with such work with memory, when cleared and written over again, it will inevitably become more and more like Swiss cheese with sections that are not used in any way.
If a memory cell is a byte, then how is a number written to it? Sequential from 0 to 7th?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vasily Bannikov, 2021-05-05
@vabka

Does the x86 architecture store negative numbers in two's complement or in normal?

Everywhere in the additional code, because in this form it is more convenient to perform arithmetic operations.
If the memory cell is a byte, then how is the number written to it? Sequential from 0 to 7th?

Both options are possible depending on the architecture. Read about LE and BE
If a bit, does this mean that, for example, allocating memory for a dynamic array, and creating an array that occupies from 7 to 39 bits, then bits 0-6 will remain unused (after all, we do not have data types that are less than 8 bits)? In my view, with such work with memory, when cleared and written over again, it will inevitably become more and more like Swiss cheese with areas that are not used in any way

The problem of memory fragmentation is solved with the help of all sorts of tricky allocators and garbage collectors.

S
Saboteur, 2021-05-05
@saboteur_kiev

Questions need to be structured, otherwise there is a stream of questions, and porridge in my head.
A memory cell is a byte.
Writing a number to a byte goes right away, but it can even be several numbers at once.
The bitness of the processor indirectly indicates how many bytes can be processed simultaneously
. Negative numbers are an abstraction for the user. From the processor's point of view, these are just bits.
In programming languages, you can specify a signed or unsigned variable, which will affect how the code is compiled and how it is used.
For example, a signed shortint is a byte, with values ​​from 0 to 255
; an unsigned shortint is a byte, with values ​​from -127 to +127 (something like this, maybe up to +128, you need to look at the wiki)
As for the Swiss cheese - don't waste your time, a lot of memory always remains unused in modern computers. For example, memory is allocated in pages of 64 KB, and if a process does not use them all, it still cannot be used by another process.

C
CityCat4, 2021-05-05
@CityCat4

What is a cell, bit or byte?

Byte
Sequential from 0 to 7th?

There is LE (Little Endian) architecture and there is BE (Big Endian). In the first, significant bytes are stored "from left to right", that is, vice versa in the usual order, in the second - just in order

H
hint000, 2021-05-06
@hint000

and creating an array that takes from 7 to 39 bits, then bits 0-6 will remain unused (after all, we do not have data types that are less than 8 bits)

Since there are no data types smaller than a byte, it is impossible to create an array that occupies individual bits. The size of the array will be equal to an integer number of bytes. The processor does not have the means to work with individual bits. Including, cyclic shift and logical operations work with a byte or a word. And if the programmer is impatient to work with bits, then a crutch is used in the form of logical operations with a mask.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question