Memory Management Problem C++ (malloc & free)
-
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :) -
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)If you are writing C++ code (you are using classes, which is C++ specific), why do you still want to use C functions like malloc ? Prefer the use of new and delete instead.
Cédric Moonen Software developer
Charting control [v1.4] -
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)dehseth wrote:
sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point } };
you are allocating 10 items (for (int i = 0; i < 10; ) and freeing 50 items (for (int i = 0; i < 50; ) and what you are trying to achieve with char* Aii = (char *)*((int*)a[i]); and you are not freeing the first dimension array "a" in sub and in main(). I prefer you to learn the concepts clearly
-
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)Hi, Like Cedric has mentioned you should try to use thew new and delete.
dehseth wrote:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
a has a size of 10 bytes (calloc(10, sizeof(char)) = 10 * 1 byte) but to store 10 pointers you need 40 byte since a pointer has a 4 byte size (sizeof(int)). Rewrite the allocation of a:
a = (char**) calloc(10, sizeof(int));
But again you should try to use the new and delete.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :) -
Hi, Like Cedric has mentioned you should try to use thew new and delete.
dehseth wrote:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
a has a size of 10 bytes (calloc(10, sizeof(char)) = 10 * 1 byte) but to store 10 pointers you need 40 byte since a pointer has a 4 byte size (sizeof(int)). Rewrite the allocation of a:
a = (char**) calloc(10, sizeof(int));
But again you should try to use the new and delete.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
BadKarma wrote:
a = (char**) calloc(10, sizeof(int));
what about sizeof (char *)
-
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)Why are you using pure C here? As you program C++, try to use C++ structures. Like a
std::vector
ofstd::strings
. Or the MFC-class CStringArray. Also, input and output is much easier using the predefined IOStreamsstd::cin
andstd::cout
!Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)You make the same error on each allocation: For instance your code is
dehseth wrote:
a = (char**) calloc(10, sizeof(char));
but
a
is declared as followschar ** a;
hence you must allocate memory this way
a = (char**) calloc(10, sizeof(char *));
Please note the
*
operator insidesizeof
. BTW as Cedric Moonen already stated, inC++
language,new
is the preferred way to allocate memory. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
BadKarma wrote:
a = (char**) calloc(10, sizeof(int));
what about sizeof (char *)
Good question. :-D
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)First of all thank you everyone! I am writing the code in DLL, and this was not the actual code. I need malloc and calloc and free in my code. Problem solved by using * in sizeof functions. As an example: previous code:
allocater** a = (allocater**) calloc(10, sizeof(allocater));
new code:allocater** a = (allocater**) calloc(10, sizeof(allocater*****));
I forgot that 50 in loop since I have tried too many things I guess. I should have noticed this ;) Thank you everybody again. Let's keep movin' :-D -
Hey everybody, I'm using MS Visual C++ 6.0 I do have a code which contains classes in classes. Most inner class has a char** pointer which allocates memory using malloc function and adds a char array to string array. To be more specific:
a = (char**) calloc(10, sizeof(char)); // main array for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); // char arrays }
the problem is I want a destructor which free this array. Says Access Violation each time I try to execute. :doh: here is my code:#include "stdafx.h" #include <malloc.h> #include <stdio.h> #include <conio.h> class sub { public: char** a; sub(); ~sub(); }; sub::sub() { a = (char**) calloc(10, sizeof(char)); for (int i = 0; i < 10; i++) { a[i] = (char*) calloc(10 , sizeof(char)); } printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]); }; sub::~sub() { for (int i = 0; i < 50; i++) { char** A = a; char* Ai = a[i]; char* Aii = (char *)*((int*)a[i]); free(a[i]); // crash point :confused: } }; class allocater { public: sub** subs; allocater(); ~allocater(); }; allocater::allocater() { subs = (sub**) calloc(10, sizeof(subs)); int i; for (i = 0; i < 10; i++) { subs[i] = new sub(); } }; allocater::~allocater() { int i; printf("destructor allocater"); for (i = 0; i < 10; i++) { delete subs[i]; } }; void main(int argc, char* argv[]) { allocater** a = (allocater**) calloc(10, sizeof(allocater)); int i; for (i = 0; i < 10; i++) { a[i] = new allocater(); } printf("allocated"); // getch(); for (i = 0; i < 10; i++) { delete a[i]; } printf("freed"); getch(); }
Thank you everyone.. :)dehseth wrote:
free(a[i]); // crash point
What is the value of
i
?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
dehseth wrote:
free(a[i]); // crash point
What is the value of
i
?"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Problem has been solved. i is the loop integer which gets values from zero to ten in given example code.
dehseth wrote:
i is the loop integer which gets values from zero to ten in given example code.
Actually, it was being incremented to 50, hence the crash and my question. ;)
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne