Click here to Skip to main content
15,912,072 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: message associted with number?[modified] Pin
Eytukan6-Mar-09 6:52
Eytukan6-Mar-09 6:52 
QuestionLeft Mouse Click and Hold Pin
Software20076-Mar-09 6:26
Software20076-Mar-09 6:26 
AnswerRe: Left Mouse Click and Hold Pin
Jonnie White6-Mar-09 6:30
Jonnie White6-Mar-09 6:30 
GeneralRe: Left Mouse Click and Hold Pin
Software20076-Mar-09 6:50
Software20076-Mar-09 6:50 
AnswerRe: Left Mouse Click and Hold Pin
Code-o-mat6-Mar-09 8:23
Code-o-mat6-Mar-09 8:23 
GeneralRe: Left Mouse Click and Hold Pin
Software20076-Mar-09 8:49
Software20076-Mar-09 8:49 
QuestionRe: Left Mouse Click and Hold Pin
David Crow6-Mar-09 10:15
David Crow6-Mar-09 10:15 
QuestionA tricky way of manipulating memory allocation [modified] Pin
JackPuppy6-Mar-09 4:25
JackPuppy6-Mar-09 4:25 
fifth Edition
in Chapter 19, there is one note:

Note: It is important to realize that a single address space consists of one executable module and several DLL modules. Some of these modules can link to a static version of the C/ C++ run-time library, some of these modules might link to a DLL version of the C/C++ run-time library, and some of these modules (if not written in C/C++) might not require the C/ C++ run-time library at all. Many developers make a common mistake because they forget that several C/C++ run-time libraries can be present in a single address space. Examine the following code:

VOID EXEFunc() {
PVOID pv = DLLFunc();
// Access the storage pointed to by pv...
// Assumes that pv is in EXE's C/C++ run-time heap
free(pv);
}

PVOID DLLFunc() {
// Allocate block from DLL's C/C++ run-time heap
return(malloc(100));
}

So, what do you think? Does the preceding code work correctly? Is the block allocated by the DLL's function freed by the EXE's function? The answer is: maybe. The code shown does not give you enough information. If both the EXE and the DLL link to the DLL C/C++ run-time library, the code works just fine. However, if one or both of the modules link to the static C/C++ run-time library, the call to free fails. I have seen developers write code similar to this too many times, and it has burned them all.


===========================================================================

At first one thing the starup() function do is to initialize a heap used by malloc and free and low-level IO manipulation use in CRT, so when there is two copies CRT liberary in one process adress and there will be two heaps for two groups of malloc and free, and that one block memory grabed by malloc can only be freed by the very free() function in the same copy.

but what free() is?

in fact free()'s source code is like this:

void free(void *ptr)
{
struct mem_control_block *free;
free = ptr - sizeof(struct mem_control_block);
free->is_available = 1;
return;
}


every malloc()function return pointer to a block,however there are some bytes ahead the block conserved for system information, and this information is stored in a struct mem_control_block, so indeed the pointer malloc()function returned points to the byte after the mem_control_block.

struct mem_control_block {
int is_available; //a flag that specify whether this block can be used by any other application
int size; //the size of the block
};



finnally, it seems no error in these code:

VOID EXEFunc() {
PVOID pv = DLLFunc();
// Access the storage pointed to by pv...
// Assumes that pv is in EXE's C/C++ run-time heap
free(pv);
}

PVOID DLLFunc() {
// Allocate block from DLL's C/C++ run-time heap
return(malloc(100));
}

if the system doesn't allow a free() to free a block in heap of another copy of CRT, then how the system is able to forbid?????

Thanks for your help.
I'm struggle reading win via c/c++

Jack

modified on Friday, March 6, 2009 11:13 AM

AnswerRe: A tricky way of manipulating memory allocation Pin
Eytukan6-Mar-09 4:59
Eytukan6-Mar-09 4:59 
AnswerRe: A tricky way of manipulating memory allocation Pin
JackPuppy6-Mar-09 5:17
JackPuppy6-Mar-09 5:17 
Answer. Pin
Perisic, Aleksandar6-Mar-09 6:10
Perisic, Aleksandar6-Mar-09 6:10 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy6-Mar-09 8:49
JackPuppy6-Mar-09 8:49 
General. Pin
Perisic, Aleksandar6-Mar-09 10:04
Perisic, Aleksandar6-Mar-09 10:04 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy6-Mar-09 10:54
JackPuppy6-Mar-09 10:54 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy6-Mar-09 10:59
JackPuppy6-Mar-09 10:59 
General. Pin
Perisic, Aleksandar6-Mar-09 11:24
Perisic, Aleksandar6-Mar-09 11:24 
General. Pin
Perisic, Aleksandar6-Mar-09 12:48
Perisic, Aleksandar6-Mar-09 12:48 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy6-Mar-09 23:08
JackPuppy6-Mar-09 23:08 
General. Pin
Perisic, Aleksandar7-Mar-09 3:16
Perisic, Aleksandar7-Mar-09 3:16 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy7-Mar-09 9:58
JackPuppy7-Mar-09 9:58 
General. Pin
Perisic, Aleksandar7-Mar-09 10:59
Perisic, Aleksandar7-Mar-09 10:59 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy10-Mar-09 2:00
JackPuppy10-Mar-09 2:00 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy10-Mar-09 2:01
JackPuppy10-Mar-09 2:01 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy10-Mar-09 4:19
JackPuppy10-Mar-09 4:19 
GeneralRe: A tricky way of manipulating memory allocation Pin
JackPuppy10-Mar-09 4:20
JackPuppy10-Mar-09 4:20 

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.