Silly problem

This is a discussion on Silly problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> struct test_struct { double **array; int test; }; int main() { struct test_struct kyle; kyle.test ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    452

    Wink Silly problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct test_struct
    {
      double **array;
      int test;
    };
    
    int main()
    {
      struct test_struct kyle;
    
      kyle.test = 18;
      printf("test = %d\n",kyle.test);
      kyle.array = (double **) malloc(5*sizeof(double *));
      if (kyle.array == NULL)
      {
        printf("fail");
      }
      int i = 0;
      for (i = 0; i < 5; i++)
      {
        kyle.array[i] = (double *) malloc(10*sizeof(double));
        if ( kyle.array[i] == NULL)
        {
          printf("fail2");
        }
      }
      
      int j = 0;
      for (i = 0; i < 5; i++)
      {
        for(j = 0; j < 10; j++)
        {
          kyle.array[i][j]=i*j;
        }
      }
      
      for (i = 0; i < 5; i++)
      {
        for(j = 0; j < 10; j++)
        {
          printf("%d\t",(int) kyle.array[i][j]);
        }
        printf("\n");
      }
      
      for (i = 0; i < 5; i++)
      {
        free(kyle.array[i]);
      }
      free(kyle.array);
    }
    Alright, so this code works, and does what it is supposed to (I was just messing around with structs to get a feel for them since I have never really used them before)

    However, if in the part where I free the memory allocated for array, I use j instead of i, I get a segfault. Why would it matter which one of the indices I use, since I initialize it to 0 at the start and hard code the limits?

    ie, if the bolded section above is replaced with
    Code:
      for (j = 0; j < 5; j++)
      {
        free(kyle.array[i]); AHA! where did that i come from... ^_^
      }
      free(kyle.array);
    it segfaults. Now I am sure this is just a trivial little thing I am missing, but I can't seem to see it.


    EDIT: and I just saw it. Ignore me...
    Last edited by KBriggs; 05-06-2010 at 11:34 AM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    In the first loop your are freeing kyle.array[0,1,2,3,4], but in the next one you are freeing kyle.array[0] 5 times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple, silly yet big problem....
    By punk in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2007, 10:25 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21