Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How a C/C++ Program Works

cpp.png


A program written in C/C++, is compiled into a binary or executable file and stored on the hard disk. To do anything useful this executable file has to be loaded into a process by the operating system from where the microprocessor can execute it.

Once an executable has been loaded into a process, the processor will start executing its code from a well known entry point usually written as the one and only main function in our program. The process is given resources by the operating system which among other things consists of:

Stack Memory - To hold auto variables, function parameters and return values. When your code invokes a function, its parameters are pushed on to the stack, its return value is implicitly pushed on to the stack and its variables are also pushed on to the stack. The called function is now logically on top of the stack. When the function ends its local variables are popped of the stack and hence local variables are also called auto variable. The size of stack memory is limited and to hold larger collections of data, we use the heap.

Heap Memory - This memory can be allocated on demandfrom our program and used to the extent of RAM limit available in the system. A reference of the address location of memory allocated to our program has to be held and later passed on explicitly for deallocation to the runtime system. This is done through a mechanism called pointers.

File Handles - This memory is used to hold file handles of open files in our program in a file handle table.

Variables in our program may be classified as data variables and pointers. Data variables are created on the stack and their address is represented in our program as a variable name. The data is stored starting from the first byte of the location. The range of bytes used to store the data depends on the type of the variable.

Pointer variables are also created on the stack but they can only hold addresses of other variables which may be on the stack or on the heap. The data held at the location indicated by the pointer can be indirectly accessed using the pointer this is known as de-referencing the pointer.

Memory can be dynamically allocated to a pointer on the heap. The size of memory allocated depends on the data type of the pointer. the pointer will hold the address of the location as long as we require. When we finish with the memory allocated, we have to explicitly deallocate the pointer which results in the pointer holding an address at which our program no longer controls memory. We have to deinitialize the pointer by setting its contents to zero or NULL.

This has been the trend with most C/C++ runtimes so far, but it may change given that now we are at the threshold of 64 bit computing and embedded computing on more powerful devices.

Comments

That's and interesting summary of how an Operating System might treat a compiled C program. But I can't find anything in that summary which relates to the C language (or C++). It seems more to be an explanation of how an Operating System might manage any compiled executable, irrespective of which language, if any, might have been used by its author when writing the source.

The impession I've gained from this seems to apply more to specific Operating Systems, rather than to languages, but I can't see a reference to the Operating System which is being described. The clue to this article's basis in a specific OS is the remark "we have to explicitly deallocate the pointer". Clearly an operating system could require that of an application or could manage memory allocation itself. But again, that would be a feature of the OS (or to be more precise, the task management element of an OS) and definitely NOT a function of a programming language.

I could go on in this vein: it would be possible to implement a task manager to handle the executable code from a C compiler on a machine which did not have a stack pointer, or even to handle the executable code from a C compiler which did not generate code for functions using the machine's stack.

I'd be interested to know if this summary was intended to refer to any feature(s) of the C language?
 
Dear kashokshenoy,
You mentioned the entry function is main() yes it is partially correct.
If you see the code(assembly) then you will see there is a start-up code. which is linked at the time of linking.
that part (start-up code) is the really entered while the OS executes the compiled program.
Next the start-up code calls the main() function.
and now your code starts executing.
 

Part and Inventory Search

Blog entry information

Author
kashokshenoy
Read time
2 min read
Views
1,170
Comments
5
Last update

More entries in Uncategorized

Share this entry

Back
Top