Thread: Dynamic Array

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Northern Va
    Posts
    18

    Arrow Dynamic Array

    Hi. I'm working with dynamic arrays to construct a histogram. I've got an array of pointers to my histogram struct and I try to malloc() and then realloc() the array as necessary. Here's some of the code:
    Code:
       typedef struct{
          double val;
          int freq;
          }hPoint;
    ...
    ...
    ...
    
    hPoint **momentBin;
    momentBin = (void *)malloc(sizeof(hPoint *) * 10);int maxIndex =10;
    
    ...
    
    found=0;
             for(k=0;k<maxIndex;k++)
                {
                if(momentBin[k]==NULL)
                   break;
                test =(momentBin[k])->val;
                if (test==bN)
                   {
                   found =1;
                   (momentBin[k])->freq++;
                   break;
                   };
                
                }
             if(found ==0)
                {
                probCounter++;
                //increase the size of the array
                if (probCounter>=maxIndex)
                   {
                   hPoint **newArr = realloc(momentBin, maxIndex*2);
                   if (newArr==NULL)
                      {
                      printf("Reallocation Error");exit(0);
                      }
                   else momentBin = newArr;
                   };
                hPoint *temp =(hPoint *) malloc(sizeof(hPoint));
                temp->val=bN;
                temp->freq=1;
                momentBin[probCounter] = temp;
    This would be great, except when I loop through my array, there aren't any structs. Your help is greatly appreciated.
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "there aren't any structs"?

    Also: do you change maxIndex after you realloc?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Northern Va
    Posts
    18
    I have a looping statement to read through all of the array:
    Code:
    for(j=0;j<maxIndex;j++)
             {
             if(momentBin[j]==NULL)
                break;
             test =(momentBin[j])->val;
             printf("%lf\n",test);
             }
    printf("Last J: %d",j);
    I changed the max index thing to update, but when I run, it still reads j=0

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by firetheGlazer View Post
    I have a looping statement to read through all of the array:
    Code:
    for(j=0;j<maxIndex;j++)
             {
             if(momentBin[j]==NULL)
                break;
             test =(momentBin[j])->val;
             printf("%lf\n",test);
             }
    printf("Last J: %d",j);
    I changed the max index thing to update, but when I run, it still reads j=0
    Just because you didn't put anything in 0, doesn't mean you didn't put anything in 1, or 4, or 11, or .... IOW: use continue instead of break.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Location
    Northern Va
    Posts
    18
    Wow, okay thanks for the catch. When I put in a continue, GDB spit out my assignment line with a segfault:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0x00401758 in main () at Z:\VortexStuff\Moment\moment.c:169
    169	         test =(momentBin[j])->val;
    I thought that my null test would have caught that. Any thoughts?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM