Sie sind auf Seite 1von 7

What is the difference between realloc() and free()?

The free subroutine frees a block of memory previously allocated by the malloc s ubroutine. Undefined results occur if the Pointer parameter is not a valid point er. If the Pointer parameter is a null value, no action will occur. The realloc subroutine changes the size of the block of memory pointed to by the Pointer par ameter to the number of bytes specified by the Size parameter and returns a new pointer to the block. The pointer specified by the Pointer parameter must have b een created with the malloc, calloc, or realloc subroutines and not been dealloc ated with the free or realloc subroutines. Undefined results occur if the Pointe r parameter is not a valid pointer. The Free() function is used to release the memory block allocated by any one of malloc, calloc or realloc. Where as the realloc function is used change the size of memory block.which is g iven that is total size not only increase size. and data is stored will remain unchanged. ////////////////////////////////////////////////////////// What is "strstream?? Class that reads and writes to an array in memory class that reads only memory that contain array ////////////////////////////////////////////////////////// Why always array starts with index 0? Array name is a constant pointer pointing to the base address(address of the fir st byte where the array begin) of the memory allocated. When you use arr[i], the compiler manipulates it as *(arr + i). Since arr is the address of the first el ement, the value of i must be 0 for accessing it. Hence all arrays begin with an index of 0. The compiler thinks that 0 is a positive number. we have an integer range of -128 to 127. here -128 to -1 are negative numbers and 0 to 127 are positive numbers so array starts with the 0 as index. //////////////////////////////////////////////////////////////////////////////// / Difference between stack memory and heap memory? STACK memory is referred as temporary memory,if you come out of the program the memory of the variable will not no more there.[eg., int a; memory for a will not maintained after v came out from the program]. HEAP memory is referred as permanent memory,memory allocated for the object will be maintained even if we came out of the program.[eg.memory for OBJECT will rem ains there ever]. Heap is allocated to a process by OS on start; therefore if program will be term inated entire heap memory will be freed. The heap The heap (also known as the free store) is a large pool of memory used for dynam ic allocation. In C++, when you use the new operator to allocate memory, this me mory is assigned from the heap. The stack The call stack (usually referred to as the stack) has a much more interesting ro le to play.about the call stack, which refers to a particular portion of memory.

//////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////// What is the real-time example for abstract class and pure virtual function? Abstract class: A class that contains at least one pure virtual function is said to be abstract. For example: class Base_Employer { public: // salary(ar-list) is a pure virtual function virtual void salary(arg-list) = 0; }; class derived_Trainee: Public Base_Employer { Public: // salary virtual function is redefined for trainee salary structure void salay(arg-list) { // redefined for trainee } }; class derived_SoftwwareEngineer: Public Base_Employer { Public: // salary virtual function is redefined for Softwware Engineer salary structure void salay(arg-list) { // redefined for Softwware Engineer } }; class derived_ProjectManager: Public Base_Employer { Public: // salary virtual function is redefined for Project Manager salary structure void salay(arg-list) { // redefined for Project Manager } }; Note: Redefinition of the salary structure for each derived class is neccessary and it is different for each level of employment for a employer. //////////////////////////////////////////////////////////////////////////////// //////////////////////////////// What is a memory leak? How can we avoid it? A memory leak can be avoided by making sure that whatever memory has been dynami cally allocated will be cleared after the use of the same. for example int main()

