|
This is from memory so I hope I'm close...
KellyR wrote: how exactly is the memory allocated
Local one is on the stack.
KellyR wrote: Is a new instance re-allocated each time the function is called
Yes but it only takes one instruction - an adjustment to the stack pointer.
Build code with both, run it in the debugger, and check the disassembly window.
You'll see the difference in the way the array is indexed.
My guess is that in instruction cycles it's real close.
Mark
|
|
|
|
|
When the array is a local variable in the function, a new array is allocated on every call. However, all that means is the stack pointer is decremented by a different amount (an extra 128 bytes).
As for which is more efficient, the dominant effect will probably be that the local variable array will already be in the L2 cache, whereas the global array may not be on the first access.
However, unless this is in a very very tight loop, I'd be shocked if you noticed any difference in execution time. Of course, the only way to be sure it to run both versions of the code and measure.
|
|
|
|
|
Michael Dunn wrote: As for which is more efficient, the dominant effect will probably be that the local variable array will already be in the L2 cache
Cool 
|
|
|
|
|
I'm trying to write a program with visual C++ 6 and I'm wanting to execute an exe file that's contained in my resources.
How do I do this?
|
|
|
|
|
|
Thanks for that. Another question: How do I write code to access the resource?
|
|
|
|
|
|
You can see an article of Mr David Crow about resource on codeproject maybe its some helpful for you
|
|
|
|
|
IDC_EDITXYZ is an edit box in dialog ABC.
IDC_LIST_Start is a list box in dialog ABC.
The first pair of lines below works. The second two pairs do not. The last line works fine.
The second pair gets compile error: 'AddString' : is not a member of 'CWnd'
The third pair gets compile error: cannot convert from 'class CWnd *' to 'class CListBox *'
OK. that's fine, but I am curious as to why the third pair doesn't work. I can understand that AddString is not a member of CWnd, but I would expect a CListBox* pointer would work since AddString is a member of CListBox. So what is the conversion from CWnd to CListBox that it can't do? What does it want?
CWnd* pEditBox = GetDlgItem(IDC_EDITXYZ);
pEditBox->SetWindowText("XYZ");
CWnd* pList = GetDlgItem(IDC_LIST_Start);
pList->AddString("XYZ");
CListBox* pList2 = GetDlgItem(IDC_LIST_Start);
pList2->AddString("XYZ");
m_List_Start.AddString("ABCDEFGHIJK");
-- modified at 18:01 Saturday 14th October, 2006
|
|
|
|
|
The compiler is unable to do a downcast like this. It has no way of knowing it's ok to
convert a CWnd pointer to a CListBox pointer (unless RTTI is used - see below).
If you use RTTI (Run-time type information) you can use:
CListBox* pList2 = dynamic_cast<CListBox*>(GetDlgItem(IDC_LIST_Start));
if (pList2)
pList2->AddString("XYZ");
or with/without RTTI use:
CListBox* pList2 = static_cast<CListBox*>(GetDlgItem(IDC_LIST_Start));
pList2->AddString("XYZ");
Either way, if you are using the control often in the dialog class it may be easier to add
a control object to the class instead
edit: ACK sorry I'm an html idiot - can't use < and > LOL
this code should look better!
Mark
-- modified at 18:18 Saturday 14th October, 2006
|
|
|
|
|
It wants this:
CListBox* pList2 =(CListBox*)GetDlgItem(IDC_LIST_Start);
pList2->AddString("XYZ");
The programmers are not humans. They are something far more superior.
Maybe some day I will too stop being a human and become one of them,
one of the X-men (or X-X-men Extraordinary-Ex-men).
|
|
|
|
|
You can declare a variable for CListBox its easy and also AddString is a function in CListBox class
|
|
|
|
|
Hello!
I’m a student and I have an assignment to research about the compatibility between Java and VC++ in socket programming, such that how a client written in Java can communicate smoothly with server written in VC++ and conversely.
I see that when a server application written in Java initialized at port 1234, why can another server written in C++ still initialize at port 1234 (on the same computer)? It need not have been able to initialize at port 1234. Why does it happen? How to avoid this? What conditions that coder has to obey when coding client and server application in the two different programming languages?
I hope someone can help me to solve this problem.
Thanks a lot.
what will be, will be ...
|
|
|
|
|
lchild385 wrote: ...why can another server written in C++ still initialize at port 1234 ...
What do you mean "initialize at port"? In C++ if you try to bind to (or listen on) the same port
you should get a WSAEADDRINUSE error.
Mark
|
|
|
|
|
Thanks very much and sorry for the confusion. I mean it "create" or "bind" too. I know that error'll arise if i bind two server program (written in VC++) to the same port. But how about one server program written in VC++ and one written in Java? What's happens if i bind them to the same port? When's it make error and when it does not? Anyone has ever meet this problem may hint me to solve it. Thanks a lot! (I've just begun to learn socket programing, so if i make mistake or confusion, please sympathize)
|
|
|
|
|
Should still be an error. If java and c++ allowed you to bind to or listen on the same port then
how would the system know where to route the connection request to?
The port is a system-wide resource, not part of java or c++. A socket port by definition defines
the APPLICATION that is bound to the socket.
Mark
|
|
|
|
|
I tried some sample programs in C++, VC++ and Java to check if two server can bind to the same port, but they failed, they got the conflict error.
But i found out on some web-sites that some time, on window 2000/XP platform, two instances of server can still bind to the same port, (it looks like an error of OS rather than programing error, i think so). In this case, only one instance of server can get traffic from clients. But the other instance, instead of failing in binding, it still runs and gets the listening state. T_T
Do you know more about this problem?
(And thank you very much, Mark)
|
|
|
|
|
lchild385 wrote: But i found out on some web-sites that some time, on window 2000/XP platform, two instances of server can still bind to the same port, (it looks like an error of OS rather than programing error, i think so). In this case, only one instance of server can get traffic from clients. But the other instance, instead of failing in binding, it still runs and gets the listening state.
I'm not sure how that would/could/should work either
It IS possible to bind to the same port using two different protocols...for example, you
can use UDP on the same port number a TCP socket is "listening" on.
lchild385 wrote: (And thank you very much, Mark)
You're welcome! (I don't think I've helped any haha)
Mark
|
|
|
|
|
Hi, now is exactly my problem. I have server program JServer and client program JClient in Java language, and I have server program CServer and client program CClient in VC++ language too. The problem is two server programs JServer and CServer can bind normally to the same port (use the same protocol TCP) and client program JClient and CClient can communicate normally with the correlative server program like two independent system (JClient with JServer, CClient with CServer).
I have to research the reason and the way to avoid this problem when programming. But to now, I have not ever coding such program to check if this problem can happen (all my programs get conflict error). But this problem has really happended (my lecturer checked it).
Maybe this problem is so reraly to happend and there are not much people met it. T_T
( What should i do now, my God!!!!!!!!!!!!!!!!! )
(i don't know why my account (lchild385) becomes hapc @_@. who is hapc???)
|
|
|
|
|
hapc wrote: The problem is two server programs JServer and CServer can bind normally to the same port (use the same protocol TCP) and client program JClient and CClient can communicate normally with the correlative server program like two independent system (JClient with JServer, CClient with CServer).
I still don't believe it unless someone is setting it up to be able to
From MSDN:
By default, a socket cannot be bound (see bind) to a local address that is already in use. On
occasion, however, it can be necessary to reuse an address in this way. Since every connection is
uniquely identified by the combination of local and remote addresses, there is no problem with
having two sockets bound to the same local address as long as the remote addresses are different.
On Windows Sockets 2 you can use setsockopt() to set SO_EXCLUSIVEADDRUSE to TRUE.
Check this info: Using SO_EXCLUSIVEADDRUSE[^]
Mark
|
|
|
|
|
p.s.
who is hapc???
|
|
|
|
|
Hi,
I am trying to copy to the clipboard a part of an image. I know all the pixels that should be copied and I can create a HRGN / CRgn object that represent the region that should be copied.
I know that I should use the SetClipboardData(UINT uFormat, HANDLE hMem).
I need help with which format and hMem should I use. How do I create the HANDLE that will represent only the region that I wish to copt and not the hole image?
Thanks,
Yaron.
|
|
|
|
|
An easy way is to create a new bitmap the size of the region rect.
Step through each source pixel in the region rect and use PtInRegion() to determine if it's in
the region. If it is, copy the pixel to the new bitmap. If not write a background-color pixel
to the new bitmap. Then place the bitmap on the clipboard.
Another way is create a new bitmap (DIB section) the size of the region rect.
Select it into a memory DC
Use StretchBlt() or whatever to draw the source region rect to the dc.
If the region is not rectangular go through the pixels using PtInRegion() like above to
remove the pixels outside the region.
Mark
|
|
|
|
|
Thanks for your help.
This is probabely a silly question, but placing a background-color will cause the image to be copied with a background in the specified color and not as cliped one, right?
Anyway this is what I did and I tried to set those pixels to be transparent but it didn't work for me for some reason (I set the rgbReserved member of RGBQUAD to 0 and also tried 255), I still get the color I have set as a bg. What should I do so that when I paste the image, the bg will be transparent?
thanks,
Yaron.
|
|
|
|
|
That would depend on which function you use to blit it to the screen.
Regular GDI you have to use an AND mask to get transparency.
GDI+ you can use the alpha channel byte.
Mark
|
|
|
|