Passing a const value

This is a discussion on Passing a const value within the C++ Programming forums, part of the General Programming Boards category; If I pass a constant value into a function is the memory allocated for the variable on the stack or ...

  1. #1

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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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
    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

    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.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-29-2008, 01:47 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08: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, 08:08 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 11:32 AM

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