[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Simple CFile question

Posted on 2002-03-24
6
Medium Priority
?
289 Views
Last Modified: 2013-11-20
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];
      savedFile.Read(tempName,sizeof(char)
           * 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?
0
Comment
Question by:c121hains
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 400 total points
ID: 6892698
Hi,

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.

Cheers
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6892795
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.
0
 
LVL 3

Expert Comment

by:job_s
ID: 6893358
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.

0
 
LVL 4

Expert Comment

by:mblat
ID: 6900659
Running couple assumptions here:

Assuming you data strcuture looks like:

typedef struct
{
int   lengthOfFileNames;
char  filePaths[MAX_PATH];
} STRUCT;

then you declare var of this type like:
STRCUT s[NUM_SAMPLES_ALLOWED];

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];
     memset(tempName,0,sizeof(char)*(lengthOfFileNames[y]+1);
     savedFile.Read(tempName,sizeof(char)*lengthOfFileNames[y]);          

     strcpy(filePaths[y],tempName);
     delete tempName;
  }
}
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question