![]() |
| | #1 |
| Registered User Join Date: Oct 2004
Posts: 25
| Variable Storing in memory * Register * Stack * Heap Each of them have their own advantages and disadvantages which I'll cover here. Nowadays the register is not often used (correct me if I'm wrong) , but when you can really use it (at very low level), it's the best place to store small variables that you will use a lot and that need to be fast, that is because the registers are directly managed by the processor, it doesn't have to access the RAM so it's very fast, however, you won't be able to store data that is too big and you have a limited amount of registers. When you create a variable in a function and don't add anything else but the type, the variable is stored in the stack (this type of variables are called auto (ex: auto int myint = 0), and is the default for all local variables). Think of the stack as a pile of papers, you can read the top of the pile, you can trash the top of the pile, you can move the top paper and read the next one, etc. So, the stack is relatevly fast and can be used for most things without any problems, and in most cases, it's best way of storing variables, they are created and initialized when the program gets to the block where they're defined and are destroyed when the block ends. However, as everything, it has it's limitations, for example, you can't have too many (or too big) auto variables because you'll overload the stack, which isn't good hehe. And finally, heap variables. This variables are stored on plain RAM, the good thing is that you have a lot space (well this depends on the ammount of RAM available). This type of variables are created with the operator new and destroyed with delete or free, they don't die when the function ends which can make memory leaking problems, but it's the best place for storing big variables and actively controlling their lifetime. Missuses of the types of variables: When you make a function that returns a pointer, for example: Code: //-----------------------
char * myfunc( const char some_arg )
{
char myvar[] = "Some string";
return myvar;
}
//-----------------------
__________________ Blog: http://karb0noxyde.blogspot.com Last edited by karb0noxyde; 10-11-2004 at 09:39 PM. |
| karb0noxyde is offline | |
| | #2 |
| Lead Moderator Join Date: Aug 1998
Posts: 2,568
| I'm not sure what kind of response you hope to get from this, but before posting further I suggest you read the board's guidelines and homework policy. |
| kermi3 is offline | |
| | #3 |
| Registered User Join Date: Oct 2004
Posts: 25
| its just a tutorial, i wrote is so it could go to the FAQ... what is wrong?
__________________ Blog: http://karb0noxyde.blogspot.com |
| karb0noxyde is offline | |
| | #4 |
| Stinking it up. Join Date: Jun 2004
Posts: 61
| sweet, I remember when all forums had this, random users would always submit their own tutorials, never really seen it here though, maybe a board should be made for it or something?
__________________ Because I can't. |
| StinkyRyan is offline | |
| | #5 | ||
| Guest Join Date: Aug 2001
Posts: 4,923
| well, no harm done – it just seems strange that you’d post a ‘tutorial thread’ when none was requested! at any rate, there are some inaccuracies in the information posted, so I don’t think it would make it to the FAQ anyway. ![]() Quote:
Code: void foo(void) {
int a;
{ // an anonymous scope
int b;
} // ‘b’ is destroyed
return;
}
Quote:
| ||
| Sebastiani is offline | |
| | #6 |
| Registered User Join Date: Oct 2004
Posts: 25
| true . . . . and . . . . true , gonna edit thosethanks
__________________ Blog: http://karb0noxyde.blogspot.com |
| karb0noxyde is offline | |
| | #7 |
| Hardware Engineer Join Date: Sep 2001
Posts: 1,397
| register - If a variable is really stored in a the processor's register, it can be accessed faster. The processor doesn't have to fetch the variable's address as part of the instruction, and you're not limited by memory speed. But, with a multitasking operating system, it's very likely that your variable would have to be swapped-out of any particular register when the processor switches to another task. So, your compiler may either ignore the register statement, or it may use a virtual register.... a 'secret' place in RAM, or in it's cache. In any case, it's supposed to make the compiler aware that you want fast access. I don't think that stack-variables are accessed any faster than heap-variables. C++ stack-variables are NOT accessed as a LIFO (Last-In-First-Out) stack. They have RAM addresses, and are accessed exactly like heap-variables... directly via their address (which is abstracted as a variable name). This is quite different from a traditional stack, where you read the "top sheet of paper" first! Variables "on the stack" ARE created and distroyed in LIFO sequence. |
| DougDbug is offline | |
| | #8 | |
| Registered User Join Date: Oct 2004
Posts: 25
| Quote:
__________________ Blog: http://karb0noxyde.blogspot.com | |
| karb0noxyde is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting number | Leslie | C Programming | 8 | 05-20-2009 04:23 AM |
| How to Restart or Clean all Variable in memory? | sergioms | C Programming | 5 | 01-08-2009 01:59 PM |
| Assignment Operator, Memory and Scope | SevenThunders | C++ Programming | 47 | 03-31-2008 06:22 AM |
| Manipulating the Windows Clipboard | Johno | Windows Programming | 2 | 10-01-2002 09:37 AM |
| Memory handler | Dr. Bebop | C Programming | 7 | 09-15-2002 04:14 PM |