Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Management Problem C++ (malloc & free)

Memory Management Problem C++ (malloc & free)

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresperformancehelp
14 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dehseth
    wrote on last edited by
    #1

    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.. :)

    C R E B J 8 Replies Last reply
    0
    • D dehseth

      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.. :)

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • D dehseth

        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.. :)

        R Offline
        R Offline
        Rajkumar R
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D dehseth

          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.. :)

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          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.

          R 1 Reply Last reply
          0
          • D dehseth

            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.. :)

            E Offline
            E Offline
            Eakalavya
            wrote on last edited by
            #5

            char* Aii = (char *)*((int*)a[i]); What is This? char* Aii = (char *)((int*)a[i]);

            D 1 Reply Last reply
            0
            • B BadKarma

              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.

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              BadKarma wrote:

              a = (char**) calloc(10, sizeof(int));

              what about sizeof (char *)

              R 1 Reply Last reply
              0
              • D dehseth

                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.. :)

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                Why are you using pure C here? As you program C++, try to use C++ structures. Like a std::vector of std::strings. Or the MFC-class CStringArray. Also, input and output is much easier using the predefined IOStreams std::cin and std::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"

                1 Reply Last reply
                0
                • D dehseth

                  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.. :)

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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 follows

                  char ** a;

                  hence you must allocate memory this way

                  a = (char**) calloc(10, sizeof(char *));

                  Please note the * operator inside sizeof. BTW as Cedric Moonen already stated, in C++ 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

                  1 Reply Last reply
                  0
                  • R Rajkumar R

                    BadKarma wrote:

                    a = (char**) calloc(10, sizeof(int));

                    what about sizeof (char *)

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    Good question. :-D

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    1 Reply Last reply
                    0
                    • D dehseth

                      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.. :)

                      D Offline
                      D Offline
                      dehseth
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • E Eakalavya

                        char* Aii = (char *)*((int*)a[i]); What is This? char* Aii = (char *)((int*)a[i]);

                        D Offline
                        D Offline
                        dehseth
                        wrote on last edited by
                        #11

                        just tryin smt..

                        1 Reply Last reply
                        0
                        • D dehseth

                          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.. :)

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          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

                          D 1 Reply Last reply
                          0
                          • D David Crow

                            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

                            D Offline
                            D Offline
                            dehseth
                            wrote on last edited by
                            #13

                            Problem has been solved. i is the loop integer which gets values from zero to ten in given example code.

                            D 1 Reply Last reply
                            0
                            • D dehseth

                              Problem has been solved. i is the loop integer which gets values from zero to ten in given example code.

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              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

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups