2

Why do we use to talk about addresses and memory of variable in C, where in other languages (like in Java, .Net etc) we do not talk about variable address and memory in a program, we will directly use the variables.

But in C Language we are listening the word address and memory.

How to explain this?

I hope C is high level language designed over the assembly language. So C is a thin layer over assembly language (in assembly language we will use memory locations to store a variable and track a variable). But in other languages these addresses and memory related things are wrapped in that specific language, so that we will not listen these words.

2
  • 8
    C being high level is relative to what was available at the time (assembly), currently it is seen as one of the lower level languages Commented Nov 6, 2013 at 9:01
  • What?!? There is an out parameter in C#, for example. And, unless you explicitly take and pass a variable address in C, it is not guaranteed to have any address at all (e.g., it can be stored in a register, shared with the other variables).
    – SK-logic
    Commented Nov 6, 2013 at 10:17

4 Answers 4

12

Contrary to what anyone would say, knowing how memory works is important to know for any language, regardless of whether or not a specific language requires you to know it. For low level languages like C, memory addresses are very important to know because dedicating space to arrays or complex data structures requires a physical act of asking the operating system for a memory space of the appropriate size and you, the programmer, must be able to know how many bytes are needed. C offers helpful methods in this regard such as sizeof, but it doesn't pretend to hide it from you.

C++ only recently has moved towards removal of responsibility for programmers to free up memory allocated by having smart pointers. C# and Java do a fairly good of hiding it, but they are still there. For example in Java, if you compare two objects with ==, you are comparing their pointers. You may not know the pointer values, but you must understand that if two objects return true using == operator, then they share the same space in memory. If you did not understand this concept, you might do things like comparing Strings with ==, which may give you what you expect but maybe not. Since "mickey mouse" == "mickey mouse" may or may not share the same memory address depending on how it is implemented in the virtual machine, Java programmers should never perform such checks using ==, but rather with .equals() instead, unless of course actually checking if they share the same memory is what you want.

Of course this is just one example. With increases in memory and advances in technology, memory management is going to become less important with time. However I assure you that the one day that your program is consuming as much memory as possible and your boss is bearing down on you to fix it, how memory is managed becomes very relevant very fast. :)

16
  • 2
    "Java programmers should never perform such checks using ==, but rather with .equals() instead" - I've always found that to be so counter-intuitive. Commented Nov 6, 2013 at 11:16
  • 2
    @MetalMikester Java chose not to go down the route of C++ and C# allowing the overriding of operators like ==. I agree with you that perhaps the jvm should call .equals() in the case in which neither object is null.
    – Neil
    Commented Nov 6, 2013 at 12:07
  • @neil but there would still need to be some mechanism to compare identities an identityEquals in System would suffice but still... Commented Nov 6, 2013 at 15:08
  • 1
    @neil but there are times where you'd want true identity check like string interning Commented Nov 6, 2013 at 18:06
  • 1
    Another example: stackoverflow.com/questions/19697147/…. The questioner has a difficult time grasping the usefulness of null, and I doubt he understands it in the end. For a C programmer, that question is trivial.
    – Siyuan Ren
    Commented Nov 7, 2013 at 2:02
5

C's original purpose was to implement an operating system (Unix); part of an operating system's job is to manage memory resources, thus the language needed to expose memory addresses and operations on those addresses to the programmer.

The designers of Java and C# decided not to expose address types and operations to the programmer for a number of reasons: security, simpler memory model (at least from the programmers point of view), garbage collection, etc. But note that plenty of older languages (Fortran, Cobol, old-school BASIC, etc.) don't expose addresses either, at least not directly.

And, to clear up a popular misconception, C is not merely a thin layer over assembly; it's every bit as high-level a language as Java, it just provides low-level abstractions. Part of this is to satisfy the mandate that C be easy to implement. Making the programmer have to deal with his or her own memory management simplifies the language quite a bit. This is why C doesn't provide a set of standard container types, or a real string type that grows and shrinks as necessary. By keeping the language and the toolkit simple, C compilers can target a wide variety of platforms that other languages don't.

1
  • There wasn't a garbage collector for C. If there's no GC then programmers need to allocate/deallocate memory themselves. A good example is Objective-C, which until it gained a GC, programmers are responsible to manage memory use.
  • There was pointer before references. It's like references but you can point it wherever you like, so there was no safety.
  • Liberal type casting, you can allocate a data type and treat it as a space in memory or another kind of data type.
1

But in C Language we are listening the word address and memory.

How to explain this?

Because computers use addresses and memory. The C language is designed to be used on computers - where the formal definition of a computer is something with a CPU, some memory and some I/O. In other words, not necessarily a desktop PC. And thus to become a half-decent C programmer, you must know what a computer is and how it works.

Higher level languages have hidden away this, to enable programming for people who don't know anything about computers, so that they can focus on writing applications and algorithms, without knowing what goes on between the lines.

Many high level languages run on top of a virtual machine, which is essentially another program written in a lower level language, with the purpose to execute the scripts or byte code generated by the high level language. But in the end, all executed programs ultimately use addresses and reside in memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.