Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with dynamically allocating arrays.

Status
Not open for further replies.

bodhi

Programmer
Jan 3, 2002
67
GB
I'm writing a data parsing utility and am coming unstuck (spectacularly) when I try to parse a keyword set.

I have a struct as follows for a keyword group :

Code:
 // Keyword Bin :
 struct KEYWORDBIN
  { INT keywordindex;
    CHAR name[MAX_STR_SIZE];
    BOOL iscasesensitive;
    COLOUR colour;
    CHAR *keywords[];
  };

In my main parse fnt I do :

Code:
KEYWORDBIN *kwd = new KEYWORDBIN;
if (getkeywords(fp,kwd->keywords) == 0)
   return FALSE;

The function getkeywords is implemented as follows :

Code:
INT DATACOLLECTOR::getkeywords (FILE *fp, CHAR *keywords[])
 { CHAR *keygroupdelims[2] = {"/K","/EOL"};
   CHAR delims[2] = {' ','\n'};
   CHAR *lptr = NULL;
   INT j = 0;
   BOOL go = TRUE;

   // Get the first line and set the pointer :
   fgets(linerecord,MAX_LINE_LENGTH,fp);
   lptr = linerecord;

   // Check if we go:
   for (INT i=0; i<2; ++i)
    { if (strstr(linerecord, keygroupdelims[i]) != NULL)
         go = FALSE;
    }

   while (go)
    { // Scan and lift all keywords on the line :
      do
       { CHAR *newkwd = new CHAR[MAX_KWD_LENGTH];
         getdelimitedstr(&lptr,newkwd,delims,MAX_KWD_LENGTH);
         keywords[j++] = newkwd;
       } while (skipspaces(&lptr) && *lptr != '\n');

      // Get the next line and reset the pointer :
      if (!feof(fp))
       { fgets(linerecord,MAX_LINE_LENGTH,fp);
         // Check if we go:
         for (INT i=0; i<2; ++i)
          { if (strstr(linerecord, keygroupdelims[i]) != NULL)
               go = FALSE;
          }
         lptr = &linerecord[0];
       }
      else
         go = FALSE;
    }
   return j-1;
 }

This falls apart when allocating new the third time (the third line of the keyword group). I assume I am doing something silly or corrupting the heap in some way but just cant see it and am now in zombie zone with this one. Is what I am passing through correct ? Any help at all would be appreciated..

Many Thx.
 
Your code is written in C++
new is the way memory is allocated in C++, and
DATACOLLECTOR::getkeywords denotes a class member function.

What are all those pseudo-types like INT, CHAR?
Use int and char if that's what you mean.

The green text for code may look pretty, but it's pretty unreadable from where I'm sitting.

--
 
Fair enough - I thought it made things clearer personally.

I typedef all my standard types into caps - again because for me it makes things more transparent (personal preference). The code is a snippet which I copy and pasted.

If it makes a difference I have exactly the same problem if I strip the class and allocate the memory using malloc.

Appreciate your looking anyways.
 
Well your best bet is to strip out as much code as possible (from a copy) which doesn't affect the problem and post what remains here as a complete program which we can examine / compile / run.

If during this process you remove some code which does memory allocation, and this part starts working, consider the possibility that the bit you just removed is actually the cause of the problem.

Don't forget a sample data file to go with it.

> The code is a snippet which I copy and pasted.
Yeah, most people end up posting the code which shows the problem. Unfortunately, the real cause of the problem is usually elsewhere when it comes to problems of memory allocation.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top