Thread: threads and struct

  1. #1
    Registered User
    Join Date
    Mar 2009

    threads and struct

    This is my whole program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <getopt.h>
    #include <dirent.h>
    #include <pthread.h>
    char* program_name;
    char program_path[PATH_MAX + 1];
    struct Foo {
            FILE *file_content;
            char* character;
    // funkcia pre vypis HELPu //
    void print_help(FILE *stream, int exit_code) {
            // help goes here //
    void GetCurrentPath(char* buffer) {
            getcwd(buffer, (PATH_MAX + 1));
    int my_assert(int exp, char* msg, int do_abort, char* program_name, int line_number) {
            if ( !(exp) ) {
                    fprintf(stderr, "Error in directory %s, in file %s, in program %s, on line c.%d\n", program_path, __FILE__, program_name, line_number);
                    fprintf(stderr, "Popis chyby: %s\n\n", msg);
                    if ( do_abort ) {
                    return 1;
            } else {
                    return 0;
    void* count_character(void* arg) {
            struct Foo *f = (struct Foo*) arg;
            const int string_length = 10;
            char buffer[string_length];
            int i=0, character_count = 0;
            while( fgets(buffer, string_length, f->file_content) != NULL ) {
                    for ( i=0; i<string_length; i++ ) {
                            if ( buffer[i] == '\0' ) {
                            } else {
                                    if ( buffer[i] == *f->character ) {
            return (void*) character_count;
    int main(int argc, char *argv[]) {
            FILE *file_content;
            program_name = argv[0];
            int next_option;
            struct Foo *f;
            pthread_t thread;
            if (system("cls")) {
            const char* const short_options = "h";
            const struct option long_options[] = {
                    {"help", 0, NULL, 'h'},
                    {NULL, 0, NULL, 0}
            do {
                    next_option = getopt_long(argc, argv, short_options, long_options, NULL);
                    switch (next_option) {
                            // program pre argument h/help //
                            case 'h':
                                    print_help(stdout, 0);
                            case '?':
                                    fprintf(stderr, "Unknown argument\n\n");
                                    print_help(stderr, 1);
                            case -1:
                                    if (argc == 3) {
                                            file_content = fopen(argv[1], "r");
                                            if (my_assert((file_content != NULL), "Cannot open file.", 0, program_name, __LINE__)) {
                                                    printf("Cannot open file %s\n", argv[1]);
                                            f->file_content = file_content;
                                            f->character = argv[2];
                                            int result;
                                            int tc = pthread_create(&thread, NULL, &count_character, (void*) &f);
                                            if (my_assert((tc == 0), "Thread does not create.", 0, program_name, __LINE__)) {
                                                    printf("Thread does not create\n");
                                            pthread_join(thread, (void*) &result);
                                            fprintf(stdout, "Result is: '%d'\n", result);
                                    } else {
                                            fprintf(stderr, "Error: argument count '%d'\n\n", argc-1);
                                            print_help(stderr, 1);
                                    fprintf(stderr, "Unexpected error\n");
            while (next_option != -1);
            return (0);
    And result is segmentation fault. I really dont know where is the problem. If I try to echo file content in count_character function it gives me some characters there wasn't in file.

    First argument must be path to text file and second argument is the character which is comparing with file content.

    Program must have 3 arguments but it doesn't for now. Third argument is number of threads that will be comparing character with file content and this count increase global variable which is total count of characters found in file content.

    Thaks for help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Quote Originally Posted by Martin Kovac View Post
    And result is segmentation fault. I really dont know where is the problem. If I try to echo file content in count_character function it gives me some characters there wasn't in file.
    Any time you see this, it generally means you're dealing with strings, and you're not terminating them correctly. Remember, in order for you to treat it like a string, it needs to be null terminated. Otherwise, string functions will merrily run off the end of your block of memory until they happen across one, or until your program crashes.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Quzah is right. But the biggest question that comes in to my mind is... Why on earth are you messing with a thread in this app??? You just create one, and then if creation succeeds, you'll join it???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing Struct Objects to Threads
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 04-18-2009, 06:25 AM
  2. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  3. "dereferencing pointer to incomplete type"
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 11-01-2005, 09:50 AM
  4. Sharing a C struct
    By rotis23 in forum Linux Programming
    Replies: 3
    Last Post: 02-22-2004, 03:47 PM
  5. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM