What is the difference between declaration and definition
Filed under: C Interview Questions, C++ Interview Questions
The declaration tells the compiler that at some later point we plan to present the definition of this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j–) //function body
cout << *;
cout << endl; }
What are the structure of Dynamic Memory Allocation, Data Segment, Code Segment and Stack area
Filed under: C Interview Questions, Microsoft Interview Questions
Process address space is organized into three memory areas, called segments: the text segment, stack segment, and data segment (bss and data) and can be illustrated below.

Code – text segment
Often referred to as the text segment, this is the area in which the executable or binary image instructions reside. For example, Linux/Unix arranges things so that multiple running instances of the same program share their code if possible. Only one copy of the instructions for the same program resides in memory at any time. The portion of the executable file containing the text segment is the text section.
Initialized data – data segment
Statically allocated and global data that are initialized with nonzero values live in the data segment. Each process running the same program has its own data segment. The portion of the executable file containing the data segment is the data section.
Uninitialized data – bss segment
BSS stands for ‘Block Started by Symbol’. Global and statically allocated data that initialized to zero by default are kept in what is called the BSS area of the process. Each process running the same program has its own BSS area. When running, the BSS, data are placed in the data segment. In the executable file, they are stored in the BSS section. For Linux/Unix the format of an executable, only variables that are initialized to a nonzero value occupy space in the executable’s disk file.
Heap
The heap is where dynamic memory (obtained by malloc(), calloc(), realloc() and new – C++) comes from. Everything on a heap is anonymous, thus you can only access parts of it through a pointer. As memory is allocated on the heap, the process’s address space grows. Although it is possible to give memory back to the system and shrink a process’s address space, this is almost never done because it will be allocated to other process again. Freed memory (free() and delete – C++) goes back to the heap, creating what is called holes. It is typical for the heap to grow upward. This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items. It is also typical for the heap to start immediately after the BSS area of the data segment. The end of the heap is marked by a pointer known as the break. You cannot reference past the break. You can, however, move the break pointer (via brk() and sbrk() system calls) to a new position to increase the amount of heap memory available.
Stack
The stack segment is where local (automatic) variables are allocated. In C program, local variables are all variables declared inside the opening left curly brace of a function’s body including the main() or other left curly brace that aren’t defined as static. The data is popped up or pushed into the stack following the Last In First Out (LIFO) rule. The stack holds local variables, temporary information, function parameters, return address and the like. When a function is called, a stack frame (or a procedure activation record) is created and PUSHed onto the top of the stack. This stack frame contains information such as the address from which the function was called and where to jump back to when the function is finished (return address), parameters, local variables, and any other information needed by the invoked function. The order of the information may vary by system and compiler. When a function returns, the stack frame is POPped from the stack. Typically the stack grows downward, meaning that items deeper in the call chain are at numerically lower addresses and toward the heap.
What is the size of an integer ?
It depend upon the machine, compiler and how big can an integer be.
For `6 bit machine (processor) its 2 byte
For 32 bit machine (processor) its 4 byte
Which tool can be used to find memory leakage and Invalid memory check
Filed under: C Interview Questions, Linux Programming Interview Questions
Valgrind can be used to find memory leakage. Valgrind is a multipurpose code profiling and memory debugging tool for Linux when on the x86 and, as of version 3, AMD64, architectures.
Below is the example on how to use valgrind:
#include
int main()
{
char *x = malloc(100); /* or, in C++, “char *x = new char[100] */
return 0;
}
% valgrind –tool=memcheck –leak-check=yes example1
This will result in some information about the program showing up, culminating in a list of calls to malloc
that did not have subsequent calls to free:
==2116== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2116== at 0×1B900DD0: malloc (vg_replace_malloc.c:131)
==2116== by 0×804840F: main (in /home/cprogram/example1)
This doesn’t tell us quite as much as we’d like, though — we know that the memory leak was caused
by a call to malloc in main, but we don’t have the line number. The problem is that we didn’t compile
using the -g option of gcc, which adds debugging symbols. So if we recompile with debugging symbols,
we get the following, more useful, output:
==2330== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2330== at 0×1B900DD0: malloc (vg_replace_malloc.c:131)
==2330== by 0×804840F: main (example1.c:5)
