0

I'm having an unfixable problem at realloc statement in main. Please help me: I'm trying to make a file into a vector of lines.

error: * glibc detected ./a.out: realloc(): invalid next size: 0x085d9018 ** ? thanks.

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

#define LINE_MAX_SIZE 255
#define WORD_LENGTH 24

struct Line
{
    char m_line[LINE_MAX_SIZE+1];
    size_t m_lineNumber;
};

typedef struct Line Line;

struct LineVector
{
    Line * m_lineVector;
    size_t m_size;
};

typedef struct LineVector LineVector;

int getLineFromFile(char * _line, FILE * _file)
{
    char currentChar=fgetc(_file);
    size_t index=0;

    if (currentChar==EOF)
    {
        return -1;
    }

    if (currentChar=='\n')
    {
        return 1;
    }

    while (currentChar!='\n' && currentChar!=EOF)
    {
        *(_line+index)=currentChar;
        ++index;
        currentChar=fgetc(_file);
    }

    if (currentChar==EOF)
    {
        return -1;
    }

    _line[index]='\0';
    return 1;

}

FILE * OpenFile (char * _name)
{
    FILE * filePointer;

    /* open chosen file */
    /*printf("\nPlease write a file location: ");
    scanf("%s", _name);*/
    strcpy(_name,"Ex8.txt");    /*CHANGE*/
    filePointer=fopen(_name, "r");
    if (!filePointer)   /* halts if open fails */
    {
        printf("\nError opening requested file. Program Halted\n");
        exit(1);
    }
    return filePointer;
}

size_t * CreateUINT32Pointer()
{
    size_t * var;

    var=(size_t *) malloc (sizeof(size_t));
    if (!var)
    {
        printf("\nBAD ALLOCATION ERROR\n");
        exit(1);
    }
    *var=0;

    return var;
}

LineVector * CreateLinesVector()
{
    LineVector * lVec;

    lVec=(LineVector *) malloc (sizeof(LineVector));

    return lVec; 
}

void GetFileInputAndCreateArray(LineVector * _linesVec)
{

    return;
}

void PrintVec (LineVector * linesVec)
{
    int i;

    for (i=0; i<linesVec->m_size; ++i);
        printf("\n");
        puts(linesVec->m_lineVector[i].m_line);
}
/*----------------------MAIN----------------------*/

int main ()
{
    LineVector * linesVec;
    char    *currentLine;
    char    fileName[20];
    FILE    *filePointer;
    int     lineCounter=0;
    Line * tmp;

    linesVec=CreateLinesVector();       /* vector of words */
    linesVec->m_size=0;         /* size will count the size of _words */
    linesVec->m_lineVector=(Line *) malloc (sizeof (Line)); 


    filePointer=OpenFile(fileName);     /* open file */

    currentLine=(char *) malloc ((LINE_MAX_SIZE+1)*sizeof (char)); /*create a string for line */
    if (!currentLine)
    {
        printf("\nBAD ALLOCATION ERROR\n");
        exit(1);
    }

    do
    {
        currentLine=fgets(currentLine, LINE_MAX_SIZE, filePointer);
        if (!currentLine)
        {
            break;
        }
        puts(currentLine);
        strcpy(linesVec->m_lineVector[lineCounter].m_line,currentLine);
        ++lineCounter;
        tmp=(Line *) realloc (linesVec->m_lineVector, ((linesVec->m_size)+1) * sizeof (Line));  
        linesVec->m_lineVector=tmp;
        ++linesVec->m_size;
    } while (currentLine!=NULL);

    /* freeing allocated data at the end */ 
    free(currentLine);

    /* close file */
    fclose(filePointer);


    PrintVec(linesVec);

    return 0;
}

1 Answer 1

1

The lineCounter vaiable is broken -- on the first iteration, the vector has one element (because you malloc()); on the second iteration, it has been realloc()ed to one element (0+1) instead of two, and so on.

That being said, take the habit of turning on all compiler warnings -- they immediately warned me about a couple of other bugs in GetLineFromFile() and Printvec()

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

4 Comments

I fully agree with turning on all warning, but to be clear, the root cause (++linesVec->m_size; at wrong line/position) was not pointed out by any compiler warning .
@alk doh! (looks like "doh!" alone isn't a comment)
it seems that doesn't solve the glibc problem. the glibc still exists. Thanks for all helpers though.
@John Yes it does once you make sure that linesvec->m_size == 1 when you enter the loop for the first time; then you'll bump into PrintVec's bug, but that's a different error :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.