|
In general it depends on the operating system and on the compiler version.
In Windows environment, new will typically relay on malloc (the good old C runtime), that hiddenly allocate from a local heap.
(C6 did it whith LocalAlloc , I did'nt look what's on C7: probaly HeapAlloc ).
The best way to detect this is (but you must be patient) to code a small program like
int main()
{ delete new int(0); }
and executing it with the debugger step by step going into the functions.
About "page": it is something that deal with the addressing architecture of the processor. It can be roughtly defined as the maxixmum amout of memory addresses you can span by changing the value of a single CPU register. But this definition may vary depending on the processor architecture.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
emilio_grv wrote:
In Windows environment, new will typically relay on malloc
but new is re-entrant while malloc() isn't.
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
On windows, reentrancy isn't a big issue since traps happen only in very well defined points (i.e. message pumps or waits). However, when using the multithreaded libraries, malloc is serialized.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
emilio_grv wrote:
C6 did it whith LocalAlloc
new: C6 does it with _nh_malloc.
malloc: C6 does it with _nh_malloc or _nh_malloc_dbg.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Yes, but _nh_malloc is an internal CRT function that call other internal functions.
Here's the call stack for a
int main() { delete new int(0); return 0; }
n1.exe!_heap_alloc_base(size=40)
n1.exe!_heap_alloc_dbg(nSize=4, nBlockUse=1, szFileName=0x00000000, nLine=0) n1.exe!_nh_malloc_dbg(nSize=4, nhFlag=1, nBlockUse=1, szFileName=0x00000000, nLine=0)
n1.exe!_nh_malloc(nSize=4, nhFlag=1)
n1.exe!operator new(cb=4)
n1.exe!main()
n1.exe!mainCRTStartup()
KERNEL32.DLL!796d893d()
and here's _heap_alloc_base next instruction
...
...
--> return <code>HeapAlloc</code>(_crtheap, 0, size);
}
Similarly, operator delete calls HeapFree .
The same program, after redefining new and delete as
void* operator new(size_t s)
{ return malloc(s); }
void operator delete(void* p)
{ free(p); }
drive towards the same _heap_aloc_dbg call.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
emilio_grv wrote:
but _nh_malloc is an internal CRT function
I never said otherwise, I just said that new and malloc both call it.
call stack:
Interesting, I just single step throu a new and delete on C6 (again) while watching the call stack (on C6). _nh_malloc() makes calls to BCCORE and NTKERNAL, but gives no details as to what functions it is calling.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Under Win32, there is no difference between GlobalAlloc() and LocalAlloc() . They are only provided for compatibility to 16-bit Windows when there was a global and a local heap.
malloc() is a wrapper around HeapAlloc() , which in turn is a wrapper around VirtualAlloc() . I believe this was mentioned in Richter's book "Advanced Windows."
One thing that the new operator does that none of the others do is call an object's constructor and destructor.
See here for more.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
DavidCrow wrote:
One thing that the new operator does that none of the others do is call an object's constructor and destructor.
That's partially correct.
operator new just returns a void* to uninitialized memory. (note that new parameters don't carry the object type)
The constructor is called after operator new returns, in a new expression, like ptr = new myclass .
The compiler tranlsates this into
(ptr = (myclass*)operator new(sizeof(myclass))))->_ctor();
(assuming no myclass::operator new exist)
Although this behavior may seem a bit obscure, it allows operantor new to be inherited, overloaded and base-called. The _ctor gets called only after all the "new" stuff is ended.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
I want to add a "time receiver" in my application, similar to that in "date and time properties" of windows control panel.
it has an editbox and some static classes and some edit controls that show time and can be modified with a small scrollbar.
how edit-class is added to this edit box to show time numbers?
|
|
|
|
|
What is a "time receiver?"
Ali Tavakol wrote:
how edit-class is added to this edit box to show time numbers?
Are you wanting to: 1) add an edit control to a dialog template; 2) associate a CEdit variable with an existing edit control using ClassWizard; 3) assign the current time to an edit control?
If you could be just a bit more specific, you're bound to get more help.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Go to windows "date and time properties" dialog-box. you see an edit-box that shows the current time, with a small scroll-bar near it.
if you have spy++, you can see that this edit-box has some created edit-boxes in it, each for hour, minute and second.
how can i add some child edit-boxes to an edit-box?
|
|
|
|
|
Ali Tavakol wrote:
how can i add some child edit-boxes to an edit-box?
Rather than go to all that trouble, why not just use the date and time picker control (window class = "DateTime") and let it handle all of that stuff for you? If you are using MFC, check out the CDateTimeCtrl class.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Ali Tavakol wrote:
, with a small scroll-bar near it
I think it's a CSpinCtrl control, have a look at the documentation on how to attach it to a CEdit.
me think the time edit box in the date-time control panel is a in fact 3 ( or 4 ) edit box without the border placed "inside" a simple static constrol with a sunken edge.
and that the spin control affect which edit box has the focus.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
I'm trying to use functions from a pascal/delphi .dll file in c++ code.
Functions without or with one parameter seem to work allright. If I try to use the function below, I get the exception error as shown.
Is there a solution to overcome this ?
The code:
-----------
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
typedef BOOL (*PROC2)(int,int,int,int,int,int);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hinstLib;
PROC2 DrawLine,DrawGraphic;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary("Intermec.dll");
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
DrawLine = (PROC2) GetProcAddress(hinstLib,"DrawLine");
// If the function address is valid, call the function.
if (NULL != DrawLine)
{
fRunTimeLinkSuccess = TRUE;
(DrawLine)(1,10,2,10,10,0);
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
This is the error-message:
--------------------------
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
|
|
|
|
|
The typedef of your function is correct but it doesn't not use the same calling convention as the one in your dll. Calling convention mean which part will call the stack after the function has been called: either the caller (your prog) or the callee (the function inside your dll). Here C++ doesn't use the same calling convention as pascal. You should change your typedef in:
typedef BOOL __stdcall(*PROC2)(int,int,int,int,int,int); <br />
(the syntax may be a little bit wrong because I never remember correctly how to typedef functions prototypes ).
Like that you specifies that the function will use the standard calling convention.
|
|
|
|
|
cedric moonen wrote:
typedef BOOL __stdcall (*PROC2)(int,int,int,int,int,int);
hummm, interresting... ;P
cedric moonen wrote:
the syntax may be a little bit wrong because I never remember correctly how to typedef functions prototypes
nop, you're totally right ! great sir !
TOXCCT >>> GEII power [toxcct][VisualCalc]
-- modified at 8:26 Friday 26th August, 2005
|
|
|
|
|
Yes, that reminds me of a recent discussion
|
|
|
|
|
Thanks but I already tried this (should have told you before). The syntax has to be BOOL (__stdcall *function). BOOL __stdcall(*function) generates an error message.
If I read all the articles about calling conventions, they learn me that the main difference is in cleaning up the stack. If I read the documentation on this topic in the ms visual c++ help documentation, they only talk about __cdecl __stdcall and __fastcall. But according to this doccumentation, the cleaning-up of the stack is the same for all 3 conventions.
I'm a little confused (maybe not due to this problem) right now.
Would you have any other suggestion fo me?
Thanks in advance
|
|
|
|
|
Hi,
__cdecl is differs from other calling conventions. __cdecl is used for C calling convention.
To be clear about the stack clean up assume the following senario.
In "C", we have an option to pass variable number of parameters to a fucntion.
Generall the function would be defined a
void Add(...)
{
}
you can call this function like
void main()
{
Add(10);
Add(10,20,30);
}
In the above case , caller knows how many parameter is passed to that Add function. Add() function has no clue about how many parameter has been passed to it. In this case Add() function cannot not do stack clean up, becasue it has no clue how many paramter passed ..
Hence here is, caller responsibe to clean up the stack of Add() function, because it knew how many parameter had been passed...
But in case of __stdcall convetion, there are a lot of articles in codeproject, net, usenet available. __stdcall forces that the number of parameter passed to a function must be defined so that the called function can do clean up the stack.
In your code, you have declared a function pointer which is not __stdcall specified. But the funtion you calling is using __stdcall convention. You are calling that function using a function pointer. According to your compiler, you function pointer is following __cdecl because no calling convention specified in your code.
(*fun) ( );
When you call the function, as per __stdcall standard, your called function will do stack clean up. Hence stack is already cleaned...
In the next line, now the caller responsiblity to do clean , because your declaration says your function pointer is follwoing __cdecl convention which mean caller has to clean up the stack.. So the caller tried to clean up not existing stack (Becuase it is already cleaned), hence an error.
Hope you understand this
" Action without vision is only passing time,
Vision without action is merely day dreaming,
But vision with action can change the world "
- Words from Nelson Mandela
Thanks & Regards,
Gopalakrishnan
|
|
|
|
|
j.dinjens wrote:
If I read all the articles about calling conventions, they learn me that the main difference is in cleaning up the stack. If I read the documentation on this topic in the ms visual c++ help documentation, they only talk about __cdecl __stdcall and __fastcall. But according to this doccumentation, the cleaning-up of the stack is the same for all 3 conventions.
I'm a little confused :confusedmaybe not due to this problem) right now.
A __stdcall function’s arguments are pushed onto the stack from right to left, and the called function pops these arguments from the stack before it returns. It cannot be used with a variable-argument function. A __cdecl function’s arguments are pushed onto the stack from right to left, and the stack is cleaned up by the caller.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Hi
I have developed and simple MFC, dialogue based application. Nothing in it.
Now i want to get the HWND(handle) of my Window then what functions to use ?
Any help plz
Regards,
Amarelia Maehsh
Gujarat
India
|
|
|
|
|
GetSafeHwnd
He is smart. He will make our Windows go.
|
|
|
|
|
Hi,
When you need window handle for main window, your can call AfxGetMainWnd ()->GetSafeHwnd() function. this is call from outside window object... From your window object you can getsafehwnd()
" Action without vision is only passing time,
Vision without action is merely day dreaming,
But vision with action can change the world "
- Words from Nelson Mandela
Thanks & Regards,
Gopalakrishnan
|
|
|
|
|
Hi,
If I have a popup menu (in HMENU form) which I'm just about to display as the context menu, how is it possible to get the CmdUpdateUI system to update all the commands in the menu with their relevant check states, disabled states etc.? This doesn't seem to happen automatically!
Joel Holdsworth
|
|
|
|
|
One method would be to have the MainFrame() the owner of the menu in the call to TrackPopupMenu(). The other would be to look at the code for
void CFrameWnd::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu)
to see how its done there any in effect mirror the code yourself.
If you vote me down, my score will only get lower
|
|
|
|
|