-2

Global variables are stored in data, dynamically allocated variables are stored in heap, etc. Where are functions (such as main, other functions in main.c, printf, etc) stored? Is it in the code segment?

7
  • 2
    Research makes you a better programmer.
    – Gunner
    Commented Dec 10, 2014 at 2:57
  • 1
    On the stack, yo. hence why recursion can eat up your whole stack, thus causing a stack overflow...
    – Gillespie
    Commented Dec 10, 2014 at 2:58
  • 1
    Google is your friend.
    – pmverma
    Commented Dec 10, 2014 at 2:59
  • Side note: you'll save yourself a lot of grief if you stop thinking of C and C++ as the same language. Commented Dec 10, 2014 at 3:00
  • Neither C nor C++ have any concept of segments, this is an implementation detail of the tool chain and there is no standard that specifies where they are stored. Commented Dec 10, 2014 at 3:01

1 Answer 1

1

This is not a C or C++ question. Neither of those ISO standards mandate anything like how different things are allocated to memory segments.

In fact, the whole idea of segments is irrelevant to the standards since they dictate the behaviour of a C or C++ "virtual machine" - they tend to dictate behaviour rather than how the behaviour is achieved.

However, if you're in an implementation that has a code segment, that's the most likely place where code would be stored :-)

1
  • This is typically called the "text segment".
    – Barmar
    Commented Dec 9, 2021 at 23:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.