Hey guys, had a quick question for you. Suppose you had a function such as:
char * hullo()
{
char * h = "hullo";
return h;
}
int main()
{
char * wee = hullo();
delete wee; <-- Segmentation fault
return 0;
}
I wouldn't really want to do anything like this, I'd rather pass in a pointer to the hullo function. However, if you were to...
String "hullo" should reside in data section, the memory is allocated when the program loaded and never and needn't be free, because it has only 1 copy during the program execution.
You can try to modify the string:
wee[1] = '\0';
you should receive a SEGFAULT because it in data section and readonly.
If you look at the address of "hullo", you should find that it is 0x08xxxxxx, very ...
1) Every new must be matched with a delete (and new[]/delete[], and malloc/free).
2) If there is no new/new[]/malloc/calloc, then there is no need to explicitly free anything.
ah I see, so it was never on the heap in the first place? I was under the impression declaring a string like this:
char * hullo = "hello";
called malloc, but I guess not , just gave it a try malloc'ing the string and this time no seg fault. Thanks guys
Quote:
Originally Posted by pi3orama
String "hullo" should reside in data section,
if it should, where does it usually reside then?
Quote:
the memory is allocated when the program loaded and never and needn't be free,
I assume that is to ensure the application you ...
ah I see, so it was never on the heap in the first place? I was under the impression declaring a string like this: char * hullo = "hello"; called malloc, but I guess not , just gave it a try malloc'ing the string and this time no seg fault. Thanks guys
String "hullo" should reside in data section, the memory is allocated when the program loaded and never and needn't be free, because it has only 1 copy during the program execution. You can try to modify the string: wee[1] = '\0'; you should receive a SEGFAULT because it in data section and readonly. If you look at the address of "hullo", you should find that it is 0x08xxxxxx, very close the...
1) Every new must be matched with a delete (and new[]/delete[], and malloc/free). 2) If there is no new/new[]/malloc/calloc, then there is no need to explicitly free anything.
Quote: Originally Posted by pi3orama String "hullo" should reside in data section, if it should, where does it usually reside then? Quote: the memory is allocated when the program loaded and never and needn't be free, I assume that is to ensure the application you made hogs all the system resource, correct? I mean, why share, right? Quote: because it has only 1 copy during the program execution. so that this string...
Related threads on "CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions":
Ati e codec video: Segmentation fault (soluzione):... Ati e codec video: Segmentation fault (soluzione): problema particolare su driver ati Segmentation fault http://tinyurl.com/6maqkq
about a year ago
from
twitterfeed
Thread profile page for "Segmentation Fault" on http://www.codeguru.com.
This report page is a snippet summary view from a single thread "Segmentation Fault", located on the Message Board at http://www.codeguru.com.
This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity