Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic char* Allocation

Posted on 1999-06-25
5
Medium Priority
?
277 Views
Last Modified: 2010-04-16
What is wrong with the following code?  It crashes on the delete statement (true condition) every time I run it.

bool ReadWriteString(CFile* fileIn, CStdioFile* fileOut, CString strOut, unsigned short nLength)
{
      UINT nBytesRead;
      char *pch = new char[nLength];

      pch[nLength] = NULL;

      nBytesRead = fileIn->Read(pch, nLength);
                              
      if (nBytesRead == nLength)
      {
            // Write Label
            fileOut->WriteString(strOut + "\n");
            // Write value
            fileOut->WriteString(pch);
            fileOut->WriteString("\n");
            delete [] pch;
            return true;
      }
      else
      {
            delete [] pch;
            return false;
      }
}
0
Comment
Question by:awd
[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
5 Comments
 
LVL 4

Accepted Solution

by:
mandhjo earned 300 total points
ID: 1198473
You need to allocate one more character than you want to read.  EG,

char * pch = new char[nLength + 1];
pch[nLength + 1] = NULL;

I prefer to clear out the whole buffer that was just allocated.  Something like this:

memset(pch, 0, nLength + 1);

this sets the whole buffer to nulls.  That way, if you only read in 5 bytes and your length is 100, you are certain that your string ends with a NULL terminator.

Your nBytesRead == nLength condition ensures that you have read in exactly as many characters as you allocated, thus you don't really know if your buffer ends with a NULL.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1198474
 char *pch = new char[nLength]
allocates nLength characters, usable from
  pch[0]
to
  pch[nLength - 1]

so
  pch[nLength] = 0;

will write in un allocated memory
0
 
LVL 22

Expert Comment

by:nietod
ID: 1198475
Except the code doesn't try to write past the end of the allocated array.  The

fileOut->WriteString(pch)

might die because the string might not be terminated (can't tell this from the shown code.)  However there is no reason why the delete [] would die.
0
 
LVL 1

Author Comment

by:awd
ID: 1198476
mandhjo was correct.  Although I think he made a careless mistake.  The line:
pch[nLength + 1] = NULL should read
pch[nLength] = NULL after making the suggested change.
Thanks for the comments everyone.
0
 
LVL 4

Expert Comment

by:mandhjo
ID: 1198477
As I mentioned later in my answer, I believe the correct way to initialize any buffer is to initialize ALL of it to NULL, not just what you THINK to be the last character.

Thanks for the points, glad I could help.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

662 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