John
John

Reputation: 1

realloc: glibc error doesn't run

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;
}

Upvotes: 0

Views: 63

Answers (1)

loreb
loreb

Reputation: 1357

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()

Upvotes: 1

Related Questions