Segmentation fault in struct

C

crystal twix

Hello. I am having trouble debugging my code. I have a struct and a
function to compute the time difference entered in HH:MM:SS format.
My code is:

const int hourConv = 3600; // used to get total hours from total
seconds
const int minConv = 60;


struct MyTime {
int hours, minutes, seconds;
};

MyTime *determineElapsedTime(const MyTime *time1, const MyTime *time2)
{
long timeOneSec = time1->hours*hourConv + time1->minutes*minConv +
time1->seconds;
long timeTwoSec = time2->hours*hourConv + time2->minutes*minConv +
time2->seconds;
long ans = timeTwoSec - timeOneSec;
cout << ans;
MyTime *timeDiff;
timeDiff->hours = ans / hourConv;
timeDiff->minutes = ans % hourConv / minConv;
timeDiff->seconds = ans % hourConv % minConv;

return timeDiff;
}

I believe the problem to be with the 2nd to last line: "timeDiff-
seconds = ans%hourConv%minConv;" since when i comment that line out,
I do not get a segmentation fault error. But I don't understand why
that line is invalid. Any help would be appreciated. Thanks!
 
T

thomas

Hello.  I am having trouble debugging my code.  I have a struct and a
function to compute the time difference entered in HH:MM:SS format.
My code is:

const int hourConv = 3600; // used to get total hours from total
seconds
const int minConv = 60;

struct MyTime {
    int hours, minutes, seconds;

};

MyTime *determineElapsedTime(const MyTime *time1, const MyTime *time2)
{
        long timeOneSec = time1->hours*hourConv + time1->minutes*minConv +
time1->seconds;
        long timeTwoSec = time2->hours*hourConv + time2->minutes*minConv +
time2->seconds;
        long ans = timeTwoSec - timeOneSec;
        cout << ans;

So far so good. But I don't know why you prefer not to check if time1,
time2 are null.
If either is null, the -> operator will cause disasters.
        MyTime *timeDiff;
        timeDiff->hours = ans / hourConv;
        timeDiff->minutes = ans % hourConv / minConv;
        timeDiff->seconds = ans % hourConv % minConv;

        return timeDiff;
You forgot to allocate memory for timeDiff. Declaring a MyTime pointer
doesn't mean memory is automatically allocated.
Remember to free the memory somewhere.
 
I

Ian Collins

crystal said:
Hello. I am having trouble debugging my code. I have a struct and a
function to compute the time difference entered in HH:MM:SS format.
My code is:

const int hourConv = 3600; // used to get total hours from total
seconds
const int minConv = 60;


struct MyTime {
int hours, minutes, seconds;
};

MyTime *determineElapsedTime(const MyTime *time1, const MyTime *time2)
{
long timeOneSec = time1->hours*hourConv + time1->minutes*minConv +
time1->seconds;
long timeTwoSec = time2->hours*hourConv + time2->minutes*minConv +
time2->seconds;
long ans = timeTwoSec - timeOneSec;
cout << ans;
MyTime *timeDiff;

thomas has already pointed out the missing allocation, but it would be
much easier if you just returned by value. You are probably better off
passing the parameters by reference:

MyTime determineElapsedTime( const MyTime& time1, const MyTime& time2 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top