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. Which is better?

Which is better?

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
14 Posts 8 Posters 1 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.
  • H HakunaMatada

    Supposing I have created a custom control derived from CWnd. Now when I use the control in my application, which is a better way of using it... CustomCtrl c ; or CustomCtrl* c = new CustomCtrl(); Which way of using the control would be more proficient?

    --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

    _ Offline
    _ Offline
    _AnsHUMAN_
    wrote on last edited by
    #2

    Hakuna-Matada wrote:

    better way of using it..

    Hakuna-Matada wrote:

    CustomCtrl c ;

    static allocation to refer to this memory allocation where all the memory that we need is allocated all at once without the issue of what is the amount of memory that we need at execution time.

    Hakuna-Matada wrote:

    CustomCtrl* c = new CustomCtrl();

    The opposite strategy, dynamic allocation, involves allocating memory on an as-needed basis. IMHO using dynamic memory is always useful if you know how to deal and avoid the memory leaks etc. issues.

    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

    H 1 Reply Last reply
    0
    • H HakunaMatada

      Supposing I have created a custom control derived from CWnd. Now when I use the control in my application, which is a better way of using it... CustomCtrl c ; or CustomCtrl* c = new CustomCtrl(); Which way of using the control would be more proficient?

      --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

      X Offline
      X Offline
      xxrl
      wrote on last edited by
      #3

      hi I recommend to try the second usage.but if you have poor memory manage techology,you have to use the first usage. For the two usage,you have to process every point exception and create a steady program.

      You are the best!Me too!

      N T 2 Replies Last reply
      0
      • _ _AnsHUMAN_

        Hakuna-Matada wrote:

        better way of using it..

        Hakuna-Matada wrote:

        CustomCtrl c ;

        static allocation to refer to this memory allocation where all the memory that we need is allocated all at once without the issue of what is the amount of memory that we need at execution time.

        Hakuna-Matada wrote:

        CustomCtrl* c = new CustomCtrl();

        The opposite strategy, dynamic allocation, involves allocating memory on an as-needed basis. IMHO using dynamic memory is always useful if you know how to deal and avoid the memory leaks etc. issues.

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        H Offline
        H Offline
        HakunaMatada
        wrote on last edited by
        #4

        So what you mean is Dynamic memory allocation is better than Static allocation if we know how to avoid memory leaks and other issues? Do we have Memory Leaks issues with static allocation or is it related to only dymanic allocation? Thanks for your answers...

        --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

        _ 1 Reply Last reply
        0
        • X xxrl

          hi I recommend to try the second usage.but if you have poor memory manage techology,you have to use the first usage. For the two usage,you have to process every point exception and create a steady program.

          You are the best!Me too!

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #5

          xxrl wrote:

          I recommend to try the second usage

          why u say so. Any reason for this?

          nave

          D 1 Reply Last reply
          0
          • H HakunaMatada

            So what you mean is Dynamic memory allocation is better than Static allocation if we know how to avoid memory leaks and other issues? Do we have Memory Leaks issues with static allocation or is it related to only dymanic allocation? Thanks for your answers...

            --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #6

            In static memory allocation the memory is used until the program ends. Thus decreasing the amount of memory for you to use in your application. On the other hand if you are working with dynamic memory allocations you need to free the memory explicitly after you are done with it. IMO using dynamic memory is difficult considering leaks etc but is more efficient in terms of memory usage and space . Memory leakage is related to dynamic allocation.

            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            H T 2 Replies Last reply
            0
            • _ _AnsHUMAN_

              In static memory allocation the memory is used until the program ends. Thus decreasing the amount of memory for you to use in your application. On the other hand if you are working with dynamic memory allocations you need to free the memory explicitly after you are done with it. IMO using dynamic memory is difficult considering leaks etc but is more efficient in terms of memory usage and space . Memory leakage is related to dynamic allocation.

              Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

              H Offline
              H Offline
              HakunaMatada
              wrote on last edited by
              #7

              Thanks a lot for clearing that up.... :)

              --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

              1 Reply Last reply
              0
              • H HakunaMatada

                Supposing I have created a custom control derived from CWnd. Now when I use the control in my application, which is a better way of using it... CustomCtrl c ; or CustomCtrl* c = new CustomCtrl(); Which way of using the control would be more proficient?

                --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #8

                Generally you should declare objects on the stack unless you need to declare them on the heap. If you do declare them on the heap consider using a smart pointer.

                Kevin

                H 1 Reply Last reply
                0
                • X xxrl

                  hi I recommend to try the second usage.but if you have poor memory manage techology,you have to use the first usage. For the two usage,you have to process every point exception and create a steady program.

                  You are the best!Me too!

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #9

                  Using the heap (i.e. new) instead of the stack can cause all sorts of performance problems due to the memory manager. However, large objects (for example, something over 20k) might cause problems if allocated on the stack.

                  Tim Smith I'm going to patent thought. I have yet to see any prior art.

                  1 Reply Last reply
                  0
                  • _ _AnsHUMAN_

                    In static memory allocation the memory is used until the program ends. Thus decreasing the amount of memory for you to use in your application. On the other hand if you are working with dynamic memory allocations you need to free the memory explicitly after you are done with it. IMO using dynamic memory is difficult considering leaks etc but is more efficient in terms of memory usage and space . Memory leakage is related to dynamic allocation.

                    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                    T Offline
                    T Offline
                    Tim Smith
                    wrote on last edited by
                    #10

                    That isn't true. Depending on the scope, declaring a variable such as "CMyClass myInstance;" can exist for the life of the program (if declared in a global scope or as a static method/function variable), exist for the life of a containing class or when used inside a method or function, exist for the life of the scope in the functoin. In general, declaring things such a "CMyClsas myInstance" is much better than declaring them on the heap. You don't have to worry about memory allocation but you do have to worry about object lifetime.

                    Tim Smith I'm going to patent thought. I have yet to see any prior art.

                    1 Reply Last reply
                    0
                    • K Kevin McFarlane

                      Generally you should declare objects on the stack unless you need to declare them on the heap. If you do declare them on the heap consider using a smart pointer.

                      Kevin

                      H Offline
                      H Offline
                      HakunaMatada
                      wrote on last edited by
                      #11

                      Thanks...

                      --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

                      1 Reply Last reply
                      0
                      • N Naveen

                        xxrl wrote:

                        I recommend to try the second usage

                        why u say so. Any reason for this?

                        nave

                        D Offline
                        D Offline
                        Don Fletcher
                        wrote on last edited by
                        #12

                        I have found that using dynamic memory allocation is always preferable, because some of the Microsoft MFC code contains the line: delete this; This will cause an exception if such a window or control is declared on the stack, and from experience it can be very frustrating to locate the source of the failure when it happens.

                        N 1 Reply Last reply
                        0
                        • D Don Fletcher

                          I have found that using dynamic memory allocation is always preferable, because some of the Microsoft MFC code contains the line: delete this; This will cause an exception if such a window or control is declared on the stack, and from experience it can be very frustrating to locate the source of the failure when it happens.

                          N Offline
                          N Offline
                          Naveen
                          wrote on last edited by
                          #13

                          do u believe Microsoft people are so foolish to do that?:-> Can u say an example...? They might have wrote such a code because they will be that much sure that the "Object" is created in the heap.

                          nave

                          1 Reply Last reply
                          0
                          • H HakunaMatada

                            Supposing I have created a custom control derived from CWnd. Now when I use the control in my application, which is a better way of using it... CustomCtrl c ; or CustomCtrl* c = new CustomCtrl(); Which way of using the control would be more proficient?

                            --- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:

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

                            See Dynamic Memory[^]

                            _**


                            **_

                            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