[H-GEN] 'C' code help needed

luke Grant skywalke at ats.com.au
Sat Oct 25 20:51:18 EDT 1997


Ok, i have this "c" assignment due in on tuesday and
i am having trouble with dynamic allocation of memory.
As far as i can see the procedure below should work properly
but when i go to use the command "realloc()" i get told that
the memory alocation has failed.

Yes i know that the code is messy so i dont need any smart ass
remarks about putting it in heaps of smaller procedures. It was
broken up but i shoved it all in one procedure so that i could focus
on the dynamic allocation problem and not have to worry about pointers.

I know that the question is not quite unix related but i figured 
you guys would know what the problem was if anyone did.



this is the structure used. It is a global variable.
/*------------------------------------------------------------*/
struct elements    /*global variable declaration*/
{
  char element[4];
  int weight;
};
  
static struct elements *elemdata;/*set struct pointer*/


/*----------------------------------------------------*/ 



*******************************************
This is the procedure in question
********************************************



void memstruct(void)
/*PRE: Struct pointer "elemdata" has been declared.
  
  POST: Structures have been read from file 
        "atomwgh2.dat" and memory has been allocated
        to struct "elemdata" acordingly.
*/
{          
 int index=0;
 int size=2;
 /*initialise integer*/       
 
 FILE *datafile;
  /*set the file pointer*/
  
  
  datafile = fopen("atomwgh2.dat", "r"); 
  /*open the file atomwght.dat*/
  
  
  
  if (datafile == (FILE *) NULL) 
  /*check file exists*/
    {
      printf("\n Failed to open the file named atomwght.dat. \n");
      exit(1);
    };
    
  
 
 
 /*size = the return from called function 'count()'*/
 
 printf("size = %d\n",size);
 
 elemdata = (struct elements *) malloc(size * sizeof(struct elements));
 /*allocate memory for array of structure*/
 
 
 while (index > -1)
   {
    if (fscanf(datafile,"%s",elemdata[index].element) == EOF)
      {
       elemdata[index].element[0]='\0';
       index = -1;    /*set variable for exit of loop*/
       printf("reached end of file\n");
       }
      else   
       {  
        fscanf(datafile,"%d",elemdata[index].weight);
        printf("scanned in struct number %d\n",dummy);
        size++;
        elemdata=(struct elements *) realloc(elemdata, size * sizeof(struct
elements));
                         /*add extra memory for string*/
 
        if (elemdata == (struct elements *) NULL) 
           /*error message for no memory*/
          {
           printf("Failed to allocate extra memory!\n");
           exit(1);
          }
        printf("successfuly alocated extra memory\n");
        dummy++;
        maxnum++; /*increment variables for loop*/
       }
     }
 
 
 
 fclose(datafile);
 printf("I got this far\n");
 
}

********************************************

Thanks a lot.
Luke Grant.
*****************************************************************************
Word of the day
================
Lapidate, v. Stone to Death. When your mother's well meaning but 
             interventionist crony Mrs. Planterbox remarks, for
             the fourteenth time, what a pitty it is that your
             children spend all their time reading instead of
             having a real hobby, you explain that they used to
             take an interest in lapidation but have difficulty
             in finding a suitable subject. Perhaps she might
             find the time to oblige.
================
E-mail:  skywalke at ats.com.au
         n2143968 at student.fit.qut.edu.au

Home Page:  http://www.ats.com.au/~skywalke
***************************************************************************** 

----------------------- HUMBUG General List --------------------------------
echo "unsubscribe general" | mail majordomo at humbug.org.au # To Unsubscribe



More information about the General mailing list