ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The following code was taken out of a book but gets a seg fault:
Code:
//time.h
#include <cstdlib>
#include <stdio.h>
struct time {
long secs;
void set_time(char *);
void get_time(char *);
};
void time::set_time(char *tod24)
{
long hours, minutes;
minutes=atol(&tod24[2]);//this line doesn't make sence to me
tod24[2] = '\0';neither does this
hours = atol(tod24);
secs = (hours*3600)+(minutes*60);
}
void time::get_time(char *tod)
{
static char *tday[] = {"MIDNIGHT","P.M.","NOON","A.M."};
int hours, minutes, amflag = 0;
hours = secs/3600;
minutes = (secs % 3600)/60;
if (hours == 24) {
hours = 0;
amflag = (minutes == 0) ? 0 : 3;
} else if (hours > 12) {
hours = hours - 12;
amflag = 1;
} else if (hours == 12)
amflag = 2;
else
amflag = 3;
sprintf(tod,"%2d:%02d %s",hours,minutes,tday[amflag]);
}
timetest.c
#include "time.h"
#include <cstdlib>
#include <stdio.h>
int
main()
{
char tstring[40];
time x;
x.secs = 23;
x.set_time("2000");
x.get_time(tstring);
puts(tstring);
return 0;
}
The comment in red above confuses me. Like I said, this is taken directly out of a book. Could someone explain how the comment in red above works or if I'm not alone on seeing it as an error
Notice how it does not compile even though it's, in some sense, the very same code.
PS. And yeah, similarly to what NevemTeve has said, based on the code, I would recommend you stop reading this book and use it for something useful like starting a fire place.
PS. And yeah, similarly to what NevemTeve has said, based on the code, I would recommend you stop reading this book and use it for something useful like starting a fire place.
I understand the programming in this (what it does) but why would I want to do that in this program? Why is it giving me a seg fault?
It depends on the input...
If you provided h:m format (where h is a digit for hours, and m one or more digits for minutes), starting on the third character would convert the minutes. The second part converts the hours, but assumes that the conversion will terminate when it reaches a non digit character (the ":" in my example format).
Unfortunately, it won't work correctly if you provide "h" or "hh" where "h" is a digit of the hours. This is because the third character would be at or beyond the end of the string - and hopefully only causes a segfault, because you are accessing data beyond the end of a buffer.
Looking at the entire program, I suspect the book is fairly old. It should be using snprintf to prevent formatting past the buffer, and the parameter should include the buffer length...
Yes, I have C++ Primer Plus, it is pretty decent. Bjarne's own "The C++ Programming Language" is also good, but very dense and probably not best for a beginner.
I understand the programming in this (what it does) but why would I want to do that in this program? Why is it giving me a seg fault?
The method void time::set_time(char *tod24) modifies the character array tod24[] yet it is called with a string literal x.set_time("2000");. If that literal is rodata then it would cause a seg fault.
One way to avoid this is for set_time() to have its own character array.
eg:
Code:
void time::set_time(char *tod24)
{
long hours, minutes;
char hourString[4];
minutes=atol(&tod24[2]);//this line doesn't make sence to me
/* tod24[2] = '\0';neither does this
hours = atol(tod24); */
hours = atol(strncpy(hourString, tod24, 2));
secs = (hours*3600)+(minutes*60);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.