|
its typically used to 'pack' a structure into a certain byte alignment because otherwise the structure gets padded out automatically and things either use up more memory than necessary OR saving and loading mechanisms (particularly on cross platform code or tool development (e.g. 16 bit machines etc.)) end up reading in the wrong amount of data and so the struct it loads is full of rubbish.
However, my experience with #pragma pack is that its the most idiotic compiler directive i have seen. You can specify a struct with size 16 bits (using bit fields of type 'unsigned') specify a #pragma pack directive of 2 bytes (so the size of the struct should be 2 bytes in memory) and yet it fails badly and instead aligns it on a 4 byte boundary ! ! Change the unsigned type to unsigned short and it chucks it on a 2 byte boundary, hooray ! !
My verdict :
Its absolute rubbish.
"When I left you I was but the learner, now I am the master" - Darth Vader
|
|
|
|
|
"I think it's a CPU quirk that data accessed on an even 4 byte boundry is more efficient on 32 bit processers???"
That's correct and the P4 is really horrendous at it. P3s and AMD processors have an easier time of it.
Actually, the default packing of data structures is to the size of the item ie., doubles are aligned to the nearest eight-byte boundary, ints on a four-byte boundary, and shorts on a two-byte boundary.
From MSDN : "If you use #pragma pack without an argument, structure members are packed to the value specified by /Zp. The default /Zp packing size is /Zp8."
I do a lot of work with communications in various manners and we often use pack(1) insure that the data that we transfer is aligned correctly. If the processor has difficulties with that then we just have to deal with it.
|
|
|
|
|
Hi,everyone:
i access my data of Doc using following code:
<br />
CMainFrame* pMainFrmWnd = (CMainFrame*)AfxGetMainWnd(); <br />
ASSERT(pMainFrmWnd != NULL); <br />
<br />
CWnd* pchildWnd = pMainFrmWnd->GetWindow(GW_CHILD);<br />
ASSERT(pchildWnd->GetSafeHwnd() != NULL);<br />
<br />
CvcydcjView *pMyView = NULL; <br />
while(pchildWnd->GetSafeHwnd() != NULL)<br />
{<br />
ASSERT(pchildWnd->GetSafeHwnd() != NULL);<br />
if(pchildWnd->GetRuntimeClass()->m_lpszClassName == "CvcydcjView")<br />
{<br />
pMyView = (CvcydcjView*)pchildWnd;<br />
break;<br />
}<br />
pchildWnd = pchildWnd->GetWindow(GW_HWNDNEXT);<br />
}<br />
ASSERT(pMyView != NULL);<br />
<br />
CvcydcjDoc *pMydoc;<br />
pMydoc=pMyView->GetDocument();<br />
ASSERT(pMydoc != NULL);<br />
<br />
pMydoc->MyData="demo";<br />
but when i use it in a thread, it didn't work, it seems stopped at
pMydoc=pMyView->GetDocument();<br />
can you give me some suggestions? Thx!
- - - - - - - - - - - - - - - - - - - - - - - - - -
Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could !
Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ...
There can be miracles! When you believe, though hope is frail, it's hard to kill !
Who knows what miracles, you can achieve! When you believe, somehow you will ...
YOU WILL WHEN YOU BELIEVE
|
|
|
|
|
I don't have the answer to your question but a little suggestion. When you create your thread, why don't you pass the document (through the use of the LPVOID pParam) to the thread function and then use it there ?
|
|
|
|
|
Thank you for your suggestion.
i create the thread in my view class with following code:
<br />
m_hCallthread = CreateThread(<br />
NULL,
NULL,
CallOut,
NULL,
0,
NULL
);<br />
could you tell me how to pass the pointer? thx? i try replace last NULL with this, it didn't work.
sorry, i'm a new bie of vc... wait for your answer... Thank you very much!
- - - - - - - - - - - - - - - - - - - - - - - - - -
Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could !
Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ...
There can be miracles! When you believe, though hope is frail, it's hard to kill !
Who knows what miracles, you can achieve! When you believe, somehow you will ...
YOU WILL WHEN YOU BELIEVE
|
|
|
|
|
Create your thread like that (supposed you have a pointer to your document that is called pDoc):
m_hCallthread = CreateThread(<br />
NULL,
NULL,
CallOut,
pDoc,
0,
NULL
);<br />
Then in your thread function you'll need to cast your void pointer to your document again:
CYourDoc* pDocument = (CYourDoc*)pParam;
|
|
|
|
|
Thank you very much! It works
- - - - - - - - - - - - - - - - - - - - - - - - - -
Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could !
Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ...
There can be miracles! When you believe, though hope is frail, it's hard to kill !
Who knows what miracles, you can achieve! When you believe, somehow you will ...
YOU WILL WHEN YOU BELIEVE
|
|
|
|
|
Dick.leeSolucky wrote:
can you give me some suggestions?
Using MFC documents/windows in a thread is not recommended. In fact, most of the time it simply won't work. You'll have to pass a pointer to your data to your thread routine.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Thank you for your reply.
we need have a thread dealing with something(like initialize drives,and connect to voice_board and dial ...) and our main functions are all in View class, for example: displaymessage... we store the data in doc class.
may i create the thread in my doc class or view class? then i can access the data easiler, but it seems if we create the callout function in view class,we get these errors:
<br />
D:\VC YDCJ\vcydcjView.cpp(431) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'int (void)' to 'unsigned long (__stdcall *)(void *)'<br />
None of the functions with this name in scope match the target type<br />
thank you and waiting for your reply... thx!
- - - - - - - - - - - - - - - - - - - - - - - - - -
Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could !
Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ...
There can be miracles! When you believe, though hope is frail, it's hard to kill !
Who knows what miracles, you can achieve! When you believe, somehow you will ...
YOU WILL WHEN YOU BELIEVE
|
|
|
|
|
Hiall,
I want to know cpu usage of a running program?
Please tell me how?
Best Wishes.
Thanks.
|
|
|
|
|
|
You can also see the discussion here.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi everiyone,
I have a problem, I'm using WinXp Pro, when I set's the "Right aligned text" to a List Box control works fine.
But when run this program on Windows 98 the property does not work.
Why?
how I can do it?
Thanks in advance.
Pd. I'm using VC++ 6.0 SP5 on WinXP Pro
Ivan Cachicatari
www.latindevelopers.com
|
|
|
|
|
I believe that that style was not introduced/implemented back in Win98. So, if you really need right-aligned text, you'll need to implement an owner-drawn list box and draw the strings right-aligned yourself.
onwards and upwards...
|
|
|
|
|
|
how to find the process name if i know the process ID. Is there any straight forward function? or pliz forward me a link
|
|
|
|
|
Try this:
char szProcessName[MAX_PATH];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE, processID);
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName) );
}
}
Still Alone in this beautiful world
My Articles
|
|
|
|
|
Hello Friends,
What's is the main advantage of having virutal destructors? Why don't we have virtual constructor?
Thanks in advance.
Neelesh K J Jain.
|
|
|
|
|
That's a good question .
In fact if we don't need virtual constructor, it's because when you instanciate your pointer, you ALWAYS know what class you instanciate. So take the example of one base class (CBase) and one child class (CChild). If you have an uninstancied pointer to the base class:
CBase* pPointer;
If you instanciate it into a child class:
pPointer = new CChild;
the compiler knows exactly that you want to create a child class (first the compiler creates a child class and then it stores the pointer into the uninstancied pointer).
So know you've done a lot of things with this pointer and you want to destroy it:
delete pPointer;
The compiler just knows that the pointer is from type CBase (that's how it has been declared) without knowing that it has been instancied as a child class. So, in this case your destructor must be virtual so that the compiler will call the destructor of your child class and not from the base class.
Hope this helps
|
|
|
|
|
I like this question. The other posting did a good job of explaining why the virtual destructor is needed. The reason you don't need a virtual constructor is that you are specifically telling the compiler what you want to instantiate.
You write
int i = 10;
CBase* pPointer;
pPointer = new CChild(i);
The concept of virtual functions is also described as dynamic run time binding. So the idea is that you call a function on the fully instantiated object and the system is smart enough to realize that since a function is virtual, the child class version of the function is called first. Then the child class can execute it's own code and may or may not ivoke the base class version of the function as well.
So virtual functions don't make sense during the construction process. The child class has to call the constructor for the base class from within it's own constructor. Once the construction of the class is complete, you can use the base class pointer to call virtual functions. If you tried to call any virtual functions from either of the constructors, I'm not sure what would really happen. I wouldn't recommend trying this as it really makes no sense. Seems like it would be an undefined operation that may or may not cause a crash.
Shawn
|
|
|
|
|
Hello,
I'm very new to programming but have learned allot over the past few months. Here is my issue.
I have a program that I'm working on. It's written in C not C++ and it is GDI based. I'm using MS Visual Studio 2003. I have the ability to edit everything that is in the .rc file under the project, dialogs, the menus, and ext. What I don't have the ability to edit is the items below the menu such as the pens and other visual designs that are contained within the main window of the program. This is not like VB or C# where you can just edit the main window from the IDE. I have no ability whatsoever to change the overall visual design or feel of the main body of the program other than manual editing of the code, for example:
// draw check box
// We select the brown pen anyway to make sure we have a valid value
// for hOldPen for later calls to SelectObject()
hOldPen = SelectObject( hDC, lpe->hBlackPen );
if ( !lpe->tracks[i].bData )
{
pts[0].x = pts[3].x = pts[3].x = ((hx[1]+1)/2 - 4) - lpe->dx;
pts[1].x = pts[2].x = pts[0].x + 12;
pts[0].y = pts[1].y = pts[4].y = rc.top + 1;
pts[2].y = pts[3].y = rc.top + 13;
Polyline( hDC, pts, 5 );
}
if ( lpe->tracks[i].bChecked && !lpe->tracks[i].bData )
{
SelectObject( hDC, lpe->hBlackPen );
pts[0].x += 3;
pts[0].y += 3;
pts[1].x = pts[0].x + 2;
pts[1].y = pts[0].y + 3;
pts[2].x = pts[1].x + 8;
pts[2].y = pts[1].y - 8;
Polyline( hDC, pts, 4 );
pts[0].x += 1;
pts[1].x = pts[0].x + 2;
pts[1].y = pts[0].y + 3;
pts[2].x = pts[1].x + 8;
pts[2].y = pts[1].y - 8;
Polyline( hDC, pts, 4 );
}
SelectObject( hDC, hOldPen );
}
SetBkMode( hDC, oldBkMode );
SelectObject( hDC, hOldFont );
EndPaint( hWnd, &p );
}
The code above is for just displaying a simple checkbox for selecting items displayed within the main window. I have been able to modify the code to an extend but it takes allot off time and pulling out of hair;) Really what I would like to do is be able to change these things at least as easy as I can make changes in the .rc file under the MSVC resource editor. There has to be an easier way to make changes or additions besides what I'm now doing. Unfortionally, I'm not the original author of this program so I don't know what editor they used to compose the GUI.
Better yet, I would like to spice up my main window from the generic look it has now to something better as well as be able to add to it. Are there any libraries out there that would easily integrate into this GDI application? I really like the new Office 2003 look and feel.
I would very much appreciate any help I can get and thank everyone in advance for your time in helping me.
Jamie
|
|
|
|
|
Hello Friends,
I am displaying a Progress control in the statusbar of my application.Whenever I switch to another window using Alt+Tab and come back to my application, the progress control is not refreshing and seems to struck in its position eventhough in the background the application is in progress. This problems arises only if I am having XP operating system, if I have Win2000 then the progress control is refreshing properly and the user can see the progress.
So please help me regarding the above mentioned problem how can I refresh the progress bar in any operating system.
thanks in advance.
Neelesh K J Jain.
|
|
|
|
|
http://www.codeproject.com/miscctrl/CSkinProgress.asp?df=100&forumid=15586&mpp=50&select=885997#xx885997xx
Kochise
In Code we trust !
|
|
|
|
|
Hi all,
Simple function (cut down to not include all the other crap.
I have #included the winsock.h and included the wsock32.lib.
int GetIPAddresses()
{
char shostname[255];
int e = 0;
e=gethostname(shostname, sizeof(shostname));
struct hostent *pdata = ::gethostbyname(ashostname);
}
On compiling I get this error :
error C2143: syntax error : missing ';' before 'type'
Does anyone know what that is?
[EDIT]This is in C and not C++. I forgot to mention that.[/EDIT]
Cheers,
"If you're too careful, your whole life can become a f---in' grind." - Mike McD (Rounders)
|
|
|
|
|
Have you tried using the winsock2 stuff instead?
winsock2.h
and
ws2.lib
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|