Variable Storing in memory

This is a discussion on Variable Storing in memory within the C++ Programming forums, part of the General Programming Boards category; In C++ you have three places where you can store variables: * Register * Stack * Heap Each of them ...

  1. #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.
    Last edited by karb0noxyde; 10-11-2004 at 10:39 PM.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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.

  3. #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?

  4. #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.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.


    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;
     }

    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    true
    .
    .
    .
    .
    and
    .
    .
    .
    .
    true

    , gonna edit those

    thanks

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

  8. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. How to Restart or Clean all Variable in memory?
    By sergioms in forum C Programming
    Replies: 5
    Last Post: 01-08-2009, 01:59 PM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  5. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 05:14 PM

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