Click here to Skip to main content
15,885,216 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: How to know if a pointer is valid? Pin
David Crow26-Jun-06 3:48
David Crow26-Jun-06 3:48 
AnswerRe: How to know if a pointer is valid? [modified] Pin
Stephen Hewitt26-Jun-06 16:53
Stephen Hewitt26-Jun-06 16:53 
GeneralRe: How to know if a pointer is valid? Pin
David Crow27-Jun-06 3:08
David Crow27-Jun-06 3:08 
AnswerRe: How to know if a pointer is valid? Pin
Viorel.26-Jun-06 3:52
Viorel.26-Jun-06 3:52 
GeneralRe: How to know if a pointer is valid? Pin
hatemtalbi26-Jun-06 3:57
hatemtalbi26-Jun-06 3:57 
GeneralRe: How to know if a pointer is valid? [modified] Pin
Stephen Hewitt26-Jun-06 18:03
Stephen Hewitt26-Jun-06 18:03 
GeneralRe: How to know if a pointer is valid? Pin
David Crow27-Jun-06 3:05
David Crow27-Jun-06 3:05 
GeneralRe: How to know if a pointer is valid? Pin
Stephen Hewitt27-Jun-06 3:56
Stephen Hewitt27-Jun-06 3:56 
DavidCrow wrote:
Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.

 
 Yes, that's what the API does. This doesn't contradict my statement. The OP wanted to know how to tell if memory "was allocated by the application and can be used without problem". AfxIsValidAddress was put forward as a solution and the OP accepted it claiming that it worked. I pointed out that if this API could be used to perform this function then the ASSERT in the code below should fire as AfxIsValidAddress should return FALSE given that it is surely not safe to use a pointer after it has been deleted:
int *pInt = new int;
delete pInt;
ASSERT( AfxIsValidAddress(pInt, sizeof(int)) );


This is not the case. If you try this code AfxIsValidAddress returns TRUE and thus the ASSERT will not fire. Why? Because the address pointed to by pInt was entirely contained in the process’s address space. It's cached by the heap (the small block heap in this case) for recycling. So the problem is that just because an address is physically mapped into the process's address space doesn't mean it's safe to use.

In general these APIs can only be used to tell if memory can't be safely used but not to tell if it is safe to use.


Steve
AnswerRe: How to know if a pointer is valid? Pin
Sarath C26-Jun-06 3:52
Sarath C26-Jun-06 3:52 
AnswerRe: How to know if a pointer is valid? Pin
Roland Pibinger26-Jun-06 9:33
Roland Pibinger26-Jun-06 9:33 
GeneralRe: How to know if a pointer is valid? Pin
Stephen Hewitt26-Jun-06 19:42
Stephen Hewitt26-Jun-06 19:42 
QuestionGet Motherboard sensors Pin
honae26-Jun-06 3:15
honae26-Jun-06 3:15 
QuestionRe: Get Motherboard sensors Pin
David Crow26-Jun-06 3:39
David Crow26-Jun-06 3:39 
AnswerRe: Get Motherboard sensors Pin
honae26-Jun-06 3:50
honae26-Jun-06 3:50 
QuestionRe: Get Motherboard sensors Pin
David Crow26-Jun-06 4:01
David Crow26-Jun-06 4:01 
AnswerRe: Get Motherboard sensors [modified] Pin
honae26-Jun-06 4:17
honae26-Jun-06 4:17 
QuestionRe: Get Motherboard sensors Pin
David Crow26-Jun-06 4:40
David Crow26-Jun-06 4:40 
AnswerRe: Get Motherboard sensors [modified] Pin
honae26-Jun-06 4:49
honae26-Jun-06 4:49 
GeneralRe: Get Motherboard sensors Pin
David Crow26-Jun-06 5:03
David Crow26-Jun-06 5:03 
QuestionAppend text to a rich edit w/o using ReplaceSel Pin
KellyR26-Jun-06 3:12
KellyR26-Jun-06 3:12 
AnswerRe: Append text to a rich edit w/o using ReplaceSel Pin
Justin Tay26-Jun-06 3:33
Justin Tay26-Jun-06 3:33 
GeneralRe: Append text to a rich edit w/o using ReplaceSel Pin
KellyR26-Jun-06 4:04
KellyR26-Jun-06 4:04 
Questionhow we add menu captions in runtime [modified] Pin
vasusree26-Jun-06 3:04
vasusree26-Jun-06 3:04 
AnswerRe: how we add menu captions in runtime Pin
Sarath C26-Jun-06 4:22
Sarath C26-Jun-06 4:22 
QuestionAssertion Error Pin
Amit Agarrwal26-Jun-06 3:01
Amit Agarrwal26-Jun-06 3:01 

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.