-1

I'm trying to read from a file and put pointers of each line to a double pointer "stack" but always fail to realloc it. Maybe I should use a triple pointer????

int i = 1;
char * line = malloc(BUFSIZ);
char ** stack;
stack = (char ** ) malloc(sizeof(char * ));

while (fgets(line, BUFSIZ, file) != NULL) {
    stack = & line;
    printf("//%s", line);
    printf("??%s", * stack);
    i++;
    stack = (char ** ) realloc(stack, sizeof(char * ) * i);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
JJK ZH
  • 21
  • 5
  • How do you know it fails? – klutt Jan 08 '20 at 08:50
  • 2
    What do you think this: `stack=&line;` does to your (re)allocated pointer? – Adrian Mole Jan 08 '20 at 08:53
  • @JJK ZH The provided code does not make sense. It has a memory leak and this statement stack=&line; does not allocate memory. – Vlad from Moscow Jan 08 '20 at 08:54
  • @Vlad I *think* (but obviously don't *know*) that OP is trying to use it to copy data from `line` to "somewhere in the stack?" – Adrian Mole Jan 08 '20 at 08:55
  • 1
    You really need to read a tutorial on allocation and pointers. I started writing an answer, but this code is full of flaws. Also, [Don't cast malloc](https://stackoverflow.com/q/605845/6699433) – klutt Jan 08 '20 at 08:55

2 Answers2

1

You get this error message because you set stack to the address of line. As a consequence, stack is no more an allocated block.

There are a few errors in your code.

  • you should store the line in the stack at index i
  • you should allocate a new block for each line
  • you should reallocate for i+1 lines to create room for the next line if any

Here is the corrected code:

int i = 0; // number of line pointers in the stack
char *line = malloc(BUFSIZ);
char **stack = malloc(sizeof(char*));
while( fgets(line, BUFSIZ, file) != NULL) {
    stack[i] = line;
    printf("//%s", line);
    printf("??%s", stack[i]);
    i++;
    line = malloc(BUFSIZ); // allocate a new block for the next line
    stack = realloc(stack, sizeof(char*)*(i+1)); // make room for the next line
}
chmike
  • 20,922
  • 21
  • 83
  • 106
1

For starters the code has a memory leak. At first a memory was allocated

stack = (char ** ) malloc(sizeof(char * ));

and then the pointer stack was reassigned with the address of the pointer line.

while (fgets(line, BUFSIZ, file) != NULL) {

stack = & line;
//…

This statement

stack = (char ** ) realloc(stack, sizeof(char * ) * i);

results in undefined behavior because the pointer stack after the statement

stack = & line;

does not point to a dynamically allocated memory. It points to a local variable line.

It seems what you are trying to do is the following shown in the demonstrative program below. Only instead of a file there is used the standard input stream stdin.

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

int main(void) 
{
    char *line = malloc( BUFSIZ );
    char **stack = NULL;
    size_t n = 0;

    while ( fgets( line,BUFSIZ, stdin ) != NULL )
    {
        line[strcspn( line, "\n" )] = '\0';

        char **tmp = realloc( stack, ( n + 1 )* sizeof( char * ) );

        if ( tmp != NULL )
        {
            stack = tmp;
            ++n;

            stack[n-1] = malloc( BUFSIZ );

            if ( stack[n-1] != NULL ) strcpy( stack[n-1], line );
        }
    }

    for ( size_t i = 0; i < n; i++ )
    {
        if ( stack[i] != NULL ) puts( stack[i] );
    }

    for ( size_t i = 0; i < n; i++ )
    {
        free( stack[i] );
    }
    free( stack );

    free( line );

    return 0;
}

If to enter two strings

Hello
World

then the output will be

Hello
World
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335