getting error Segmentation fault (core dumped) when trying to pass a string to a function

Laksith

When I try to pass the current time as a string to the function logger in my code I get an error saying that "Segmentation fault (core dumped)". but when I put static char str[30] the error doesn't come. but with no errors the resulting file makes impossible to open it.

void logger(char * logType, int loggingLevel, char * massage)
{
    FILE *fp = fopen("log.txt", "a");
    fprintf(fp, "%s|%d|%s|%s",logType,loggingLevel,massage,currentTime());
    fclose(fp);   
}

char * currentTime(void)
{
    time_t rawtime;
    char str[30];
    char *string;
    struct tm * timeInfo;
    time(&rawtime);
    timeInfo = localtime(&rawtime);
    strftime(str, 26, "%Y:%m:%d %H:%M:%S", timeInfo);
    return str;

}

so previously I made the currentTime function like this

char * currentTime(void) {
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    return asctime(timeinfo);
}

it works fine but that is not the way I need to display the time.

Mohit Jain

You are returning a local variable from function currentTime() which is undefined behaviour.

Change function signature to: char * currentTime(char *inputBuffer, size_t bufLen)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting "Segmentation fault Core Dumped Error " while reversing a string

From Dev

Getting error in C "Segmentation fault (core dumped)"

From Dev

I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++

From Dev

Segmentation fault error (core dumped)

From Dev

Segmentation fault and core dumped when trying to declare a big array

From Dev

segmentation fault (core dumped) when changing string characters

From Dev

segmentation fault (core dumped) when changing string characters

From Dev

llvm pass segmentation fault:(Core dumped)

From Dev

Trying to read file which contains ints and store in int vector, but I keep getting "Segmentation fault (core dumped)" error

From Dev

string,print, {segmentation fault (core dumped)}

From Dev

Runtime Error: Segmentation fault (core dumped)

From Dev

Assembly/Nasm - Segmentation fault (core dumped) error

From Dev

segmentation fault(core dumped) error message

From Dev

Segmentation fault (core dumped) error with strcpy() (suspected)

From Dev

Runtime Error: Segmentation fault (core dumped)

From Dev

segmentation fault: core dumped error in C

From Dev

nam error : Segmentation fault (core dumped)

From Dev

C Programming Segmentation fault (core dumped) error

From Dev

C -Segmentation fault (core dumped) error

From Dev

C program Error - Segmentation fault (core dumped)

From Dev

Segmentation fault (core dumped) when making compliation

From Dev

Segmentation fault (core dumped)

From Dev

Segmentation fault (core dumped)

From Dev

Deleted my /usr/ directory, recovered with chroot now getting a Segmentation fault (core dumped) trying to run Sudo

From Dev

I don't understand why I am getting "Segmentation fault (core dumped)" error

From Dev

getting Segmentation fault (core dumped) when using ffmpeg and specifing start time

From Dev

How to avoid getting segmentation core dumped error?

From Dev

Segmentation fault core dumped in C while trying to create memory for a Struct

From Dev

Getting segmentation fault (core dumped) and don't understand why

Related Related

  1. 1

    Getting "Segmentation fault Core Dumped Error " while reversing a string

  2. 2

    Getting error in C "Segmentation fault (core dumped)"

  3. 3

    I'm getting a segmentation fault (core dumped) error when trying to add an Node to the end of a Linked List in c++

  4. 4

    Segmentation fault error (core dumped)

  5. 5

    Segmentation fault and core dumped when trying to declare a big array

  6. 6

    segmentation fault (core dumped) when changing string characters

  7. 7

    segmentation fault (core dumped) when changing string characters

  8. 8

    llvm pass segmentation fault:(Core dumped)

  9. 9

    Trying to read file which contains ints and store in int vector, but I keep getting "Segmentation fault (core dumped)" error

  10. 10

    string,print, {segmentation fault (core dumped)}

  11. 11

    Runtime Error: Segmentation fault (core dumped)

  12. 12

    Assembly/Nasm - Segmentation fault (core dumped) error

  13. 13

    segmentation fault(core dumped) error message

  14. 14

    Segmentation fault (core dumped) error with strcpy() (suspected)

  15. 15

    Runtime Error: Segmentation fault (core dumped)

  16. 16

    segmentation fault: core dumped error in C

  17. 17

    nam error : Segmentation fault (core dumped)

  18. 18

    C Programming Segmentation fault (core dumped) error

  19. 19

    C -Segmentation fault (core dumped) error

  20. 20

    C program Error - Segmentation fault (core dumped)

  21. 21

    Segmentation fault (core dumped) when making compliation

  22. 22

    Segmentation fault (core dumped)

  23. 23

    Segmentation fault (core dumped)

  24. 24

    Deleted my /usr/ directory, recovered with chroot now getting a Segmentation fault (core dumped) trying to run Sudo

  25. 25

    I don't understand why I am getting "Segmentation fault (core dumped)" error

  26. 26

    getting Segmentation fault (core dumped) when using ffmpeg and specifing start time

  27. 27

    How to avoid getting segmentation core dumped error?

  28. 28

    Segmentation fault core dumped in C while trying to create memory for a Struct

  29. 29

    Getting segmentation fault (core dumped) and don't understand why

HotTag

Archive