|
|
Hi,
I allocated a struct from a heap, the struct may be like this one:
struct abc{
int tmp1;
string tmp2;
int tmp3;
}*pabc;
pabc=(abc*)malloc(sizeof abc);
And when I initialize the string(pabc->tmp2="1234"), an "Illegal Pointer" exception occured.
It seems that the size of the string is changed, so I decided to allocate a bigger size.
pabc=(abc*)malloc(sizeof abc+4);
But the error still exists.
sizeof(string("1234")) seems to be 8 instead of 4.
Can anyone help me with this problem?
modified on Sunday, November 22, 2009 11:46 PM
|
|
|
|
|
tyftyftyf wrote: abc->tmp2="1234"
Shouldn't this be pabc->tmp2="1234"
sizeof(string) will give you the size of the internal pointers/counters maintained by the string class.
To get the size of the string use its size() method.
modified on Sunday, November 22, 2009 10:42 PM
|
|
|
|
|
I'm really sorry i mistyped this. And the problem is probably solved by using a pointer to string instead of the string itself.
But I'm still confused about the size problem, for example, a string with 4 characters actually use 8 bytes in memory.Maybe a string contains other parameter variables, not just pure characters.
|
|
|
|
|
Try sizeof(string("abcdefghijklmnopqrstuvwxyz"));
This should also give you 8.
|
|
|
|
|
I see, the string probably itself has a pointer to an array of char.
And I solve the problem by adding:
memset(pabc,0,sizeof(abc));
I realized when using malloc, the memory is not cleared, so the pointer inside the string architect is just a mess, but when setting a value for string, the program found the string struncture is not all zero and considered that the string is already initialized so the program just change the value of the array of char that pointer pointed to.Ofcourse that pointer is just a mess, so "illegal pointer" occured.
If I clear the memory first, the pointer is just zero, so program will initialize it first.
Forgive my poor english....
Thanks!
|
|
|
|
|
As Superman mentioned sizeof(string) returns size of string class and it will return 16 for ANSI and 32 for UNICODE.
In your structure one of a member is a string and you do not know what string size would be until you assign a characters to it.
You are also facing another issue, that you have probably experience. Assigning string to member of pabc structure will cause exception, since you are merely reserving block of memory (including memory for a string) and as a result string member of the abc is not initialized because string constructor is never called.
There is remedy for that but...
pabc = (abc*)malloc(sizeof abc);
memcpy(&pabc->tmp2, &string(), sizeof(string));
pabc->tmp2 = "1234***************************";
Much better and safer is to allocate memory on the heap is to use "new" operator. This will assure that string’s constructor is called and string is properly initialized
pabc = new abc();
pabc->tmp2 = "1234***************************";
Much simpler.
Out of curiosity if you are using MFC why using std string instead of CString class?
JohnCz
MS C++ MVP
modified on Monday, November 23, 2009 10:32 AM
|
|
|
|
|
I see, I should copy a constructed null string first right? I used malloc because I was dealing with a multi-thread program, the thread which allocated and initialized the structure will die far before the structure is useless and should be deleted.So I'm consider using one thread allocate it, and another thread dispose it.
But C++ only allow "new" operator disposing a memory from a thread's local heap.So I used Windows API HeapAlloc and HeapFree, those two can allow two threads allocate and dispose memory from a same heap.
Then I encountered this initializing string problem.To make the code simpler, I wrote malloc instead of HeapAlloc, afterward I found out that these two are almost the same in single thread mode.
I guess I'm still just a beginner in C++.
ps: That program is a console, not MFC. I'd like to use BCB to design GUI. I'm still pretty confused about Windows Messages.
Thanks a lot.
|
|
|
|
|
I have edited my previous post, since code demonstrating new usage was the same (copying I guess).
I do not know if what tool you are using to write your programs. If MSVC and you are using multithreaded runtime libraries, using new is thread safe.
I do not know what function you are using to create thread. Nevertheless if you are concern about memory allocation, allocate your structure in a main thread and pass it as a parameter to a worker thread. After thread terminates, delete object.
I think using new to allocate object is much safer than using other allocation calls.
If you are serious about Windows programming I would suggest writing small Win32 application and see what window procedure does. There is nothing to be confused about windows messages, once you grasp the idea of message loop, dispatching and handling messages. Borland doe not shield you from Windows messages.
I would stay with MS tools and after familiarizing with Windows insides I would suggest using MFC. You can use other higher level languages and packages but they are using separate framework to allow application to run on Windows and make Windows insides transparent to a programmer.
JohnCz
MS C++ MVP
|
|
|
|
|
It was really wierd that new operator's proformance is much lower than HeapAlloc when more than ten threads are allocating memory form different heaps. Maybe new operator is using a mutex lock to prevent allocation errors in MSVC. The "heap concept" is hidden in new operator and I cannot free a memory from another thread, the DbgMsg shows that the "delete" operator can only free a memory from a thread's "local" heap. But HeapAlloc(Win API) allow choosing which heap to manipulate, so I eventually chose HeapAlloc.
Although I like it better when Message Dispatching and handling is hidden to programmer(I don't need to care about the details, source code is hidden, C++ CLR also have this "hallmark"), but message is the basis of window programming after all. I need to know the basic thing(maybe this is why the 'F' in "MFC" is "foundation").
I've really learned a lot, Thanks again!
|
|
|
|
|
You are welcome.
Treat messages like codes. Windows OS or other windows send messages to inform what is going on and may interest window. Like WM_MOUSEMOVE means mouse is mowing over you window and cursor position id 20, 340.
User selected item in a drop box: Hey, parent selection changed!
Messages are delivered to a thread message queue (LILO) by PostMessage and dispatched from message loop in WinMain.
Others are delivered by directly calling window procedure (Send message).
Some of them carry additional information as parameters some do not.
In VS create basic Win32 application with “Hello world” option and everything will be clear.
If you have any questions regarding API based application, post it here.
Once you understand at list basics you can switch to MFC. Remember MFC does not wrap all Windows API calls therefore knowing Windows will help.
Good Luck.
JohnCz
MS C++ MVP
|
|
|
|
|
hi,i would like to ask.. if the running time of java programs was more difficult to analyze,than c programs????
|
|
|
|
|
Assuming that both Java and C have functions to capture the time then there should be no difference. Maybe you should try it first.
|
|
|
|
|
yah, maybe i should have to!!!
running time includes fetching and storing..right?
|
|
|
|
|
It depends what you mean by 'running' time. In a multi-application OS like Windows the running time (i.e. wall clock time) depends on what else is running in the machine. If you are talking of consumed CPU cycles then that will largely depend on the data that is processed. You would expect C programs to be a little faster than Java owing to the difference in object code, but that is not always the case.
|
|
|
|
|
ahhh i see!!!
thank you by the way sir!!!
|
|
|
|
|
I do call ITextServices::Release() to release that reference. But the allocated memory isn't released. I got following stack trace by "!heap -p -a" in Windbg after ITextServices::Release() is called.
Or, who can tell me what is the offical steps to release ITextServices correctly?
Anybody could help me? Thanks in advance.
0:001> !heap -p -a 027b5360
address 027b5360 found in
_HEAP @ 150000
HEAP_ENTRY Size Prev Flags UserPtr UserSize - state
027b5360 0013 0000 [07] 027b5368 00080 - (busy)
Trace: 02fb
7c98ee72 ntdll!RtlDebugAllocateHeap+0x000000e1
7c96b3a2 ntdll!RtlAllocateHeapSlowly+0x00000044
7c93aa2b ntdll!RtlAllocateHeap+0x00000e64
7c8099ef kernel32!LocalAlloc+0x00000058
74de8f78 RICHED20!CW32System::PvReAlloc+0x00000026
74d9577c RICHED20!CArrayBase::ArAdd+0x00000040
74d957d1 RICHED20!CArrayBase::ArInsert+0x0000001a
74da426c RICHED20!CTxtArray::AddBlock+0x0000001c
74dd90d6 RICHED20!CTxtPtr::InsertRange+0x00000034
74dd99bf RICHED20!CTxtPtr::ReplaceRange+0x00000110
74dcb3a6 RICHED20!CRchTxtPtr::ReplaceRange+0x0000039c
74da7b24 RICHED20!CTxtEdit::SetRichDocEndEOP+0x00000041
74daa42f RICHED20!CTxtEdit::Init+0x000003be
74ddd88a RICHED20!CreateTextServices+0x000000a0
*** WARNING: Unable to verify checksum for d:\Build\BooguNT\BooguNote_2009_11_22_datime\BooguNote\Debug\BooguNote.exe
4b7b40 BooguNote!CBooguNoteText::Init+0x00000260
4bd4ad BooguNote!CreateHost+0x000000fd
4bd5f2 BooguNote!CreateTextControl+0x00000062
4cb745 BooguNote!CBooguNoteView::CreateTextBlock+0x00000085
4bf801 BooguNote!CBooguNoteView::LoadData+0x00000141
4bea3b BooguNote!CBooguNoteView::OpenFile+0x0000002b
4beb5e BooguNote!CBooguNoteView::OnCreate+0x0000008e
4e7790 BooguNote!CBooguNoteView::ProcessWindowMessage+0x000000a0
4a786a BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::WindowProc+0x0000007a
77d18734 USER32!InternalCallWinProc+0x00000028
77d18816 USER32!UserCallWinProcCheckWow+0x00000150
77d1b4c0 USER32!DispatchClientMessage+0x000000a3
77d1f9fe USER32!__fnINLPCREATESTRUCT+0x0000008b
7c92e473 ntdll!KiUserCallbackDispatcher+0x00000013
77d1fecc USER32!_CreateWindowEx+0x000001ed
77d1fc58 USER32!CreateWindowExW+0x00000033
4aa5f2 BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x00000122
4f0e29 BooguNote!ATL::CWindowImpl<CBooguNoteView,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x000000a9
:001> !heap -p -a 027b53f8
address 027b53f8 found in
_HEAP @ 150000
HEAP_ENTRY Size Prev Flags UserPtr UserSize - state
027b53f8 0008 0000 [07] 027b5400 00028 - (busy)
Trace: 02fe
7c98ee72 ntdll!RtlDebugAllocateHeap+0x000000e1
7c96b3a2 ntdll!RtlAllocateHeapSlowly+0x00000044
7c93aa2b ntdll!RtlAllocateHeap+0x00000e64
7c8099ef kernel32!LocalAlloc+0x00000058
74d919fb RICHED20!CW32System::PvAlloc+0x00000011
74d919e3 RICHED20!operator new+0x0000000f
74dde615 RICHED20!CTxtEdit::GetDocInfo+0x00000013
74ddd7e1 RICHED20!CTxtEdit::TxSetText+0x0000001b
4b7bc5 BooguNote!CBooguNoteText::Init+0x000002e5
4bd4ad BooguNote!CreateHost+0x000000fd
4bd5f2 BooguNote!CreateTextControl+0x00000062
4cb745 BooguNote!CBooguNoteView::CreateTextBlock+0x00000085
4bf801 BooguNote!CBooguNoteView::LoadData+0x00000141
4bea3b BooguNote!CBooguNoteView::OpenFile+0x0000002b
4beb5e BooguNote!CBooguNoteView::OnCreate+0x0000008e
4e7790 BooguNote!CBooguNoteView::ProcessWindowMessage+0x000000a0
4a786a BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::WindowProc+0x0000007a
77d18734 USER32!InternalCallWinProc+0x00000028
77d18816 USER32!UserCallWinProcCheckWow+0x00000150
77d1b4c0 USER32!DispatchClientMessage+0x000000a3
77d1f9fe USER32!__fnINLPCREATESTRUCT+0x0000008b
7c92e473 ntdll!KiUserCallbackDispatcher+0x00000013
77d1fecc USER32!_CreateWindowEx+0x000001ed
77d1fc58 USER32!CreateWindowExW+0x00000033
4aa5f2 BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x00000122
4f0e29 BooguNote!ATL::CWindowImpl<CBooguNoteView,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x000000a9
4ef8d8 BooguNote!CMainFrame::OpenFileByName+0x00000158
4e900c BooguNote!CMainFrame::OnDropFiles+0x0000013c
4e10b7 BooguNote!CMainFrame::ProcessWindowMessage+0x000003a7
5166ca BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::WindowProc+0x0000007a
77d18734 USER32!InternalCallWinProc+0x00000028
77d18816 USER32!UserCallWinProcCheckWow+0x00000150
0:001> !heap -p -a 027b5438
address 027b5438 found in
_HEAP @ 150000
HEAP_ENTRY Size Prev Flags UserPtr UserSize - state
027b5438 001b 0000 [07] 027b5440 000c0 - (busy)
Trace: 02ff
7c98ee72 ntdll!RtlDebugAllocateHeap+0x000000e1
7c96b3a2 ntdll!RtlAllocateHeapSlowly+0x00000044
7c93aa2b ntdll!RtlAllocateHeap+0x00000e64
7c8099ef kernel32!LocalAlloc+0x00000058
74de8f78 RICHED20!CW32System::PvReAlloc+0x00000026
74d9577c RICHED20!CArrayBase::ArAdd+0x00000040
74da202d RICHED20!CDisplayML::RecalcLines+0x000000a3
74da2ba2 RICHED20!CDisplayML::RecalcView+0x00000033
74d9ee4e RICHED20!CDisplay::RecalcView+0x00000029
74ddb211 RICHED20!CTxtEdit::OnTxInPlaceActivate+0x00000066
4b7c7c BooguNote!CBooguNoteText::Init+0x0000039c
4bd4ad BooguNote!CreateHost+0x000000fd
4bd5f2 BooguNote!CreateTextControl+0x00000062
4cb745 BooguNote!CBooguNoteView::CreateTextBlock+0x00000085
4bf801 BooguNote!CBooguNoteView::LoadData+0x00000141
4bea3b BooguNote!CBooguNoteView::OpenFile+0x0000002b
4beb5e BooguNote!CBooguNoteView::OnCreate+0x0000008e
4e7790 BooguNote!CBooguNoteView::ProcessWindowMessage+0x000000a0
4a786a BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::WindowProc+0x0000007a
77d18734 USER32!InternalCallWinProc+0x00000028
77d18816 USER32!UserCallWinProcCheckWow+0x00000150
77d1b4c0 USER32!DispatchClientMessage+0x000000a3
77d1f9fe USER32!__fnINLPCREATESTRUCT+0x0000008b
7c92e473 ntdll!KiUserCallbackDispatcher+0x00000013
77d1fecc USER32!_CreateWindowEx+0x000001ed
77d1fc58 USER32!CreateWindowExW+0x00000033
4aa5f2 BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x00000122
4f0e29 BooguNote!ATL::CWindowImpl<CBooguNoteView,ATL::CWindow,ATL::CWinTraits<1442840576,0> >::Create+0x000000a9
4ef8d8 BooguNote!CMainFrame::OpenFileByName+0x00000158
4e900c BooguNote!CMainFrame::OnDropFiles+0x0000013c
4e10b7 BooguNote!CMainFrame::ProcessWindowMessage+0x000003a7
5166ca BooguNote!ATL::CWindowImplBaseT<ATL::CWindow,ATL::CWinTraits<114229248,262400> >::WindowProc+0x0000007a
|
|
|
|
|
Hi.
First I would like to say that I am quite the beginner using C++. I have just started to learn DirectX and I have a problem compiling. Maybe this post should be put in the Graphics forum but I don’t know if it is really a Graphics problem.
I went through the tutorial called “Beginning Direct3D by Vahid Kazemi” found here codeproject.com[^] and it worked great. I am currenly reading a book called “Introduction to 3D game programming with DirectX 9.0 by Frank D. Luna”. In the beginning of this book there is a tutorial/sample project that basically is the same as the one by Vahid Kazemi only structured a bit differently. It has the basic framework that I am supposed to build the rest of the tutorials in the book on. Now I am having some problems getting the project form the book to compile.
Here is the compiler error:
-----
1>Linking...
1>Main.obj : error LNK2019: unresolved external symbol "bool __cdecl d3d::InitD3D(struct HINSTANCE__ *,int,int,bool,enum _D3DDEVTYPE,struct IDirect3DDevice9 * *)" (?InitD3D@d3d@@YA_NPAUHINSTANCE__@@HH_NW4_D3DDEVTYPE@@PAPAUIDirect3DDevice9@@@Z) referenced in function _WinMain@16
1>C:\Users\Stian H. Haug\Documents\Visual Studio 2008\Projects\Spill Programmering\Spillprogrammering - Sample One\Debug\Spillprogrammering - Sample One.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Stian H. Haug\Documents\Visual Studio 2008\Projects\Spill Programmering\Spillprogrammering - Sample One\Spillprogrammering - Sample One\Debug\BuildLog.htm"
1>Spillprogrammering - Sample One - 2 error(s), 0 warning(s)
-----
Judgeing by the error i thought it might be a linking error. However I managed to compile the tutorial by Vahid Kazemi and I have included all the same files, .lib and such that I included in that tutorial.
Here is my WinMain loop where I call the InitD3D() function.
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance, 800, 600, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
Here is the prototype in my header file:
bool InitD3D(
HINSTANCE hInstance,
int width, int height,
bool windowed,
D3DDEVTYPE deviceType,
IDirect3DDevice9** device);
I belive the InitD3D()function is implemented in one of these files:
#include <d3d9.h>
#include <d3dx9.h>
I have included them both.
If anyone can help me with understanding this problem it would be great. If you want me to post the whole code i can.
Thanks!
modified on Sunday, November 22, 2009 3:38 PM
|
|
|
|
|
stianhh wrote: I belive the InitD3D()function is implemented in one of these files:
You need to verify which it is! From the error messages you have posted there is no implementation of d3d::InitD3D() , so you need to find out if it is implemented in one of your source files, or is part of a library/dll that should be available to the linker. The article you refer to shows a local implementation of this function.
|
|
|
|
|
Seems the example in the book mentioned a .cpp file implementing the InitD3D() function that had to be downloaded from the books homepage. Sorry but the way it was written in the book didn't make me think twice about weather the function was implemented in the included files or not.
Thanks for the help!
|
|
|
|
|
Generally speaking the .h files should only contain declarations. All the implementation code should be in the .cpp files.
And yes, I do know that ATL does it the other way, but that is a foul abomination that should be wiped from the face of the earth.
|
|
|
|
|
stianhh wrote: I have just started to learn DirectX and I have a problem compiling.
It's not really at compilation but rather at linking.
You probably forgot to link to the direct3D library. Did you follow these steps described at the begining of the article ?
If you use Microsoft Visual C++ create an empty Win32 project and in the Project Properties>Linker>Input>Additional Dependencies add d3d8.lib (main direct3d library), d3dx8.lib (extensions to the main library) and winmm.lib (windows multimedia library).
|
|
|
|
|
Hi,
I need to search for a file in a specific directory. This directory would also have subdirectories in it. Can I have a API / code snippet for this.
Thanks in advance.
|
|
|
|
|
There is no single API to do that, however this[^] MSDN sample may help.
-Suhredayan
|
|
|
|
|
FindFirstFile、FindNextFile
|
|
|
|
|