Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CtrlAltDelete

basic question about *var + bonus question (-:

This topic is 6927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok I was wondering which of the following is better to use (and if not better all the time, then when is it?) ClassTitle CT(var1, var2); or ClassTitle *CT = new ClassTitle(var1, var2); and unrelated to that I have this wierd bug when i leave VC++ open, after a while my computer will start to freeze(stall). in sort of this pattern . computer works for 5 seconds... Stalls for 5 seconds, then repeats. Its diving me +__INSANE__+ {; }; 0: Thanks, Chuck

Share this post


Link to post
Share on other sites
Advertisement
Well, the first statement becomes an automatic (local) variable, or a global one. The second statement gives you dynamic memory allocation. Kind of asking what''s best:
int i = 0 or int* i = new int(0)
and I hope you know the difference between those.

Your bonus? Don''t have a clue

Share this post


Link to post
Share on other sites
An object created with new is made on the heap, while a object just declared is made on the stack. Basically, its temporary v.s permanent (until you do delete, at least).

You new if you need this variable thoughout the whole program, or if you''re adding it to a linked list.

Oh god, that reminds me of a terrible bug i had once. i was creating a variable using new and adding it to a linked list. I called delete on the variable after i added it. Sounds peachy, right? Wrong. When i deleted the pointer, the memory the item in the linked list pointed to became invalid. Oh the troubles ive seen!

Share this post


Link to post
Share on other sites
From experience during a large project, I found that declaring local objects on the stack is quite "slow". (Not sure about VC++, but in CB3 at least.

I was using Borland C++ v 3.0 with large objects on the stack which were used extensively at deeper levels.

For some reason I had a noticable difference by changing 3 local objects to allocated objects AFTER the allocation of the objects. Ie: it wasnt a once-off occurance, there seemed to be some higher penalty.

The program was a win32 visual raytracer.

Doesnt immediately make sense. I could only think of one reason why it might be faster: better pointer optimisation when using objects.

It all sound quite crazy... but i''m doing a compiler optimization construction & course and optimizing isnt easy...

If its in a tight loop, dont NEW the object rather have it as a local variable.

-Tim

PS: That was CB3. I think VC++ might have better optimisation, so you might not have that problem.

(Anyone able to confirm whether VC++ optimisation is "better" than CB3 ?)



Share this post


Link to post
Share on other sites
The responses have their ideas of stack and heap memory somewhat mixed up. Creating a variable in the first example will create the variable on the stack, like they said. The stack isn''t really permenant memory at all. The variable is deleted after the function call is made (unless declared with the static keyword). The reason the heap memory is there is because the stack only has 640KB of memory. So if you need a large portion of memory, then you should declare it on the heap, using the second method. There is no real reason to use the second method unless the amount of memory you are consuming is above, say, a couple KB. Then it might be benificial to use the new operator to save your stack space. But if you''re just declaring one variable, use the local memory.


ColdfireV

Share this post


Link to post
Share on other sites
Sorry Coldfire, but that''s dated information.. The DOS, 16bit stack had 640k, but the Win32 stack can resize if you add more to it. It''s almost infinite, with virtual memory and all. I had one program for a computer class last year in Pascal where I allocated a 1 mb array on the stack (we didn''t know how to allocate in the heap in that language yet).

Just thought I''d let you know

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!