1
#define NAME "stackoverflow"
void a()
{
    char *name = NAME;
}

My assumption would be stack. The assignment looked slightly odd to me. Is this a good thing to do?

Laz
  • 6,036
  • 10
  • 41
  • 54
  • 4
    The type of `name` should be `const char*` as string literals are not modifiable. – hmjd Apr 26 '13 at 08:33
  • 2
    Neither the stack nor the heap, see [here][1] [1]: http://stackoverflow.com/questions/7440185/heap-or-stack-when-a-constant-string-is-refered-in-function-call-in-c – Jeff Watkins Apr 26 '13 at 08:35
  • If you think about it, as the stack is a LIFO structure, where would it go on there? Constants are just that, not context sensitive WRT the execution path of the process, so I think it'd be very difficult (i.e. almost impossible) to create stack level constants. – Jeff Watkins Apr 26 '13 at 08:37
  • I think @JeffWatkins nailed it better as duplicate. We should reference that instead. – CppLearner Apr 26 '13 at 08:38
  • Thanks for finding the dup guys. Im assuming its very much a valid thing to do as far I declare the pointer as a const char *. – Laz Apr 26 '13 at 08:46

0 Answers0