i was writing my other program and kept getting segmentation fault. i finally found out it was because i made a function with return type string. so i just wrote the following simple code to test it and it turns out it was b/c of a function with string return type that was causing the segmentation fault.
MY QUESTION IS WHY DID A SIMPLE CODE LIKE BELOW CAUSE A SEGMENTATION FAULT? WHAT IS IT ABOUT THE STRING CLASS THAT CAUSES THIS? if i changed the function to int, it would work without a segmentation fault, so why won't it work with strings?
1. Ok, why the hell do you need seven header fils? You only use two of them, iostream and string, what are the other five doing?
2. Why would you call a function "dude?" The whole point of naming variables, procedures, etc. is to make it easy to remember what they're for, and easy to understand what they do.
3. Why doesn't "dude" return anything?
4. Where is the return statement and closing brace for the main() function?
Before you edit your post again, calling a function "foo" or "bar" except in example code holds the death penalty.
@chrisname,
1) this is not about style or anything, ignore those several preprocessor directives... i was justing testing why a string function was giving me a segmentation fault.
this is b/c i was writing a bigger program in which a function with string return type was giving a segfault and i wanted to see if it was true. with this code, i see that it was because of that function with return type string that i was getting a segmentation fault but i want to know why
2) look at 1 for answer
3) look at 1 for answer
4) yea it got cut off when i copy and pasted the code. the return 0; and closing brace was in my pgrogram code though. i just fixed it here.
like i said the point here is not my style or why have all those #include, this is just test code.
the point is why is the function with string return type causing a segmentation fault? if i change it to return type int, it works perfectly, so why not with string?
'dude' isn't returning anything. It's supposed to be returning a string.
If it doesn't return anything, make it a void. Otherwise, actually return something. Your compiler should be at least giving you a warning about this.
1 2 3 4 5 6
string dude(int e)
{
cout << e;
return""; // actually return something
// no more segfault
}
The reason for this is because since you don't specify a return value, the compiler has to fill one in. For ints this isn't as bad because if it returns a garbage int it doesn't matter, but a complex object like a string has a dtor that will be called and memory that will be freed, so it ends up deleting a bad pointer and/or doing other nasty stuff.
AHHH thanks man, great explanation, just the answer i needed :D. i was going to ask how come it worked for ints and i did not return anything BUT YOU ANSWERED THAT TOO :D. this also explains why when i tried the following code it worked, i didn't why but now i know, thanks!:
1 2
string dude(string me)
{return me;}
so this means that i should always try to return something for functions? even if its ints or doubles, this way i don't get garbage or potential garbage? or i should just return something only sometimes?
Before you edit your post again, calling a function "foo" or "bar" except in example code holds the death penalty.
Does foo bar baz hold any history, I know foo means forward operations officer and Bar is (establishment), a retail establishment that serves alcoholic beverages, whats baz.