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?
Asked
Active
Viewed 1,536 times
-2
-
2Research makes you a better programmer. – Gunner Dec 10 '14 at 02:57
-
1On the stack, yo. hence why recursion can eat up your whole stack, thus causing a stack overflow... – Gillespie Dec 10 '14 at 02:58
-
1Google is your friend. – pmverma Dec 10 '14 at 02:59
-
Side note: you'll save yourself a lot of grief if you stop thinking of C and C++ as the same language. – Andrew Barber Dec 10 '14 at 03: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. – Captain Obvlious Dec 10 '14 at 03:01
-
@paxdiablo I thought I was reading pure gold, but then I came to the end! Dangit! – Andrew Barber Dec 10 '14 at 03:07
-
I don't think the linked duplicate is a duplicate. None of the answers to that question answer this question and the duplicate it is a duplicate of is about undefined behaviour and not about memory segments at all.. – Jerry Jeremiah Dec 09 '21 at 23:51
1 Answers
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 :-)

paxdiablo
- 854,327
- 234
- 1,573
- 1,953