Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Here's a good one.

Here's a good one.

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JD Steffen
    wrote on last edited by
    #1

    :wtf: Okay here's a problem I am having: I create a pointer to a char like so: char *test = new char; then I pass that pointer to a class member function which fills it with data from a file. Heres the code for that: bool FileManager::ReadString(LPSTR StringData) { char SingleChar; bool done = false; while (done != true) { if(ReadData(&SingleChar, 1)==2) { return true; } switch (SingleChar) { case 10: case 13: { done=true; }break; default: { *StringData = SingleChar; StringData++; }break; } } *StringData = 0; StringData++; return true; } Then afterwards in the function that called the member function I do a: delete test; When it runs this piece of code I get: Debug Error! Program: c:\JD's Projects\FileManager\Debug\FileManager.exe DAMAGE: after Normal block (#38) at 0x00851FC0. Any Ideas? thanks, JD

    R 1 Reply Last reply
    0
    • J JD Steffen

      :wtf: Okay here's a problem I am having: I create a pointer to a char like so: char *test = new char; then I pass that pointer to a class member function which fills it with data from a file. Heres the code for that: bool FileManager::ReadString(LPSTR StringData) { char SingleChar; bool done = false; while (done != true) { if(ReadData(&SingleChar, 1)==2) { return true; } switch (SingleChar) { case 10: case 13: { done=true; }break; default: { *StringData = SingleChar; StringData++; }break; } } *StringData = 0; StringData++; return true; } Then afterwards in the function that called the member function I do a: delete test; When it runs this piece of code I get: Debug Error! Program: c:\JD's Projects\FileManager\Debug\FileManager.exe DAMAGE: after Normal block (#38) at 0x00851FC0. Any Ideas? thanks, JD

      R Offline
      R Offline
      Rory Solley
      wrote on last edited by
      #2

      Your test variable is a pointer to a SINGLE char value. If you want to store an array of chars i.e. a string, you will have to declare an array: char* pArray = new char[NumChars + 1]; where NumChars is the number of characters you want to store. If you don't know in advance how many you want to store, pick a large constant value. Note the extra one added to accommodate the null-terminator at the end of the string. This is a style issue, you may wish to include the null-terminator in the NumChars count. Now when you use your pointer arithmetic: StringData++; you will be using valid allocated memory. Remember that when you come to deallocate an array, the syntax is as follows: delete []pArray; // Note the square brackets On a related note, you might want to look at the standard library's basic_string<> template (STL). use MSDN - it can simpify string handling a great deal and is a good way to learn about templates. Hope that helps.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups