LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   programming-segmentation fault(sprintf) (https://www.linuxquestions.org/questions/linux-general-1/programming-segmentation-fault-sprintf-394806/)

ramakrishna sura 12-20-2005 03:52 AM

programming-segmentation fault(sprintf)
 
Hi everybody,
Iam new to linux. I was doing networking programming. There was a program in 6th chapter of unix network programming by richard stevens that prints host info using gethostbyname() function.I typed the as it is and compiled and ran it.
It is printing info correctly if given host is there but giving segmentation fault if given host is not there.

the sprintf() function of host_err_string() (page 730 of richard stevens) is giving that error. why?

regards
Ramakrishna sura

timmeke 12-20-2005 04:12 AM

It's probably giving a segmentation fault because you're trying to print a variable
that hasn't been initialized properly (that isn't defined).
In doing so, it accesses (accidentally) a piece of memory that belongs to a different program,
causing the segmentation fault.

Simply said, check the code. See what gets printed and check if it is always defined (always has a value).
I don't have the Richard Stevens book, so I can't really help you any further.


All times are GMT -5. The time now is 08:52 AM.