0

I'm having trouble using string arrays. When creating the string array, I can print the data of e.g. globals[0], but at the end of the function the application crashes when doing the same thing. Does anyone know what causes this?

#define TRUE    1
#define FALSE   0

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

char** globals; // Naam van alle globals
int* adressen; // Adres van alle globals
unsigned int index; // Plaats voor het toevoegen van globals

int InitializeGlobals(char* path)
{
    // Variabelen voor het bestand
    struct stat st;
    FILE* bestand;

    // Variabelen voor de regels in op te slaan
    char* buffer;

    // Variabelen voor strings te tellen
    unsigned int i;
    unsigned int aantal = 0;
    unsigned char b = FALSE;

    // Variabelen voor het omzetten van de buffer
    char* number;
    unsigned int grootte;
    unsigned int start;
    unsigned int tmp;

    // Debug variabelen
    int debug;

    // Bestand in de buffer lezen en sluiten
    //
    //
    bestand = fopen(path, "r");
    if (bestand == NULL) {
        printf("Kon het opgegeven bestand niet openen! globals.c/r42\n");
        return -1;
    }

    debug = stat(path, &st);
    if (debug < 0) {
        printf("Kon het opgegeven bestand niet analyzeren! globals.c/r48, return: %i\n", debug);
        return -2;
    }

    buffer = (char*)malloc(st.st_size);
    if (buffer == NULL) {
        return -3;
    }

    fread(buffer, 1, st.st_size, bestand);
    fclose(bestand);

    // Het aantal strings vinden en de arrays klaarmaken
    //
    //
    for (i = 0; i < (unsigned int)st.st_size; i++) {
        if (buffer[i] == '\n' && b == FALSE) {
            aantal++;
        }
        else {
            b = FALSE;
        }
    }

    globals = (char**)malloc(sizeof(char*)*aantal);
    adressen = (int*)malloc(sizeof(int*)*aantal);

    // Buffer omzetten naar de string array "globals" en de int array "adressen"
    //
    //
    b = FALSE;
    index = 0;
    start = 0;
    for (i = 0; i < (unsigned int)st.st_size; i++) {
        if (b == TRUE) {
            if (buffer[i] == '\n') {
                b = FALSE;
                start = i+1;
            }
        }
        else if (buffer[i] == ';') {
            b = TRUE;
        }
        else if (buffer[i] == '=') {
            grootte = (i-start);
            number = (char*)malloc(grootte);
            if (number == NULL) {
                return i+1;
            }
            memcpy(number, buffer+start, grootte);
            start = i+1;
            tmp = atoi(number);
            memcpy(&adressen[index], &tmp, 4); // application is x86 only
            index++;
            free(number);
        }
        else if (buffer[i] == '\n') {
            grootte = (i-start);
            globals[index] = (char*)malloc(grootte+1);
            if (globals[index] == NULL) {
                return i+1;
            }
            memcpy(globals[index], buffer+start, grootte); 
            globals[index][grootte] = '\0';
            start = i+1;
            printf("%s\n", globals[index]);
        }
    }

    free(buffer);
    printf("%s", globals[0]); // <-- crash

    return 0;
}
5
  • learn to use the debugger (gdb on Linux) and compile with all warnings and debug info (gcc -Wall -g on Linux). Use a leak detector (valgrind on Linux). Commented Mar 1, 2013 at 17:14
  • 2
    Why don't you mention the exception text? Commented Mar 1, 2013 at 17:14
  • Unhandled exception at 0x614616B3 (msvcr110d.dll) in app.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD. Commented Mar 1, 2013 at 17:16
  • wtb a nullchar terminator (or several in this case). Commented Mar 1, 2013 at 17:19
  • 1
    You mustn't cast the return value of malloc. Commented Mar 1, 2013 at 17:19

2 Answers 2

1

This 0xCDCDCDCD address the debug runtime's way of marking uninitialized heap memory. So it's safe to assume that globals[0] was never initialized.

Hypothesizing how that might happen:

If your input file is empty, or if it has one line of text that line doesn't end with a newline, then you'll never allocate globals[0].

Sign up to request clarification or add additional context in comments.

Comments

0

Could you check the input file?

In the code you are expecting output at globals[0] but getting an crash.

Looking at the code, index variable is updated when the input character is '=' or '\n'. Check if the text file contains '=' before '\n'. In this case the index would be incremented and globals[] would never have memory allocated at index 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.