1
$\begingroup$

When we write code, after compilation the code will be converted to machine language and then stored in the hard disk. But before compiling the code, it is still in the high-level language. How and where is the memory allocated for the source code before compilation?

I assume that before compiling the code is stored in RAM. How is the code stored in RAM? Because I think we can store only machine language in RAM.

$\endgroup$
5
  • 3
    $\begingroup$ Welcome to the site. Of course I have no way of knowing where you got the notion that one can only store machine language in RAM. We can store anything, in any format, in RAM. It's just bits as far as the computer is concerned. $\endgroup$ Commented Sep 6, 2020 at 17:27
  • $\begingroup$ isn't the machine language is nothing but bits? $\endgroup$
    – vinter
    Commented Sep 6, 2020 at 17:38
  • $\begingroup$ @RickDecker when we write something in notepad, how it is converted into bits(machine language)? $\endgroup$
    – vinter
    Commented Sep 6, 2020 at 17:40
  • $\begingroup$ I can store machine words in RAM. I can have them interpreted by the RAM containing that memory - during compilation rather than before. $\endgroup$
    – greybeard
    Commented Sep 7, 2020 at 14:26
  • 1
    $\begingroup$ @SHASHIKANTHGAJJELLI it's not converted into bits. it's always bits. there is nothing else. $\endgroup$
    – user20574
    Commented Sep 8, 2020 at 18:45

3 Answers 3

4
$\begingroup$

Here is a brief overview of a typical compilation and execution flow on a computer:

  1. Type in the source code in a plain text editor.
  2. This source code consists of characters. Characters are encoded as bits. An example of encoding is ASCII. These bits are stored on the disk, when you save the source code.
  3. Invoke the compiler to compile the source code. The compiler parses the source code file(s), produces object code and links the object files to produce a machine language file called executable. The executable is stored on the disk.
  4. Run the executable. This is called execution.
  • At all steps in the process, the operating system controls and co-ordinates processes and does the memory management.

  • Data is stored in the RAM temporarily when the process running on the machine requires it.

  • Data is stored on the disk when it needs to be stored permanently.

  • There is also a process called swapping which is done by the OS when there is no RAM memory left. A section of the disk memory is used for swapping.

  • There can be multiple processes running on the machine. These processes can run parallely or concurrently. The operating system allocates resources for processes and schedules the processes to run using scheduling algorithms.

This was just a brief overview. To learn more, I highly recommend the free online course on MIT's OCW called Computational Structures.

$\endgroup$
2
$\begingroup$

Typically source code is stored in a file on your hard drive or SSD drive. Usually you have backup copies in all kinds of places, and you use source code control to store more copies in more places, together with the ability to get older versions or newer versions to your files at any time.

When compiling, the source code files are read like any other files. Maybe they are read completely, or only in parts. Whatever the author of the compiler thinks is most efficient. The source code will be moved to the address space of the compiler, and then to RAM. Depending on your operating system, if you compile the same program multiple times, file contents may be automatically cached in RAM.

$\endgroup$
3
  • $\begingroup$ Thanks for your response, but my question is, how the source code is stored in RAM or drive, in which format. Does it automatically gets stored in bits( or machine language)? $\endgroup$
    – vinter
    Commented Sep 7, 2020 at 2:32
  • $\begingroup$ If I open a notepad and write some content, how it is converted to bits and before saving, where does it gets stored? $\endgroup$
    – vinter
    Commented Sep 7, 2020 at 2:33
  • $\begingroup$ So your question has nothing to do with compilers? $\endgroup$
    – gnasher729
    Commented Sep 7, 2020 at 18:59
0
$\begingroup$

This reply was stored in RAM on your computer as part of the mechanisms involved in you reading it.

It is very definitely not written in machine code. I don't even know what the machine code of your computer is.

Fundamentally, RAM stores bits. The meaning of the bits is not inherent in the bits. These particular bits are used as data to some software that turns them into dots on a screen.

More fundamentally: if RAM only stored "machine code", there would be no data for the machine code to compute with.

$\endgroup$

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.