• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Simple CFile question

When reading a file, I want to pick up a string after a series of reads of different structures.  However after reading in a string, the string that is read in contains garbage after it.  So then I found that after I created a temp character pointer initialized to a size of one and then read it in, it seemed to read the string correctly however it wont let me delete the string after a loop:

char* tempName;
for (y = 0; y < NUM_SAMPLES_ALLOWED; y++)
   if (lengthOfFileNames[y] > 0)
      //When the following is initialized to the actual
      //  size of the string to read, the string is
      //  retrieved but has garbage after it
      //  example:   'c:\myfiles\myfile.doc@@@@@@'
      //  Initializing it to 0 or 1 seems to work for some
      //    unknown reason.  
      tempName = new char[1];
           * lengthOfFileNames[y]);          
      filePaths[y] = tempName;

      //It crashes if I include the following line:
      //delete [] tempName;

So now with this bizzare initialization of size 1 and a no delete line, it works but could someone tell me why??
Do I have a memory leak without that delete [] tempName?
1 Solution

savedFile.Read(tempName,sizeof(char)* lengthOfFileNames[y]);    

If lengthOfFileNames[y] had a value anything other than 1 then the above line will lead memory corruption.
If, say lengthOfFileNames[y] had a value of 100, then you would be reading 100 bytes of data into a memory area only one byte long (tempName), thereby creaming 99 bytes of unsuspecting innocent memory.

Plus you don't seem to be taking any steps to NULL terminate the filenames you read.

Why are you calling new and delete for one byte?
Even though it is technically an array, if you must use delete, remove the braces [].
This my have nothing (really) to do with your problem.
why did u call delete [] U were allocated only one char

if u want to read a string U must allocate the size of bytes in the source string + 1 for the NULL termination.
Before reading the string call ZeroMemory or memset for clearing the buffer. Otherwise delete[] will crash becasue delete[] will look for the NULL char for finding the end of string.

Running couple assumptions here:

Assuming you data strcuture looks like:

typedef struct
int   lengthOfFileNames;
char  filePaths[MAX_PATH];

then you declare var of this type like:

if so, your loop should look like

char* tempName;
for (y = 0; y < NUM_SAMPLES_ALLOWED; y++)
  if (lengthOfFileNames[y] > 0)
     tempName = new char[lengthOfFileNames[y] + 1];

     delete tempName;

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now