?
Solved

segmentation fault with strftime() in client/server program

Posted on 2003-03-18
10
Medium Priority
?
780 Views
Last Modified: 2012-06-21
I'm making a client/server program for my networks class.  In the server.c file, I'm running a loop so that when one client is done and logs out, another can log back in.  Each client asks for the date and time...i'm doing this using the strftime() function.  However, only the first client can get the date and time, then after it logs out if another client calls strftime() I get a segmentation fault.  Is there any way around this? thanks

chad

here's my loop:

       do{
           memset(s2, '\0', sizeof(s2));
           memset(s1, '\0', sizeof(s1));
           read(sd_current, getCom, sizeof(s1));
           time_t t = time(NULL);
           ctime(&t);
           struct tm *tm=localtime(&t);

           if(strcmp(getCom, "date")==0){
    /*fault*/ strftime(sendCom, sizeof(s2), "%a %b %d %Y", tm);
              write(sd_current, sendCom, sizeof(s2));
              memset(s2, '\0', sizeof(s2));
              memset(s1, '\0', sizeof(s1));
           }
           if(strcmp(getCom, "time")==0){
    /*fault*/ strftime(sendCom, sizeof(s2), "%I:%M:%S", tm);
              write(sd_current, sendCom, sizeof(s2));
              memset(s2, '\0', sizeof(s2));
              memset(s1, '\0', sizeof(s1));
           }
        }while(strcmp(getCom, "quit") < 0 || strcmp(getCom, "quit") > 0);

0
Comment
Question by:chadyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8162464
you write:

strftime(sendCom, sizeof(s2)...

How big is sendCom?  If it is bigger than s2, that could be where your problem lies...

you should write:

strftime(sendCom, sizeof(sendCom)...

or

strftime(s2, sizeof(s2)...

0
 
LVL 6

Expert Comment

by:gj62
ID: 8162474
by the way, you never store anything in s2, so setting the memory to 0 all the time is a waste of instructions...
0
 

Author Comment

by:chadyo
ID: 8163018
sorry i should have specified this before, sendCom is a pointer to s2, i'm using it as the buffer.

char s2[100];
char *sendCom=s2;

I don't think its anything with the size of the char array.  The date and time aren't that big.  Plus it works the first time through and the messages after that are the same length, just different times.

Thanks for your help though.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:chadyo
ID: 8163152
sorry i should have specified this before, sendCom is a pointer to s2, i'm using it as the buffer.

char s2[100];
char *sendCom=s2;

I don't think its anything with the size of the char array.  The date and time aren't that big.  Plus it works the first time through and the messages after that are the same length, just different times.

Thanks for your help though.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 8164488
Segmentation fault generally occus when you try to reference a memory location which is now free or deallocated. For example, if you are returning a pointer from a function, and let's say that the pointer pointed to a local variable of the function, then after the function execution finishes, the memory allocated for all its local variables is freed and the address returned is now free, not allocated. So effectively, the pointer in the callling function which held the address returned by this called function, is now pointing to garbage.

Since you have not provided your entire code, it is not possible to point out where it is occuring, but can you o through your code and see if the above information helps you to correct your problem.

Mayank.
0
 
LVL 22

Expert Comment

by:grg99
ID: 8166277
What I would do is to write a dummy strftime() that does as little as possible,  like just a strcpy( arg, "foo" );

That will tell us if the problem is truly in styrftime or wether some other pointer is getting messed up.


My guess is: since your pointer "strcom" is declared right after the array it points to, if you somehow overflow your array by even one byte, it's going to zap the pointer, leading to a seg fault the next time you use it.

I would put a "long int MayGetZapped= 0x12345678;"

Between the two variables, then check it with:

#define CheckVars   if( MayGetZapped != 0x12345678 )printf("OOPS!" );

Sprinkle "CheckVars" throughout your code and wait for the OOPS! to appear.

0
 
LVL 2

Assisted Solution

by:honey_hamster
honey_hamster earned 100 total points
ID: 8167733
I changed your source to use the console instead of the read/write socket interface you had, and the code ran fine, so there's nothing inherently wrong in what you're trying to do.  I suggest you ensure that sendCom == s2 before the calls to strftime() to ensure that the sendCom value is valid.

#include <stdio.h>
#include <time.h>

int main( void )
{
  char s1[100];
  char *getCom = s1;
  char s2[100];
  char *sendCom = s2;
 

  do{
          memset(s2, '\0', sizeof(s2));
          memset(s1, '\0', sizeof(s1));
          printf("\n Enter String ");
          gets(getCom);
          time_t t = time(NULL);
          ctime(&t);
          struct tm *tm=localtime(&t);

          if(strcmp(getCom, "date")==0){
   /*fault*/ strftime(sendCom, sizeof(s2), "%a %b %d %Y", tm);
             printf( "\n%s\n", sendCom );
             memset(s2, '\0', sizeof(s2));
             memset(s1, '\0', sizeof(s1));
          }
          if(strcmp(getCom, "time")==0){
   /*fault*/ strftime(sendCom, sizeof(s2), "%I:%M:%S", tm);
             printf( "\n%s\n", sendCom );
             memset(s2, '\0', sizeof(s2));
             memset(s1, '\0', sizeof(s1));
          }
       }while(strcmp(getCom, "quit") < 0 || strcmp(getCom, "quit") > 0);
}

0
 
LVL 20

Expert Comment

by:jmcg
ID: 10144633
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between mayankeagle and honey_hamster.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10144785
Please proceed with that recommendation.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question