0

something like 20 hours have passed and i still dont manange to get what is the problem.

First of all, there is the main function with these parameters.

  char ***matrix;
  int line, maxCollumn, i, j;
  int lineData[100];

Then i am calling the function

PutFirstAndLast(&matrix, &line, &maxCollumn, lineData);

This function is intended to dynamicly allocate my matrix and to return its number of lines and collumns. lineData is used to store how many items i have on each line because later in the function i will have to fill the "empty" spaces with "".

My troubling function has this header

int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int *lineArray)

In this function i will generate a new matrix called in a semi-retarded way "array", i will allocate memory for the new "array" in orded to put each word from a file "in.txt" in it.

int PutFirstAndLast(char**** matrixPointer, int *line2, int *maxCollumn2, int     *lineArray)
 {
FILE *inputFile;
char buffer[100], *p;
char ***array;
int i, j, collumn, maxCollumn = 0, line = -1;




inputFile = fopen("in.txt", "r");

while (fgets(buffer, 100, inputFile))
{

    line++;
    collumn = 0;


    if (line == 0)
        array = (char***)malloc(sizeof(char**));
    else
        array = (char***)realloc(array, sizeof(char**)* (line + 1) );




    p = strtok(buffer, " \n");


    while (p)
    {

             //taking each word and allocating a space for it

        if (collumn == 0)
            array[line] = (char**)malloc(sizeof(char*));
        else
            array[line] = (char**)realloc(array[line], sizeof(char*)*  (collumn + 1));


        array[line][collumn] = (char*)malloc(sizeof(char)* strlen(p) + 1);


        strcpy(array[line][collumn], p);

        collumn++;
        p = strtok(NULL, " \n");
    }


          // Here i count what is the maximum numbers of items on a line


    lineArray[line] = collumn;
    if (collumn > maxCollumn)
        maxCollumn = collumn;



}


    // Here i add "" in the "empty spaces.
for (i = 0; i < line + 1; i++)
{
    for (j = lineArray[i]; j < maxCollumn; j++)
    {
        array[i] = (char**)realloc(array[i], sizeof(char*)* maxCollumn);
        array[i][j] = (char*)malloc(sizeof(char)* 10);
        strcpy(array[i][j], "");
    }

}

fclose(inputFile);


*matrixPointer = array;
*maxCollumn2 = maxCollumn;
*line2 = line;

return 0;
 }

Problem is that when i come to main and i do this

for (i = 0; i < line + 1; i++)
{
    for (j = 0; j < maxCollumn; j++)
    {
        printf("%s", matrix[i][j]);
        matrix[i][j] = (char*)realloc(matrix, sizeof(char)* 11);
    }

}

for item matrix[2][0] there is an error "Acces violation reading location".

bur if i do

for (i = 0; i < line + 1; i++)
    {
        for (j = 0; j < maxCollumn; j++)
        {
            printf("%s", matrix[i][j]);
        //  matrix[i][j] = (char*)realloc(matrix, sizeof(char)* 11);
        }

    }

there is no problem and all my words from the matrix are shown on the screen\

my model of text is:

etesta este astru   
de mext pentru bun 
examen multa programare  

by adding "" in "empty" spaces i refer to : puttin the "" in the arraylike this:

