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 problem

Memory problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpperformancetutorialquestion
11 Posts 7 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.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

    H N S 3 Replies Last reply
    0
    • A Anu_Bala

      Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      I think that these links are helpful to you Dynamic memory allocation in C++[^] and Memory leaks in C++ and how to avoid them[^]_**


      **_

      whitesky


      1 Reply Last reply
      0
      • A Anu_Bala

        Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        Anu_Bala wrote:

        I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

        Well some of the things I know I would like to share. :)

        1. Use new and delete.

        2. Always use delete[] with new[] and delete with new

        3. Do not mix up c, c++. Either you use C, or use C++ for memory allocation.

        4. Always set a pointer to NULL after deletion.

        5. Always allocate sufficient amount of memory. Do not allocate memory like this...

          char *str = new char[ strlen( someStr ) ];//stingy
          strcpy( str, someStr );// well asking for trouble
          delete [] str;//error here
          str = NULL;//good practice

          instead use

          char *str = new char[ strlen(someStr) + 1 ];//bring it on
          strcpy( str, someStr );//gooood, very gooood.
          delete [] str;//hehe fine
          str = NULL;//good practice

        6. Try using auto_ptr;

        7. Maintain a list of pointers to memory that you have allocated. When you exit from your app you can delete them all at one go if any of them hasn't been deleted.

        8. Always check for NULL on pointers after a new.

        9. Always check for NULL on pointers before using them.

        10. Be really really afraid to dynamically allocate memory.


        Nibu thomas A Developer Programming tips[^]  My site[^]

        D 1 Reply Last reply
        0
        • A Anu_Bala

          Hi, I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          I suggest you to read Effective C++ 2nd Edition or the latest 3rd Edition. If u r confused about pointers, read Pointers in C to know more about pointers. as nibu said if u r much concerned about pointers, try using auto_pointers that is the best way to handle memory leaks. but I suggest that not to use auto_pointer because simply u can handle those allocation and deallocation of objects. In MFC, it is not using any special allocation strategy, its all C++. Code Complete 2 is a book which contains some best practices. SaRath
          "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

          L 1 Reply Last reply
          0
          • S Sarath C

            I suggest you to read Effective C++ 2nd Edition or the latest 3rd Edition. If u r confused about pointers, read Pointers in C to know more about pointers. as nibu said if u r much concerned about pointers, try using auto_pointers that is the best way to handle memory leaks. but I suggest that not to use auto_pointer because simply u can handle those allocation and deallocation of objects. In MFC, it is not using any special allocation strategy, its all C++. Code Complete 2 is a book which contains some best practices. SaRath
            "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            SaRath C wrote:

            In MFC, it is not using any special allocation strategy, its all C++.

            That is not entirely accurate. See this MSDN Article[^]

            "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
            Christian Graus in the C# forum

            led mike

            S 1 Reply Last reply
            0
            • L led mike

              SaRath C wrote:

              In MFC, it is not using any special allocation strategy, its all C++.

              That is not entirely accurate. See this MSDN Article[^]

              "What classes are you using ? You shouldn't call stuff if you have no idea what it does"
              Christian Graus in the C# forum

              led mike

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              Thanks for ur information. It was quite new for me. Anu said that (he/she) is new to MFC and suffering with some exception and all. Everything has an user point of view. We are the users of C++ right, at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact. never I meant any regarding internal handling of compiler. I think the language I used was something not proper :( SaRath
              "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -- modified at 1:55 Thursday 1st June, 2006

              P 1 Reply Last reply
              0
              • N Nibu babu thomas

                Anu_Bala wrote:

                I am working in vc++ for past 4 months.But i dont know very much about MFC.That is where the variables stored and memory allocation like that.When im doing coding,i got error as unhandled exception,Out of memory like that.So, i think im unaware about the memory allocation.So, Is there any tutorial about this allocation etc.,..?

                Well some of the things I know I would like to share. :)

                1. Use new and delete.

                2. Always use delete[] with new[] and delete with new

                3. Do not mix up c, c++. Either you use C, or use C++ for memory allocation.

                4. Always set a pointer to NULL after deletion.

                5. Always allocate sufficient amount of memory. Do not allocate memory like this...

                  char *str = new char[ strlen( someStr ) ];//stingy
                  strcpy( str, someStr );// well asking for trouble
                  delete [] str;//error here
                  str = NULL;//good practice

                  instead use

                  char *str = new char[ strlen(someStr) + 1 ];//bring it on
                  strcpy( str, someStr );//gooood, very gooood.
                  delete [] str;//hehe fine
                  str = NULL;//good practice

                6. Try using auto_ptr;

                7. Maintain a list of pointers to memory that you have allocated. When you exit from your app you can delete them all at one go if any of them hasn't been deleted.

                8. Always check for NULL on pointers after a new.

                9. Always check for NULL on pointers before using them.

                10. Be really really afraid to dynamically allocate memory.


                Nibu thomas A Developer Programming tips[^]  My site[^]

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

                Nibu thomas wrote:

                strcpy( str, someStr );// well asking for trouble

                True, use strncpy() instead.

                Nibu thomas wrote:

                delete [] str;//error here

                Why do you assert this is in error?

                Nibu thomas wrote:

                Be really really afraid to dynamically allocate memory.

                Why?


                "The largest fire starts but with the smallest spark." - David Crow

                N 1 Reply Last reply
                0
                • S Sarath C

                  Thanks for ur information. It was quite new for me. Anu said that (he/she) is new to MFC and suffering with some exception and all. Everything has an user point of view. We are the users of C++ right, at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact. never I meant any regarding internal handling of compiler. I think the language I used was something not proper :( SaRath
                  "D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!" -- modified at 1:55 Thursday 1st June, 2006

                  P Offline
                  P Offline
                  palbano
                  wrote on last edited by
                  #8

                  SaRath C wrote:

                  at our point of view, operator new has a standard form and a predefined way which it should be used. I just indicated about that fact.

                  ah, I understand now, thanks. :)

                  -- signature under construction --

                  -pete

                  1 Reply Last reply
                  0
                  • D David Crow

                    Nibu thomas wrote:

                    strcpy( str, someStr );// well asking for trouble

                    True, use strncpy() instead.

                    Nibu thomas wrote:

                    delete [] str;//error here

                    Why do you assert this is in error?

                    Nibu thomas wrote:

                    Be really really afraid to dynamically allocate memory.

                    Why?


                    "The largest fire starts but with the smallest spark." - David Crow

                    N Offline
                    N Offline
                    Nibu babu thomas
                    wrote on last edited by
                    #9

                    DavidCrow wrote:

                    Nibu thomas wrote: delete [] str;//error here

                    That was in relation to the point I mentioned. i.e. to allocate sufficient amount of memory. Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block. And when you try to delete such pointers it will result in memory damaged error message. strcpy was used here just to regenerate that issue.

                    DavidCrow wrote:

                    Nibu thomas wrote: Be really really afraid to dynamically allocate memory. Why?

                    Yeah, use it as a last resort. For eg: using standard stl classes can avoid most of the issues. Note: I didn't say not to use it but instead to be afraid. Sometimes being afraid is good. :)


                    Nibu thomas A Developer Programming tips[^]  My site[^]

                    D 1 Reply Last reply
                    0
                    • N Nibu babu thomas

                      DavidCrow wrote:

                      Nibu thomas wrote: delete [] str;//error here

                      That was in relation to the point I mentioned. i.e. to allocate sufficient amount of memory. Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block. And when you try to delete such pointers it will result in memory damaged error message. strcpy was used here just to regenerate that issue.

                      DavidCrow wrote:

                      Nibu thomas wrote: Be really really afraid to dynamically allocate memory. Why?

                      Yeah, use it as a last resort. For eg: using standard stl classes can avoid most of the issues. Note: I didn't say not to use it but instead to be afraid. Sometimes being afraid is good. :)


                      Nibu thomas A Developer Programming tips[^]  My site[^]

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

                      Nibu thomas wrote:

                      Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block.

                      But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.

                      Nibu thomas wrote:

                      For eg: using standard stl classes can avoid most of the issues.

                      Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.


                      "The largest fire starts but with the smallest spark." - David Crow

                      N 1 Reply Last reply
                      0
                      • D David Crow

                        Nibu thomas wrote:

                        Well if you are using strcpy for that purpose strcpy will put a NULL char just after the end of the memory block.

                        But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.

                        Nibu thomas wrote:

                        For eg: using standard stl classes can avoid most of the issues.

                        Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.


                        "The largest fire starts but with the smallest spark." - David Crow

                        N Offline
                        N Offline
                        Nibu babu thomas
                        wrote on last edited by
                        #11

                        DavidCrow wrote:

                        But the point is that the source could be longer than the destination, which is why strcpy() should not be used, regardless of how it deals with the \0 terminator.

                        Exactly, I was just demonstrating how such things could happen.

                        DavidCrow wrote:

                        Last time I checked, STL used memory from the heap (i.e., dynamic). There's just not any way around not using memory from the heap.

                        Exactly, they work by allocating on the heap. But then we don't have to do it. These are proven classes (AFAIK) which work efficiently. Hence using them would be best for us. These classes takes the onus on them to do memory clean up.


                        Nibu thomas A Developer Programming tips[^]  My site[^]

                        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