user1335175
user1335175

Reputation: 189

C segmentation fault when dynamically allocating 2d array

I am trying to write a c program, which will take an input from Linux pipe and puts each word up to the point when it encounters \n into arrays. Then it outputs the words. It works when array contain less than 3 \n but it gives me segmentation error if there is 3 \n`s in the input. Here is the code that I wrote, please help. I am new to programming,so please try to live the code as intact as possible.

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

int main()
{
  int out=0;
  int in=0;
  char **arr2D;
  int i;
  int flag = 1;
  arr2D=(char**)malloc(out*sizeof(char*));
  for(i=0;i<out;i++)
    arr2D[i]=(char*)malloc(in*sizeof(char*));

  while (!feof(stdin))
    {
      in = in +1;
      arr2D[out]=(char*)realloc(arr2D[out],(in)*sizeof(char*));
      scanf("%c",&arr2D[out][i]);

      i=i+1;
      if(arr2D[out][i-1]=='\n')
        {
        out=out+1;
        arr2D=(char**)realloc(arr2D,out*sizeof(char*));
        i=0;
        in=0;
        }
    }

  int out2=0;
  int in2=0;
  do
    {
      do
        {
          printf("%c",arr2D[out2][in2]);
          in2++;
        }
      while(in2<=in-1);
      out2++;
      in2=0;
    }
  while(out2<=out);

  printf("\n");
  return 0;
}

Upvotes: 1

Views: 142

Answers (4)

user1335175
user1335175

Reputation: 189

Parham`s method seems to work, Also I found another way, altough Im not sure if there is something wrong in doing this, but it works.

Evey time when I am allocating or reallocating, I multiply the *sizeof(char) buy a buffer size which is a big number.

Upvotes: 0

Parham Alvani
Parham Alvani

Reputation: 2440

Following code work correctly:

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


int main(int argc, char *argv[])
{
    int out = 1;
    int in = 1;
    char **arr2D;
    int i = 0;

    arr2D = malloc(out * sizeof(char *));
    for (i = 0; i < out; i++)
          /* IMPORTANT */
          arr2D[i] = malloc(in * sizeof(char));

    while (!feof(stdin)) {

        scanf("%c", &arr2D[out - 1][in - 1]);

        in++;
        if (arr2D[out - 1][in - 2] == '\n') {
            arr2D[out - 1][in - 2] = '\0';
            out++;
            arr2D = realloc(arr2D, out * sizeof(char *));
            /* IMPORTANT */
            arr2D[out - 1] = NULL;
            in = 1;
        }

        /* IMPORTANT */
        arr2D[out - 1] = realloc(arr2D[out - 1],in * sizeof(char));


    }

    int out2 = 0;
    do {
        printf("%s\n", arr2D[out2++]);
    } while(out2 < out);
    printf("\n");
    return 0;
}

Upvotes: 2

First you allocate a buffer for out elements, but then you proceed to access arr2D[out] which is out of bounds in the first line with realloc(). Remember that indexing of an array int arr[size] in C is always from 0 to size-1, arr[size] is never ok.

For reference: the allocation line is the first malloc() call:

arr2D=(char**)malloc(out*sizeof(char*));

and the line with the first undefined behavior is the second line within the while() loop:

arr2D[out]=(char*)realloc(arr2D[out],(in)*sizeof(char*));

within the first while() loop. That is the point that invokes undefined behavior, and after which anything is allowed to happen.

Upvotes: 0

Ye Olde Programmer
Ye Olde Programmer

Reputation: 1

You haven't properly initialized the variable i within your while loop.

int i;
scanf("%c",&arr2D[out][i]);

Upvotes: 0

Related Questions