Click here to Skip to main content
15,899,124 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
JokeRe: Heap corruption?! Pin
Nader Elshehabi31-Aug-06 12:29
Nader Elshehabi31-Aug-06 12:29 
GeneralRe: Heap corruption?! Pin
Stephen Hewitt31-Aug-06 14:10
Stephen Hewitt31-Aug-06 14:10 
GeneralRe: Heap corruption?! Pin
Zac Howland1-Sep-06 3:56
Zac Howland1-Sep-06 3:56 
GeneralRe: Heap corruption?! Pin
Stephen Hewitt1-Sep-06 4:19
Stephen Hewitt1-Sep-06 4:19 
GeneralRe: Heap corruption?! Pin
Zac Howland1-Sep-06 4:30
Zac Howland1-Sep-06 4:30 
AnswerRe: Heap corruption?! Pin
Jun Du31-Aug-06 8:20
Jun Du31-Aug-06 8:20 
GeneralRe: Heap corruption?! Pin
Nader Elshehabi31-Aug-06 9:06
Nader Elshehabi31-Aug-06 9:06 
AnswerRe: Heap corruption?! Pin
Stephen Hewitt31-Aug-06 14:07
Stephen Hewitt31-Aug-06 14:07 
Try this instead.

#include <iostream>
#include <iomanip>
 
int main(int argc, char *argv[])
{
    using namespace std;
 
    char *pChar = new char[2];
    cin >> setw(2) >> pChar;
    delete [] pChar;
 
    return 0;
}


You're reading too many charcters: you've got room for a NULL terminator (a string of zero length!) but your reading more and getting a buffer overrun. My code tells the IO classes how big the buffer is so it will not overrun it.

If you only want to read a single char you could try doing this (in your code):
cin >> *MyText;


Steve

GeneralRe: Heap corruption?! Pin
Nader Elshehabi1-Sep-06 5:22
Nader Elshehabi1-Sep-06 5:22 
Questionvoid main ... crashing Pin
jon-8031-Aug-06 7:12
professionaljon-8031-Aug-06 7:12 
AnswerRe: void main ... crashing Pin
Jun Du31-Aug-06 7:24
Jun Du31-Aug-06 7:24 
GeneralRe: void main ... crashing Pin
jon-8031-Aug-06 7:41
professionaljon-8031-Aug-06 7:41 
AnswerRe: void main ... crashing [modified] Pin
Chris Losinger31-Aug-06 7:43
professionalChris Losinger31-Aug-06 7:43 
GeneralRe: void main ... crashing Pin
jon-8031-Aug-06 7:51
professionaljon-8031-Aug-06 7:51 
GeneralRe: void main ... crashing Pin
Chris Losinger31-Aug-06 7:59
professionalChris Losinger31-Aug-06 7:59 
GeneralRe: void main ... crashing Pin
Zac Howland31-Aug-06 8:10
Zac Howland31-Aug-06 8:10 
GeneralRe: void main ... crashing Pin
Chris Losinger31-Aug-06 8:17
professionalChris Losinger31-Aug-06 8:17 
GeneralRe: void main ... crashing Pin
Zac Howland31-Aug-06 8:36
Zac Howland31-Aug-06 8:36 
QuestionFrameless SDI Application Pin
kiranin31-Aug-06 7:05
kiranin31-Aug-06 7:05 
QuestionMSFLEXIGRID UPDATING Pin
Desmond Mardle31-Aug-06 6:33
Desmond Mardle31-Aug-06 6:33 
QuestionMFC Program crashes when using release configuration (Visual C++ 6.0) Pin
darbien siamak31-Aug-06 5:58
darbien siamak31-Aug-06 5:58 
AnswerRe: MFC Program crashes when using release configuration (Visual C++ 6.0) Pin
toxcct31-Aug-06 6:19
toxcct31-Aug-06 6:19 
QuestionEnding a Thead and retrieve the result in MFC Pin
manustone31-Aug-06 5:10
manustone31-Aug-06 5:10 
AnswerRe: Ending a Thead and retrieve the result in MFC Pin
Zac Howland31-Aug-06 5:22
Zac Howland31-Aug-06 5:22 
GeneralRe: Ending a Thead and retrieve the result in MFC Pin
manustone31-Aug-06 5:47
manustone31-Aug-06 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.