Frage

I'm running in to a segmentation fault on the line enclosed with **. I pipe this output: |status: OK| |version: 0.85| |author: PBrooks| |nitems: 0|

in to my code below and it gives me a segmentation fault after I print out the '\n' in my printf statement. I don't know how to debug this though.. Does anyone have a clue?

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


char string[300];

struct NameValue {
    char *name;
    char *value;
};

struct NameValue *pairs;

void ReadStdin(int argc, char *argv) {
    int x;

    fread(string, sizeof (char), 300, stdin);
    printf("%s\n", string);

}

void ParseInput() {
    int x, num = 0; //figure out how many i need 
    for (x = 0; x < 300; x++) {
        if (string[x] == '|') {
            num++;
        }
    }
    num /= 2; //num = how many i need 
    pairs = (malloc(num)); //allocate the array 
    int pipe = 0, i, j = 0, tempCounter = 0;
    char tempName[50], tempValue[50];
    printf("%lu \n", sizeof (string) / sizeof (string[0]));
    if (pairs != 0) {
        for (i = 0; i <= num; i++) { //counts pairs 
            printf("i = %i\n", i);
            printf("j = %i, pipe: %i \n", j, pipe);
            if (string[j] == '|') {
                printf("there's a pipe\n");
                pipe++;
                j++;
            }

            while (string[j] != ':') {
                printf("counter for main string: %i\n tempCounter: %i\n", j, tempCounter);
                tempName[tempCounter] = string[j];
                tempCounter++;
                j++;
                if (string[j] == ':') {
                    tempName[tempCounter] = '\0';
                    tempCounter = 0;
                    **printf("~~~~tempName\n is: %s", tempName);**
                    break;
                }
            }
            while (string[j] != '|') {
                j++;
                tempValue[tempCounter] = string[j];
                tempCounter++;
                if (string[j] == '|') {
                    tempValue[tempCounter] = '\0';
                    tempCounter = 0;
                    strcpy(pairs[i].value, tempValue);
                    pipe++;
                    break;
                }
            }
        }
    }
}

int main(int argc, char *argv) {
    ReadStdin(argc, argv);
    ParseInput();

    return (EXIT_SUCCESS);
}

edit: Sorry! I removed the null terminate character line by accident. It is in there but it's still giving me the error.

edit: a bit more information: The j variable gets incremented up to 6 and and the temp counter gets incremented up to 5 before the program spits out a segmentation fault.

War es hilfreich?

Lösung 3

I figured it out. Not only did I need to malloc the pairs but I also need to malloc the member variables inside each malloc's pairs.

I ended up using my tempCounter to see how many characters I needed and malloc'd the correct amount for the member variables.

Andere Tipps

You never null terminate the string tempName. Then you try printing it here:

if (string[j] == ':') {
    tempCounter = 0;
    **printf("~~~~tempName\n is: %s", tempName);**
    break;
}

Strings in C are a series of characters in memory, terminated by a null byte (i.e. '\0'). You're copying bytes into a buffer, but you never yourself create this null terminator, hence printf runs off the end and into undefined memory.

Instead, at the end of the while loop, you need to assign the next character in tempName to '\0', probably inside the while loop just before printing it.

Before tempCounter = 0, just put tempName[tempCounter] = '\0';.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top