C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2008, 09:56 AM   #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
firetheGlazer is offline   Reply With Quote
Old 07-11-2008, 10:08 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,831
What do you mean by "there aren't any structs"?

Also: do you change maxIndex after you realloc?
tabstop is offline   Reply With Quote
Old 07-11-2008, 10:35 AM   #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
firetheGlazer is offline   Reply With Quote
Old 07-11-2008, 10:36 AM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,831
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.
tabstop is offline   Reply With Quote
Old 07-11-2008, 11:57 AM   #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?
firetheGlazer is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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