Thread: Counting the pointers in a pointer and an invalid read of size 8...

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Counting the pointers in a pointer and an invalid read of size 8...

    Hello All,

    I'm trying to count the number of pointers in a pointer.

    Basically, if we have
    Code:
    int **x;
    then I wanna know how many *x's are in **x.

    I keep getting an invalid read error from valgrind every time I run this code even though it seems to be counting properly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    
    
       int **x = malloc(4*sizeof(*x));
    
    
       for (int i=0; i<4; i++) {
    
    
          x[i] = malloc(4*sizeof(*x[i]));
          *x[i] = 0;
       }
    
    
       int i=0;
    
    
       while(x[i]) {
    
    
          if (x[i]) i++;
       }
    
    
       printf("Total number of pointers, %d\n", i);
    
    
       for (int i=0; i<4; i++)
          free(x[i]);
    
    
       free(x);
    
    
       return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    One problem I have with this code is that it's not clear when this while loop ends.
    Code:
       while(x[i]) {
          if (x[i]) i++;
       }
    The way things are, the loop needs to end on NULL, or some other sentinel value. I could set it up like so:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
      const int xDim = 4;
      const int yDim = xDim;
      int i;
      int **x;
      
      x = malloc((xDim + 1) * sizeof x[0]);
      for (i = 0; i < xDim; i++) {
        x[i] = malloc(yDim * sizeof *x[0]);
        memset(x[i], 0, sizeof *x[0] * yDim);
      }
      x[xDim] = NULL;
    
      
      for (i = 0; x[i]; i++) ;
    
      printf("%d pointers in x.\n", i);
      
      for (i = 0; x[i]; i++) free(x[i]);
      free(x);
      
      return 0;
    }
    C code - 29 lines - codepad
    Last edited by whiteflags; 07-29-2013 at 05:02 PM.

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Ah, I see now. I was hoping that I wouldn't have to construct an extra memory value just to set it to NULL. Oh well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free(): invalid next size (fast)
    By livin in forum C Programming
    Replies: 13
    Last Post: 08-01-2012, 01:56 AM
  2. Valgrind - Invalid read of size 1
    By Castelmagno in forum C Programming
    Replies: 7
    Last Post: 02-29-2012, 03:19 PM
  3. Counting vowels in a string - invalid identifier
    By hencherz in forum C++ Programming
    Replies: 11
    Last Post: 02-25-2012, 05:38 AM
  4. realloc(): invalid size:
    By fxtdr79 in forum C Programming
    Replies: 4
    Last Post: 06-03-2010, 09:30 PM
  5. *** glibc detected *** free(): invalid next size
    By kraghavan in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2010, 07:17 AM