Code:
#include<iostream>
using namespace std;
int main(){
/*
Ram 2 part: Stack & heap
when declaring a variable it's memory allocated in the stack and it relates 2
things: value & address
memory size depends on the variable's data type(int, float, double, char)
*/
//1st
// int i; //allocate 4 byte memory sapce by data type int
// i =40;
// // char ch;
// // ch = i; //can't do this. Bcz i= 4 byts but ch = 1byte
// // int j;
// // j = i; //even then i & j have different adress
// int* k; //it create an address for address & contain integer type adress
// //k = j; //can't work. bcz k is a adress holder that only hold j's address not value
// k = &i; // &i is as a contain of k. K also have an address.
// cout << i << endl; // print i's value
// cout << &i << endl; // print i's address //print same values for k and &i
// cout << k << endl; // print i's address //print same values for k and &i
// cout << &k << endl; // print k's address
// *k = 400; //it change to 400 on k's address that means i's value. Not necessary to declare i if declare *k
// cout << *k << endl; // print i's value
// delete k;
// /*
// Dynamic memory allocation
// int j;
// int *k;
// k = &j;
// if we want this 3 line in 1 line then
// int *k(allocate in stack) = new int(allocate in heap); //int *k allocate a 4byte space on stack //k's value = heap's address
// delete k; //delete k's address that means heap address
// */
//2nd
// int i[5]; //allocates 20 bytes
// i[0] = 200;
// i[1] = 300;
// cout << i[0] << endl;
// cout << i[1] << endl;
// cout << &i[0] << endl;
// cout << &i[1] << endl;
// cout << i << endl; // prints i[0] address
// cout << *i << endl; // prints i[0] value
// cout << *(i+1) << endl; // prints i[1] value
// cout << (i+1) << endl; // prints i[1] address
// cout << (*i+1) << endl; // prints i[0] value + 1
// cout << *(i+1)+2 << endl; //prints i[1] value + 2
// delete i;
return 0;
}
0 Comments