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. Question about dynamically allocated memory

Question about dynamically allocated memory

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancetutorial
8 Posts 5 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.
  • E Offline
    E Offline
    Eikthrynir
    wrote on last edited by
    #1

    Hi! I have a question about dynamically allocated memory, actually about cleaning that memory. For example I compile and run this piece of code: char *pString = NULL; if ( (pString = new char [100]) == NULL ) exit( EXIT_FAILURE ); ... <--- Use pString delete [] pString; The question is how does the program know how much memory (how many bytes) to free (deallocate)? Isn't it compulsory to specify the number of bytes between the two brackets in order to be sure that it will free all the dynamically allocated memory? :confused: Any suggestions would be greatly appreciated! Thanks in advance!

    S P H 3 Replies Last reply
    0
    • E Eikthrynir

      Hi! I have a question about dynamically allocated memory, actually about cleaning that memory. For example I compile and run this piece of code: char *pString = NULL; if ( (pString = new char [100]) == NULL ) exit( EXIT_FAILURE ); ... <--- Use pString delete [] pString; The question is how does the program know how much memory (how many bytes) to free (deallocate)? Isn't it compulsory to specify the number of bytes between the two brackets in order to be sure that it will free all the dynamically allocated memory? :confused: Any suggestions would be greatly appreciated! Thanks in advance!

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Each heap allocation keeps track of its size.

      Steve

      1 Reply Last reply
      0
      • E Eikthrynir

        Hi! I have a question about dynamically allocated memory, actually about cleaning that memory. For example I compile and run this piece of code: char *pString = NULL; if ( (pString = new char [100]) == NULL ) exit( EXIT_FAILURE ); ... <--- Use pString delete [] pString; The question is how does the program know how much memory (how many bytes) to free (deallocate)? Isn't it compulsory to specify the number of bytes between the two brackets in order to be sure that it will free all the dynamically allocated memory? :confused: Any suggestions would be greatly appreciated! Thanks in advance!

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #3

        See this[^]

        Prasad Notifier using ATL

        1 Reply Last reply
        0
        • E Eikthrynir

          Hi! I have a question about dynamically allocated memory, actually about cleaning that memory. For example I compile and run this piece of code: char *pString = NULL; if ( (pString = new char [100]) == NULL ) exit( EXIT_FAILURE ); ... <--- Use pString delete [] pString; The question is how does the program know how much memory (how many bytes) to free (deallocate)? Isn't it compulsory to specify the number of bytes between the two brackets in order to be sure that it will free all the dynamically allocated memory? :confused: Any suggestions would be greatly appreciated! Thanks in advance!

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

          See here[^]

          _**


          **_

          WhiteSky


          C 1 Reply Last reply
          0
          • H Hamid Taebi

            See here[^]

            _**


            **_

            WhiteSky


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

            :confused: Did you read his question ?? The link you posted has nothing to do with new and delete, it is purely C programming (alloc, malloc, realloc, ...). When posting a link, at least take the time to verify if it contains valuable information for the OP. Just googling and pasting the first link that cames up is not really helpfull.


            Cédric Moonen Software developer
            Charting control [Updated - v1.1]

            H 1 Reply Last reply
            0
            • C Cedric Moonen

              :confused: Did you read his question ?? The link you posted has nothing to do with new and delete, it is purely C programming (alloc, malloc, realloc, ...). When posting a link, at least take the time to verify if it contains valuable information for the OP. Just googling and pasting the first link that cames up is not really helpfull.


              Cédric Moonen Software developer
              Charting control [Updated - v1.1]

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

              It seems I must read complete a thread not only title,thanks for remind,sometime when I see a title I guess body is like title (thanks), but i dont think that stuff and paste link is good,yes?

              _**


              **_

              WhiteSky


              C 1 Reply Last reply
              0
              • H Hamid Taebi

                It seems I must read complete a thread not only title,thanks for remind,sometime when I see a title I guess body is like title (thanks), but i dont think that stuff and paste link is good,yes?

                _**


                **_

                WhiteSky


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

                WhiteSky wrote:

                It seems I must read complete a thread not only title

                :omg::wtf: Of course !! That seems logical no ? How can you describe completely a problem in one single title line ??

                WhiteSky wrote:

                but i dont think that stuff and paste link is good

                It depends. Sometimes it is usefull, but sometimes it can be really annoying. For example if that links to a lot of text and if the OP needs to search a lot to find the information, it is in general not very valuable. And if you provide a link to something you googled for, then at least verify that it contains the information. We ask the people who post the questions to have some respect for the other (so formulate the questions correctly, use proper english if possible, ...) but the opposite is true also.


                Cédric Moonen Software developer
                Charting control [Updated - v1.1]

                H 1 Reply Last reply
                0
                • C Cedric Moonen

                  WhiteSky wrote:

                  It seems I must read complete a thread not only title

                  :omg::wtf: Of course !! That seems logical no ? How can you describe completely a problem in one single title line ??

                  WhiteSky wrote:

                  but i dont think that stuff and paste link is good

                  It depends. Sometimes it is usefull, but sometimes it can be really annoying. For example if that links to a lot of text and if the OP needs to search a lot to find the information, it is in general not very valuable. And if you provide a link to something you googled for, then at least verify that it contains the information. We ask the people who post the questions to have some respect for the other (so formulate the questions correctly, use proper english if possible, ...) but the opposite is true also.


                  Cédric Moonen Software developer
                  Charting control [Updated - v1.1]

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

                  Cedric Moonen wrote:

                  Of course !! That seems logical no ? How can you describe completely a problem in one single title line ??

                  "How to show a bmp file on background of dialog" a title i think see yesterday not sure well anyway when you see this sentence what do you think?so its possible of course not always. -------------------------- ...but sometime Im wondering because you see a question and sender explain a lot of then you search and find a site when you answer to sender he say its very helpful,great and... but i want to know why sender doesnt search or sometime some one ask what is ?(it) without any try

                  _**


                  **_

                  WhiteSky


                  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