How memory works?

There’s a difference between programming C++ and programming Java in C++.

A new language is not just a new syntax. There’s lots of new patterns to learn, and some that don’t work in one language work really well in another, or vice versa.

Pretty much, the more concise way to think about it is as an offset:


// array is located at some address, but we don't care,
// we use addresses relative to the offset of the array:
int[] array = new int[10];
int a = array[4]; // we fetched the data at {array offset} + {sizeOf int} * 4

Individual variable offsets are also relative to the offset of the program they are contained in, which is in turn contained in the “virtual address space” created by the OS.
It’s offsets all the way down…

Learn C NOT C++ if you wanna learn about memory, there is too much noise in C++ that keeps you from learning the systems programming part of it. In fact you might even wanna pick up a simple assembly language/machine language for the sake of learning how all this works without all the abstraction, it will help you visualize how programs work (Don’t try picking up x86 first, there are other asm languages made with the sole intention of teaching asm)

Just try out Assembly language. It helps understanding what happens in the lowest programmable layer. I found assembly language easy to understand. It’s just really hard to write big bug-free programs with it, but the for the sake of understanding, I’d recommend. :slight_smile:

[quote=“matheus23,post:24,topic:51959”]
Isaac Asimov wrote a famous short story on this general theme in the early days of computers called “The Feeling of Power”. It’s quite short and well worth a read if you’re having a lazy Sunday … here’s the text in a web page: http://downlode.org/Etext/power.html

The moment you “get” assembler and realize all other languages, operating systems and all the rest, come down to the same simple stuff, nothing about software will ever be scary again :smiley:

The best way to learn assembly is to learn the LC3 assembly. Google it, learn it, have fun with it.

LOL do you use: Introduction to Computing Systems: From bits & gates to C & beyond? I learned LC-3 but found there is very, very little on the subject except that one book. Thats the only reason I can’t recommend LC-3 unless you also decide to buy that book (which isn’t a bad buy it is actually very informative book)

Enjoy: https://www.dropbox.com/s/n9s12aywnmtgn19/Introduction_to_computing_systems.pdf?dl=0

It’s a really great book. The language is easy to read and the subject is actually pretty fun.

I don’t really want to learn any of those languages…
My chosen language is Java and I am not changing…
I just wanted to know the answer to a question that was on my mind…
Btw, thx for answering…

No. One does not simply use one language and one language only. If you really want to learn how to program, become multilingual.

On the scale of nice languages (C++ at the bottom, something that doesn’t exist yet at the top), Java is actually pretty low. Lots of things that should be really simple to express end up taking a lot of code. It also doesn’t exactly teach the best coding practices.

It’s fine as a beginner language, but once you’re ready to handle trickier concepts, there’s no reason not to try out some more powerful languages.

* plugs Clojure for it’s small size (read: easy to pick up), elegance, and it’s lispy brain-expanding powers… * :point:

So if I understand you correctly you don’t want to understand this topic thoroughly but you just want a quick and simple answer?
In that case it’s enough to concentrate on the stack. There is a stack pointer (SP) in a CPU register. The stack pointer is an address into memory (the location of the stack which is simply an array that grows backwards). Every time you store something on the stack the SP get’s decremented and the value stored at the location the SP points to. When a program requests memory from the OS it get’s a pointer to that memory location and can then store that pointer on the stack. So you could say that you have one single pointer (the SP) that allows you to store and retrieve all the other pointers.

Again, for correctness…OSes pretty much only wraps virtual memory management which is a very low level operation. The language and/or runtime handle all the higher level functionality. The role of a stack is logically to support local (short lifetime) variables…some of which will be pointers (memory addresses).

I seem to recall learning how memory works (badum-tish) at about the age of 8 from a Knockabout or Ladybird book in school. Happy memories. (Canned laughter)

Cas :slight_smile:

You see, I have been through alot of languages…
HTML (complete set), Python, BASIC, a bit of Lua…
And I just ended up on Java and Java is actually a popular language when it comes to GUI apps…
I have tried C++ and didn’t like it…

I just like Java

(although, this is really getting of topic)

I think the suggestions to look at assembly weren’t to “take it up” instead of Java, or ever write any useful programs in it. Just take a look at it, which is to say take a look at the language the CPU itself understands. It will answer a lot of questions about why we have to do things certain ways in our higher-level languages, all of which need to get ultimately translated down to that common lowest level.

But anyway, the original question wasn’t totally clear so perhaps we’ve answered it by now :smiley:

Princec, I think I did the same. Back in the days when a user interface was a basic compiler. Sometimes.

In reality you ask the OS to store like say 500 numbers, the OS returns the first address and says you can use this address and the next 499 for your storage. But the idea of abstraction with programming languages is that you don’t need to worry about these details.

So… There is an offset in the memory… So the memory point 3 would actually be something like 162? But where is this ‘offset’ stored?

In the actual program code (which is itself, somewhere in memory).

Cas :slight_smile: