Thread: Passing a const value

  1. #1
    Jeremy
    Guest

    Passing a const value

    If I pass a constant value into a function is the memory allocated for the variable on the stack or heap? There is an example bellow.

    void foo( const int value )
    {
    }

    What i mean is, Is value declaired on the heap or stack? I am thinking the only values stored in the heap are pointers but I'm not sure about constants...
    Thanks
    Jeremy

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void foo( const int value )
    The const in this context just means that foo will not modify the parameter. Nothing changes in the way functions are called.

    This is especially important when you start passing pointers, and you want to stop the function from unintentionally modifying something outside it's immediate scope.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jeremy
    Guest
    Thanks for the reply, but is the memory for "value" declaired on the stack or the heap??? I think it's allocated on the stack, but I can't find any documentation on this.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156

    Passing a const value

    It is on the stack. Allocating memory from the heap requires the use of the new operator.

    The const is a modifier that tells the compiler that the data type is constant ( not modifiable ). Const has nothing to do with where the memory is allocate from.


    dang

  5. #5
    Jeremy
    Guest
    Thanks, That was what I was thinking but wasn't positive....
    Thanks for the help...
    Jeremy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-29-2008, 12:47 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Replies: 6
    Last Post: 12-06-2005, 09:23 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM