C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-11-2004, 04:39 PM   #1
Registered User
 
Join Date: Oct 2004
Posts: 25
Variable Storing in memory

In C++ you have three places where you can store variables:

* 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;
}
//-----------------------
The variable myvar is destroyed at the end of the function so the the pointer will be pointing to a non-existing locations. This might work as expected, my not work or might just exit giving you a memory violating error because you're triying to read protected memory. This is one of the worst errors a programmer can make, because as I said, it might work some times and he/she won't notice it until it becomes a huge problem and has to spend hours debugging the application.
__________________
Blog: http://karb0noxyde.blogspot.com

Last edited by karb0noxyde; 10-11-2004 at 09:39 PM.
karb0noxyde is offline   Reply With Quote
Old 10-11-2004, 06:14 PM   #2
Lead Moderator
 
kermi3's Avatar
 
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

If you're new to the boards, welcome and reading this will help you get started.
Information on code tags may be found here

- Sandlot is the highest form of sport.
kermi3 is offline   Reply With Quote
Old 10-11-2004, 06:24 PM   #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   Reply With Quote
Old 10-11-2004, 06:43 PM   #4
Stinking it up.
 
StinkyRyan's Avatar
 
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   Reply With Quote
Old 10-11-2004, 06:57 PM   #5
Guest
 
Sebastiani's Avatar
 
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:
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 function where they're defined and are destroyed when the function ends.
stack variables are created and destroyed from whatever scope they exist in – that could be a for loop, a function, or an anonymous scope, etc, ie:


Code:
void foo(void) {
 int a;
  { // an anonymous scope
  int b;
  } // ‘b’ is destroyed 
 return;
 }

Quote:
And finally, heap variables. This variables are stored on plain RAM and thats why they're the slowest (considering the speed of registers and the stack)
both stack and heap variables live in RAM, so access times are comparable.
Sebastiani is offline   Reply With Quote
Old 10-11-2004, 07:01 PM   #6
Registered User
 
Join Date: Oct 2004
Posts: 25
true
.
.
.
.
and
.
.
.
.
true

, gonna edit those

thanks
__________________
Blog: http://karb0noxyde.blogspot.com
karb0noxyde is offline   Reply With Quote
Old 10-11-2004, 07:19 PM   #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   Reply With Quote
Old 10-11-2004, 07:31 PM   #8
Registered User
 
Join Date: Oct 2004
Posts: 25
Quote:
Originally Posted by DougDbug
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.
for the first thing, yes, thats why I said that you could use it at very low level, and i already fixed the second thing
__________________
Blog: http://karb0noxyde.blogspot.com
karb0noxyde is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:02 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22