Memory Segments - Part 2 [ The sample Code ]

The complete example is shown below:

 

// CPPTST.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <conio.h>

 

//MemSeg01: Declare a Global Variable

int x = 10;

 

//MemSeg02: Store address of the Global variable in a Global Pointer

int* px = &x;

 

//MemSeg03:  Function definition. Takes a number and prints it

void PrintNo(int x)

{

            int y;

            y = x;

            printf("The given number is %d", x);

}

 

 

int _tmain(int argc, _TCHAR* argv[])

{

            //MemSeg04: Declare a loval variable and declare a pointer and store the address of local variable

            int m = 12;

            int* pm = &m;

 

            //MemSeg05: Local pointer storing the Global Address

            int* pgx = &x;

 

            //MemSeg06: Local pointer storing the heap address and assigning a value to the heap

            int *pInt = new int;

            *pInt = 22;

 

            //MemSeg07: Local pointer to a function that returns void and takes int

            void (*pFun)(int);

 

            //MemSeg08: Pointer storing the base address of the function

            pFun = &PrintNo;

 

            //MemSeg09: Calling the function through function pointer

            pFun(117);

           

 

            return 0;

}

 

In the next blog I will refer this sample code when I am explaining the Memory segments and pointers.