7 Replies - 1854 Views - Last Post: 10 April 2012 - 12:51 PM

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

A note about return statements

Posted 23 March 2012 - 10:20 AM

Hi all,

I thought I'd share a little tidbit I just learned, just so no one else tears their hair out like me in the future. :)

I was writing a program, and I had a function that returned a string. I didn't realize that I'd forgotten the return statement, and unfortunately I wasn't compiling with -Wall, which would have shown me that. Anyway, I was getting a segfault and bad permissions error, like this:

==25217== Process terminating with default action of signal 11 (SIGSEGV)
==25217==  Bad permissions for mapped region at address 0x80F2B61
==25217==    at 0x40B5C1B: std::string::~string() (in /usr/lib/libstdc++.so.6.0.10)
==25217==    by 0x804ACD8: MyCode::run() (MyCode.cpp:219)
==25217==    by 0x804BD28: main (MyCode.cpp:243)



And whenever I looked in the code I wrote, the line numbers corresponded to the method signatures, but no particular line in the method I could debug from.

So what did I learn from this? ALWAYS, ALWAYS check to make sure you have appropriate return statements. If a lack of return statements was an error in g++, rather than a warning, I bet it would save people a lot of debugging headaches.

The takeaway message from this is, if you get segfaults that map back to method signatures, check the return statements first. It'll save you a lot of hassle.

Hope that helps someone!
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: A note about return statements

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: A note about return statements

Posted 23 March 2012 - 11:35 AM

That's why you should always compile with the highest level of warnings/errors, IMO.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: A note about return statements

Posted 23 March 2012 - 01:58 PM

Ya, I'm pretty paranoid about warnings. Turn on everything your IDE will allow or read up on your compiler's options and turn on warnings like those for missing return statements.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: A note about return statements

Posted 23 March 2012 - 03:04 PM

With gcc, there is actually a setting that makes all warnings errors.
Was This Post Helpful? 0
  • +
  • -

#5 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: A note about return statements

Posted 23 March 2012 - 09:03 PM

gcc -Werror hello.c -o hello

Was This Post Helpful? 0
  • +
  • -

#6 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 181
  • Joined: 30-December 09

Re: A note about return statements

Posted 04 April 2012 - 02:56 PM

Usually when I define my function, I'll first write out the return statement and then fill in the rest of the function definition:
int myfunc()
{
// code goes here...
return 0; // I write this line first, then go back and flesh out the rest of the function definition.
}


I don't know if anyone really does this, its just my thing. :)

-Masoug
Was This Post Helpful? 0
  • +
  • -

#7 CodeBreather  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 28
  • Joined: 06-April 12

Re: A note about return statements

Posted 10 April 2012 - 11:48 AM

View Postvividexstance, on 23 March 2012 - 10:04 PM, said:

With gcc, there is actually a setting that makes all warnings errors.


Heres a picture of in Code::Blocks goto project->build options in the top menu bar this is what you should see:

Attached Image

i have all but the pedantic errors ill see the warning and ill fix it from there but some may chose to get them as error, but to each their own.
Was This Post Helpful? 0
  • +
  • -

#8 novacrazy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 117
  • Joined: 01-March 11

Re: A note about return statements

Posted 10 April 2012 - 12:51 PM

View PostCodeBreather, on 10 April 2012 - 12:48 PM, said:

View Postvividexstance, on 23 March 2012 - 10:04 PM, said:

With gcc, there is actually a setting that makes all warnings errors.


Well, in response to the pic and setting every warning message... I have noticed GCC actually fails to compile some things with extra warnings enabled sometimes. Not even when it considers warnings errors, but really where a new error pops up that was never there before. Usually some weird thing deep in the standard or boost libraries that I've been unable to resolve unless I turn off that warning flag. Eh, so sometimes I have to go without warnings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1