Various C++ questions
1. Is there any command or keyword that ends a program right there and then?
2. What are the benefits of putting a pointer on the free store?
3. When you declare a variable does the memory come from the hard drive or ram.
Thanks in advance
1) Not that I know of. There are, however, platform-specific functions. In Win32 and Win32 console, you can use exit(...). This is generally not a good practice, because you might create memory and resource leaks.
2) [BS-ing] Because the stack is slower.
3) RAM.
Peace,
ZE.
(BTW, I''m not sure on #2, obviously )
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
2) [BS-ing] Because the stack is slower.
3) RAM.
Peace,
ZE.
(BTW, I''m not sure on #2, obviously )
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[twitter]warrenm[/twitter]
1) cleanly: exit() (yes, it is standard and portable), violently: abort() (portable too).
2) Succintly, pointer to the free store allow you to create new variables at run time. You are not restricted to the named variables and fixed-size arrays you explicitely wrote in your source code (though you must use them to access the pointers). Note though that while the allocation process is slower than creating a local variable on the stack, access is just as fast.
3) This is architecture- and operating-system-dependend. Usually, it comes from the RAM until some threshold is reached, then, if you have a virtual memory system, it will start to page out unused memory block to the disk to free up some RAM which can then be allocated to your program. In that respect the memory ''comes from the disk'' as the paging process is sluggish compared to memory access.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
2) Succintly, pointer to the free store allow you to create new variables at run time. You are not restricted to the named variables and fixed-size arrays you explicitely wrote in your source code (though you must use them to access the pointers). Note though that while the allocation process is slower than creating a local variable on the stack, access is just as fast.
3) This is architecture- and operating-system-dependend. Usually, it comes from the RAM until some threshold is reached, then, if you have a virtual memory system, it will start to page out unused memory block to the disk to free up some RAM which can then be allocated to your program. In that respect the memory ''comes from the disk'' as the paging process is sluggish compared to memory access.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Dynamically allocating memory to the free store allows you to create a variable in one function and return a pointer to that variable/struct/class.
If you don't dynamically allocate the memory, when the program leaves the function, the variable will be erased automatically. However, with dynamic allocation to the free store, the variable will stay in the free store until you use the delete command.
Example:
I hope that this clears things up. And I also hope I didn't make any mistakes in the code. The idea is definitely right, hopefully no typos.
Feel free to email me at NYYanks432@hotmail.com if you have any questions
[edited by - gamedev135 on August 6, 2002 3:03:13 PM]
If you don't dynamically allocate the memory, when the program leaves the function, the variable will be erased automatically. However, with dynamic allocation to the free store, the variable will stay in the free store until you use the delete command.
Example:
int * foo();int * barr();int main() {int *p;p = foo(); /*p now points to empty space, so the variable that was created in foo no longer exists */cout << *p << endl; /* I have no idea what this will output. Either junk or it will cause a segmentation fault.int *q = barr();/*q now points to a variable which has a value of 5 */cout << *q << endl; // outputs 5delete q; // remember to delete q since it was dynamically allocated}int * foo() { int a = 0; // a is created and stored on the stack int *p = a; return p; // a is now deleted from the stack. p points to nothing}int * barr() { int *p = new int(); // a variable is allocated to the free store *p = 5; // set p to 5 return p; // variable remains on the free store. p points to it}
I hope that this clears things up. And I also hope I didn't make any mistakes in the code. The idea is definitely right, hopefully no typos.
Feel free to email me at NYYanks432@hotmail.com if you have any questions
[edited by - gamedev135 on August 6, 2002 3:03:13 PM]
Feel free to email me at NYYanks432@hotmail.com if you have any questions
1. exit() works, although you will want to activate your destructors to prevent memory leaks and even then you never know (it is better to let the program run its course). Also if you want to be REALLY clumsy you could use goto() (NOT recommended)
2.I don''t know.
3.RAM (it would be pointless saving to the hard drive and having to reload every time the variable is needed). If you wish to keep a variable to use later (and after the system is shut down) you need to use file I/O.
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
August 07, 2002 06:25 AM
After calling exit(), the destructors for globals ARE automatically called. Objects allocated with new won''t be deleted though.
That''s why you shouldn''t call exit() in a destructor, you can get infinite recursion.
That''s why you shouldn''t call exit() in a destructor, you can get infinite recursion.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement