0

I am a beginner in Computer Science and I started learning the C language. (So, I apologize at first if my question doesn't make any sense) I am facing a doubt in the chapter called Pointers.

I am thinking that when we declare a variable, let's say an integer variable i.

A memory is allocated for the variable in the RAM. In my book nothing was written about how the computer selects the memory address which is to be allocated for the variable. I am asking this because, I was thinking a computer has 8GB RAM and a 32-bit processor.

I learnt that a 32-bit processor consists of a 32-bit register, which can allow the processor to access atmost up to 4GB of the RAM.

So, is it possible in that computer, when we declare the integer variable i, the computer allocated a memory space with address which can't be accessed by the 32-bit processor?

And if this happens what will be shown on the screen if I want to print the address of i using the address of operator?

Jens
  • 69,818
  • 15
  • 125
  • 179
  • What makes you think memory will be allocated "out of range" ? – Raildex Jun 28 '21 at 06:46
  • Which model do you wish to buy? As MSalters told you a computer with 8 GB of RAM will have a 64 bit processor. As far as I remember last 32 bit PC processor was introduced around 2000 (maybe Pentium 4). Another thing to consider is the operating system: if is only 32 bit, it will use the processor is 32 bit mode and only 4 GB of memory can be used. – Laci Jun 28 '21 at 06:54
  • @Raildex No,I just wanted to know whether it is possible? Or the specific way in which the memory address is allocated for the variable will not allow this to happen i.e. the address will not be out of the range of the 32-bit register. – It's probable Jun 28 '21 at 06:58
  • You should also read this: What and where are the stack and heap? https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Laci Jun 28 '21 at 07:14
  • 1
    @It'sprobable if you are running 32 bit software on a 64 bit OS and the OS supports 32 bit (which is not granted btw), the OS is most likely using virtual adress space. Which means the application sees virtual memory - 4GB in a 32 bit environment and the physical 8 GB are **mapped** to these 32 bit. i.e. Even though you have 8GB, the 32 bit app only sees 4GB which are "borrowed" – Raildex Jun 28 '21 at 07:41
  • @Raildex: All major OS'es are using Virtual Address spaces, in any configuration, and have done so for decades. – MSalters Jun 28 '21 at 08:17
  • 1
    @Laci: The last 32 bit processors were _introduced_ in 2008 (Intel Atom), and systems based on them were sold a few years later. Still, it's now 2021, these cheap systems will all be gone by now. – MSalters Jun 28 '21 at 08:22
  • The number of less-than-64-bit systems is by far higher than the number of 64 bit or higher. Just leave your PC-only view... ;-) – the busybee Jun 28 '21 at 10:11
  • The number of bits of a processor just tells us how many bits the data path has (or any other marketroid's wishful thinking). This has nothing to do with the number of bits of the address bus. The first IBM-PC was an 8 bit system and had addresses with 20 bits. – the busybee Jun 28 '21 at 10:13

1 Answers1

0

A computer with 8 GB of RAM will have a 64 bit processor.

Many 64 bit CPU's can still run 32 bit programs, in which case that both use 4 GB. You will see the address &i as a 32 bit offset in your program's memory. But even if you did compile for 64 bits, you'd still see the 64 bit offset in your program - the OS will instruct the CPU how different programs use different parts of memory.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 2
    Some 32-bit x86 processors have Physical Address Extension (PAE), which allows them to use 36 bits for addressing. – DarkAtom Jun 28 '21 at 08:03
  • @DarkAtom: I believe these are rapidly going extinct now that there are 64 bit processors. – MSalters Jun 28 '21 at 08:15
  • 1
    Yes, but they do exist. And 32-bit OSes actually use their functionality (64-bit processors support it), therefore they are CPU-agnostic. They can run on any CPU that supports PAE, 32 or 64 bits. – DarkAtom Jun 28 '21 at 08:32