Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science Introduction to Data Structures Exploring Arrays Accessing a Value in an Array

How does the computer find a 'location' of memory?

For example when a pointer contains a value for a location in memory. How does the computer find that location? Are values in memory labeled with a location value that itself takes up memory?

2 Answers

Eric M
Eric M
11,515 Points

Hi okilydokily,

It depends what you mean by "computer" here. There are several levels of abstraction between the silicone and our code. Even a low level language like C deals with the C machine model, rather than the machine itself. Still we say C is closer to the silicone (or in common parliance, "closer to the metal") than a language like JavaScript because it still deals with memory "manually".

In any programming language, even assembly and C, this memory allocation is handled through a virtual memory abstraction. This is something that the operating system provides to a running process, so that process does not have to worry about physical memory addressing.

In essence the program communiates with the opearting system, which communicates with the device drivers, which communicate with the hardware itself. So our allocation to a memory address is translated multiple times to actually become an electrical signal with a high voltage (1) or a low voltage (0) in the physical world.

So part of the answer to your question is, yes, the computer does store information about it's available memory address space, but it doesn't need to store each avialable address. So long as it knows what addresses are used and what are free it can give programs a range to access.

You will also want to read a little about the difference between stack memory and heap memory. This is another level of abstraction in the memory access provided to your program by the operating system.

This is all pretty complicated, and due to the complication a lot of security exploits occur when malware tricks a program or operating system into allowing it to access memory that it shouldn't have access to. That's why there is movement to use "memory safe langauges" that prevent programmers from writing code that interacts with the computer's memory in a way that could be exploited or lead to avoidable crashes.

Keep in mind that computer memory is a whole topic unto itself that spans electrical and software engineering. I hope my answer helps a little but no doubt you still have a lot of questions! There's some good information online about this, and a lot of people smarter than me have written some pretty in depth answers on sites like stack overflow. Even wikipedia has good resources on better understanding memory.

Cheers and good luck on your journey,

Eric

senay yakut
senay yakut
4,124 Points

Different programming languages stores data differently in computer memory. For example in JavaScript, if you have an array data structure which has a length of 6 (var arr =[0,1,2,3,4,5];) and since arrays in javascript can be grow and if(arr[9]=11); so now my arr has a length of 10 and it shows even if you didnt have any values between 6 and 10, it still reserves the memory for the up coming data. this might be a little bit memory consumption sometimes.