Comments: Flat Nested Threaded Embedded Oldest First Newest First Simple one, a lil added crap By thephoenics on Wednesday, February 27, 2008 (UMST) #include <iostream> #include <cstdlib> #include <list> using namespace std; class Allocator { public: Allocator() { cout << "Allocator Initialised" << endl; }; ~Allocator() { cout << "Deallocator Initialised" << endl; DeallocateAll(); } void *Allocate(unsigned int size) { void *newItem = malloc(size); cout << "Allocating (" << size << " bytes) space at " << hex << newItem << endl; allocatedObjects.push_back(newItem); list<void *>::iterator i; int j=0; cout << endl << "Allotted Addresses :" << endl; for (i=allocatedObjects.begin(); i!=allocatedObjects.end(); i++, j++) { cout << j << " : " << hex << *i << endl; } cout << "--- end ---" << endl << endl; return newItem; } char *Allocate(unsigned int size, const char* str) { char *temp = (char *) Allocate(size); strcpy(temp, str); return temp; } int Deallocate(void *ptr) { cout << "Deallocating space for " << (char *) ptr << " at "<< hex << ptr <<endl; free(ptr); allocatedObjects.remove(ptr); return 0; } int DeallocateAll() { list<void *>::iterator i; for (i = allocatedObjects.begin(); i != allocatedObjects.end(); i++) { if (*i != NULL) { cout << "Deallocating space for " << (char *) *i << " at "<< hex << *i <<endl; free(*i); *i = NULL; } } cout << "Deallocation Done" << endl; return 0; } private: list<void *> allocatedObjects; }; int main() { Allocator *newitem = new Allocator; char *a = (char *) newitem->Allocate(20, "hello"); char *b = (char *) newitem->Allocate(20, "hello"); char *c = (char *) newitem->Allocate(20); char *d = (char *) newitem->Allocate(20, "hello"); newitem->Deallocate(d); strcpy(c,"Hello"); cout << c << endl; delete newitem; system("PAUSE"); } Reply to this Comment @above - need some clarification By Kusumita on Sunday, October 05, 2008 (UMST) Hi , That was nice reply. But I need some more clarifications . The output of your program is coming as follows: Allocator InitialisedAllocating (20 bytes) space at 00339D60 Allotted Addresses :0 : 00339D60--- end --- Allocating (14 bytes) space at 00339E90 Allotted Addresses :0 : 00339D601 : 00339E90--- end --- Allocating (14 bytes) space at 00336668 Allotted Addresses :0 : 00339D601 : 00339E902 : 00336668--- end --- Allocating (14 bytes) space at 00336700 Allotted Addresses :0 : 00339D601 : 00339E902 : 003366683 : 00336700--- end --- Deallocating space for hello at 00336700HelloDeallocator InitialisedDeallocating space for hello at 00339D60Deallocating space for hello at 00339E90Deallocating space for Hello at 00336668Deallocation Done But I am somehow not getting the point that , when the following line is executed : char *b = (char *) newitem->Allocate(20, "hello"); How come we are allocating memory to allocatedObjects[1]? where in the code the pointer is incremented by 1 from allocatedObjects[0] to allocatedObjects[1]? The same question holds for char *c = (char *) newitem->Allocate(20); & char *d = (char *) newitem->Allocate(20, "hello"); where we are al;locating memory to allocatedObjects[2] & allocatedObjects[3] respectively ? Also for allocatedObjects[1] , allocatedObjects[2] & allocatedObjects[3] why the memory allocated is showiung 14 bytes instead od 20 bytes ? Thanks in advance Reply to this Comment
Reply to this Comment
Hi ,
That was nice reply. But I need some more clarifications .
The output of your program is coming as follows:
Allocator InitialisedAllocating (20 bytes) space at 00339D60
Allotted Addresses :0 : 00339D60--- end ---
Allocating (14 bytes) space at 00339E90
Allotted Addresses :0 : 00339D601 : 00339E90--- end ---
Allocating (14 bytes) space at 00336668
Allotted Addresses :0 : 00339D601 : 00339E902 : 00336668--- end ---
Allocating (14 bytes) space at 00336700
Allotted Addresses :0 : 00339D601 : 00339E902 : 003366683 : 00336700--- end ---
Deallocating space for hello at 00336700HelloDeallocator InitialisedDeallocating space for hello at 00339D60Deallocating space for hello at 00339E90Deallocating space for Hello at 00336668Deallocation Done
But I am somehow not getting the point that , when the following line is executed :
char *b = (char *) newitem->Allocate(20, "hello");
How come we are allocating memory to allocatedObjects[1]?
where in the code the pointer is incremented by 1 from allocatedObjects[0]
to allocatedObjects[1]?
The same question holds for
char *c = (char *) newitem->Allocate(20); & char *d = (char *) newitem->Allocate(20, "hello"); where we are al;locating memory to allocatedObjects[2] & allocatedObjects[3] respectively ?
Also for allocatedObjects[1] , allocatedObjects[2] & allocatedObjects[3]
why the memory allocated is showiung 14 bytes instead od 20 bytes ?
Thanks in advance