{ char *myCharData[20]; for (int nLoop =0;nLoop < 20; ++nLoop) { myCharData[nLoop ] = new char[256]; strcpy(myCharData[nLoop],"SABITH"); ....... } ......................... /*Some manipulations here using myCharData*/ /*Now here we have to clear the data. The place can vary according to ur program . This being a simple program,u can clear at the end*/ for(int nLoop =0;nLoop < 20; ++nLoop) { delete[] myCharData[nLoop ]; } return 0;

memory leak - when we allocate memory dynamically and somehow lose the way to re ach that memory. ex: void fun() { int *ptr = new int; } void main() { fun(); } here the pointer ptr is local to the function fun(); which dies when control ret urns from the function.so we loose the way to reach that address, but the dynami cally allocated memory would continue to remain allocated. the easiest way to avoid memory leak is to place the pointers inside objects and let the objects manage them. Memory leaks happens for the memory allocated on Heap(ex A *temp = new A()) . me mory allocated by us on stack (int a) is released automatically when the functio n returns or module goes out of scope. But memory allocated on heap will not be freed automatically, we need to release it manually. ex: func()

{ A *a = new A(); //on heap int b; // on stack } main() { func(); } Now in above example when func is called memory for "a" is created on HEAP by us ing NEW, but it is not freed by using DELETE, hence is memory leak. On the other hand "b" is created on STACK & freed automatically. so correct implementation i s: func() { A *a = new A(); //on heap int b; // on stack delete a; //deleting memory on heap } main() { func(); } //////////////////////////////////////////////////////////////////////////////// // Can you allocate the memory using malloc() in C and deallocate the same memory u sing free() in c++? No, we should not do that. if we do that we can have un-defined or un-predictabl e results. Which can corrupt other memory too. Instead, we should make a simple rule not to do that. there is in a error in the question. We can delete memory created by malloc() by using free() comfortably and same applies for new/delete combination. However memory created using malloc() cant be deleted using delete and same for new/free() combination. //////////////////////////////////////////////////////////////////////////////// ////////// When does a name clash occur? A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. I f you try to use many class libraries at the same time, there is a fair chance t hat you will be unable to compile or link the program because of name clashes. //////////////////////////////////////////////////////////////////////////////// ////////// Define a class to represent a bank account. In the class, include the following members: Data members: name of the depositor, account number, type of account, a nd balance amount in the account. Member functions: to assign initial values, to deposit an amount, to withdrawal an amount after checking the balance, and to d isplay the name and balance. Write main() code to test your class?

#include<iostream.h> #include<stdio.h>

#include<conio.h> #include<process.h> // statement of customer detail.... struct acc_data { char c_name[25]; int acc_no; float balance; char acc_type[2]; }; //statement of Class.... class bank { struct acc_data x; public: void input(); void withdrawal(int,float); void deposit(int,float); void display(int); }; // Statement of customer information... void bank:: input() { cout<<endl<<" Enter the name of customer:- "; gets(x.c_name); cout<<" Enter account no:- "; cin>>x.acc_no; cout<<" Enter opening balance:- "; cin>>x.balance; cout<<" Enter acc_type (SA/CA):- "; gets(x.acc_type); } //Statement of Withdrawal function... void bank::withdrawal(int ac,float money) { if(x.acc_no==ac) { if(x.balance-1000>=money) x.balance -= money; else cout<<" Sorry, Insufficient Balance"; } else cout<<" ** Sorry, Account no. is not found ** "; } // Statement of deposite function...

void bank::deposit(int ac,float money) { if(ac==x.acc_no) x.balance += money; else cout<<" Sorry, Account no. is not found..."; } // Statement of display Funtion... void bank::display(int ac) { if(ac==x.acc_no) { cout<<" Your acc_no is: "<<x.acc_no; cout<<" Your name is: "; puts(x.c_name); cout<<" Your current balance is: "<<x.balance; cout<<" YOur type of account is: "<<x.acc_type; } } // statement of main Function.... main() { bank obj; //creating object for class bank int ac; float money; int c; clrscr(); do { clrscr(); cout<<endl<<" Welcome to Banking System"; cout<<" _________________________"; cout<<endl<<" 1:Open New Account of the customer."; cout<<endl<<" 2:Withdrawal the money."; cout<<endl<<" 3:Deposit the money."; cout<<endl<<" 4:Display information about customer."; cout<<endl<<" 5:quit"; cout<<endl<<" what is your choice: "; cin>>c; switch(c)

{ case 1: obj.input(); break; case 2: cout<<" Enter account no: "; cin>>ac; cout<<" enter the money to withdrawal: "; cin>>money; obj.withdrawal(ac,money); break; case 3: cout<<" enter acc no: "; cin>>ac; cout<<" enter the money to deposit: "; cin>>money; obj.deposit(ac,money); break; case 4: cout<<" enter acc no: "; cin>>ac; obj.display(ac); break; case 5: exit(0); default: cout<<" Sorry, unable to process.. Try again later "; } cout<<" Press Enter to continue..."; getch(); }while(c!=5); return 0; }

Das könnte Ihnen auch gefallen