Which is better?
-
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 ;
orCustomCtrl* 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:
-
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 ;
orCustomCtrl* 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:
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_
-
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 ;
orCustomCtrl* 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:
-
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_
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:
-
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!
-
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:
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_
-
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_
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:
-
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 ;
orCustomCtrl* 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:
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
-
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!
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.
-
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_
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.
-
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
Thanks...
--- :beer: Hakuna-Matada :beer: It means no worries for the rest of your days... It's our problem free, Philosophy :jig:
-
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.
-
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.
-
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 ;
orCustomCtrl* 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: