compiling program / segmentation fault

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 908 Views - Last Post: 30 November 2017 - 04:52 PM Rate Topic: -----

#16 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 101
  • View blog
  • Posts: 373
  • Joined: 09-September 16

Re: compiling program / segmentation fault

Posted 30 November 2017 - 02:15 PM

After I changed the openInfile to void it cleanly compiles on VS 2015 and runs, though the change function has some flaws.
Was This Post Helpful? 0
  • +
  • -

#17 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: compiling program / segmentation fault

Posted 30 November 2017 - 04:17 PM

View PostThomas1965, on 30 November 2017 - 01:09 PM, said:

Your openInfile function doesn't have a return statement. Not sure if this could cause a segmentation fault but you should correct it, probably declaring openInfile as void.

No return statement in a function returning a value invokes undefined behavior -- so yes, it can cause a seg fault.

Since the function openInfile has no return statement (and there seems to be no reason for it to return any value) there's no doubt that it should be declared void.

I see another problem: an ofstream outs is declared in main, and another one (with the same name) is declared in openInfile. These are not both the same stream. If you want to use outs in multiple functions you have to pass the same outs around from function to function.

Maybe I'm missing something, but you don't seem to actually use the ofstream anyplace, but if you do this is going to cause trouble. But why does openInfile call openOutfile anyway? Wouldn't it be better to call it from main?
Was This Post Helpful? 0
  • +
  • -

#18 stalktiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-November 17

Re: compiling program / segmentation fault

Posted 30 November 2017 - 04:52 PM

Thanks all !
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2