etesta   este      astru         
de       mext      pentru        bun 
examen   multa     programare    
sorin.va
  • 41
  • 5
  • Check that first argument to your `realloc` call that fails. You're not reallocating the correct pointer. Also, you should be careful assigning back to the same pointer you reallocate, what if `reallocate` fails? Then you loose the original pointer. – Some programmer dude Jan 28 '14 at 08:00
  • Yeah but the problem is that i cant figuire what would be a propper reallocation in this case? I go checking for a fail right now. – sorin.va Jan 28 '14 at 08:03
  • 1
    Shouldn't you be reallocating `matrix[i][j]` instead of `matrix`? – Some programmer dude Jan 28 '14 at 08:04
  • idea is that when i read the matrix in the main function every word i put in it also appears on the screen, would that happen if reallocate failed? – sorin.va Jan 28 '14 at 08:05
  • 1
    You also don't need the first malloc at all. just make sure `array` is originally NULL before going into this. I'm still staring at the dereferences (thats a lot of stars, and frankly being [a 3-star programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer) is not usual. – WhozCraig Jan 28 '14 at 08:05
  • Please make code easy to read for us: Don't have all those empty lines, and indent properly! It looks rather messy now. (Oh, don't use TABs.) BTW, nicely looking code helps keeping your mind clear. – meaning-matters Jan 28 '14 at 08:06
  • @WhozCraig yes, correct, next timpe i will put char ***array = NULL; but still problem not solved :d – sorin.va Jan 28 '14 at 08:07
  • @meaning-matters i tryed my best, big tabs logicly separate the actions. i hope :) – sorin.va Jan 28 '14 at 08:08
  • Where does your debugger proclaim the access violation is rooted? When it breaks you should be staring at a specific line or lines of code, and a call-stack tell you how you got there. – WhozCraig Jan 28 '14 at 08:09
  • If `realloc` fails it will return `NULL`, but will not free the data pointed to by the first argument. That means if you have e.g. `ptr = realloc(ptr, some_size)` and the call fails, then `ptr` will now be `NULL` and you no longer have access to the previously allocated data. – Some programmer dude Jan 28 '14 at 08:09
  • @WhozCraig in main at matrix[2][0]. the word "examen" in my example – sorin.va Jan 28 '14 at 08:12
  • @Joachim Pileborg. i did what u said, i put a condition if malloc or realloc fails to return 1 but the function still return 0 wich means there was no fail in realloc or malloc. – sorin.va Jan 28 '14 at 08:18
  • So how is `lineArray` passed in to this function? Can you post a simplified call-invoking `main()` that exhibits the problem? Just curious how you know how big to make `lineArray` on the *caller* side, as it needs to be the number of lines in the input file wide, which is still unknown until this function is finished. – WhozCraig Jan 28 '14 at 08:21
  • 1
    @JoachimPileborg "Shouldn't you be reallocating matrix[i][j] instead of matrix?" this is the correct answer. You should make it one. – Per Johansson Jan 28 '14 at 08:21
  • @WhozCraig simply i dont, i just declare int lineData[100]; – sorin.va Jan 28 '14 at 08:23
  • @PerJohansson well everytime i read a new line from my file i should allocate for that line the spaces used by an array of strings (char* *). But everytime i read a new line shouldnt i also allocate for my array the space for it to hold another array of characters? thats why everytime i read a new line i do array = (* * * array)realloc(array, sizeof(char * *) * lineCounter); – sorin.va Jan 28 '14 at 08:26
  • @PerJohansson I'll certainly up-vote it if he does. – WhozCraig Jan 28 '14 at 08:26
  • @sorin.va its not that. Look at that `realloc()`. you're throwing out the base `matrix` pointer and all it surveys, then saving the resulting realloc to a double-indrection (`[i][j]`) off a pointer that is no longer valid. That should be `matrix[i][j] = realloc(matrix[i][j], ...`, not `matrix[i][j] = realloc(matrix, ...`. (and as was said before , it really should go through a `tmp` first, but thats a different issue). – WhozCraig Jan 28 '14 at 08:27
  • @WhozCraig OH MY GOD what stupid i was to make a mistake like that. its incredilbe how im looking at this peace of code since like 20 hours and couldnt see that mistake. Thats it, you are right, ty guys, make it an answer so we can upvote – sorin.va Jan 28 '14 at 08:34
  • @JoachimPileborg c'mon sir. step into it. You now have three people waiting to pull the trigger on your answer in-comment if posted as a solution. =P – WhozCraig Jan 28 '14 at 08:37
  • @WhozCraig untill now i really thout that going trough a temp first was a waste of time. you guys managed to change my opinion. thx – sorin.va Jan 28 '14 at 08:38
  • @WhozCraig Oh well, It seems I couldn't handle the group pressure! :) – Some programmer dude Jan 28 '14 at 08:39
  • @WhozCraig ty for yor help, i will remember :d – sorin.va Jan 28 '14 at 08:48
  • @Joachim Pileborg Nice sharp eye, thank you – sorin.va Jan 28 '14 at 08:48

1 Answers1

5

You call the realloc function with the matrix pointer when it should be matrix[i][j]:

matrix[i][j] = realloc(matrix[i][j], sizeof(char)* 11);

Or, if you want to protect yourself against failures:

char *temp = realloc(matrix[i][j], sizeof(char)* 11);
if (temp != NULL)
    matrix[i][j] = temp;

Note: Don't cast the return of malloc (or realloc) in C.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621