|
string s1 = "shilpi";
string s2 = "shilpi";
if(strcmp(s1.c_str(),s2.c_str())==0)
{
printf("Hi");
}
what i wanna say is this ,try it
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
that's crap. why in hell do you cast the strings objects to C-style strings to use the C runtime to compare them, when the string class provide various comparison operators ?
|
|
|
|
|
It is up to you ..i m just giving u one solution
Yes U Can ...If U Can ,Dream it , U can do it ...ICAN
|
|
|
|
|
i'm not the guy who was asking. and me too is trying to give best answers...
so please read the other posts when you have answers to see if it has not already been submitted, or if someone gave a better answer
|
|
|
|
|
Hello,
how to check if an object has a valid adress or is valid pointer?
I got errors when i try to clear CMapStringToOb!
tanks for help!
termal
|
|
|
|
|
I have the same problem.Trying to check a pointer validity i get Access Violation
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if ( (m_hWnd != NULL) && (m_pRightPaneFrame !=NULL) )
{
------------->if (m_pRightPaneFrame->m_pSplitterView != NULL) ----> I get Unhandled exception in XXXXX : Access Violation
{
}
}
}
Is there anyway to check an unallocated pointer for validity without getting errors ? What seems to be wrong here ?
Regards,
sdancer75
|
|
|
|
|
In practice, you can't. The only way to know whether an address is valid is to try it; if not valid you'll get an access violation. That still won't tell you whether the pointer value actually points to what you expect it to.
The only answer is to be disciplined. Avoid pointer arithmetic whenever possible. Prefer using classes to raw data structures, so that bounds can be checked. Ensure all variables are initialised. Use smart pointers. Do these, and the chances of random memory corruption are much reduced.
|
|
|
|
|
As stated by Mike Dimmick, be disciplined.
However, IMHO, you don't need avoid pointer usage: you have only to handle pointers with care. For instance, always initialise pointers, either with NULL or with a valid memory area. Remember also to set to NULL a pointer immediately after you've released the pointed area.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hello,
thanks for answers!
regards
termal
|
|
|
|
|
As Mike Dimmick said there are no 100% sure methods to verify a pointer. Nevertheless here are some basic techniques that could be used to increase the odds.
Some basic check that works on all pointers:
<br />
VERIFY(NULL != lpSomePointer)<br />
VERIFY(0 == (lpSomePointer & 3));<br />
At this point it *might* be a pointer because the number is an even number which is not NULL.
Microsoft supplies the functions IsBadCodePtr() and IsBadWritePtr() for checking the memory location. An example of using it on a function pointer might be:
if(FALSE == IsBadCodePtr((FARPROC)(LPVOID)pInstructions))<br />
{<br />
}
While using it on a pointer to a writable address might look like:
if(FALSE== IsBadWritePtr((LPVOID)pInstructions,1))<br />
{<br />
}
If you wanted to create your own verification to check if the pointer is pointing to a memory address which is executable... for a function pointer you might do something like:
MEMORY_BASIC_INFORMATION mbi;<br />
if(VirtualQueryEx(GetCurrentProcess(),(LPVOID)pInstructions,&mbi,sizeof(MEMORY_BASIC_INFORMATION)))<br />
{<br />
if(mbi.Protect & PAGE_EXECUTE_READ && mbi.State & MEM_COMMIT && mbi.AllocationProtect & PAGE_EXECUTE_WRITECOPY && mbi.Type & MEM_IMAGE)<br />
{<br />
(*this.*MyMemberFunction)(someval);<br />
}<br />
}
If you wanted to write some extremely hacked-up NON-PORTABLE and compiler specific code to do some additional checking on a C++ member function you might look for some prologue asm instructions:
For example:
push esi
push edi
<br />
unsigned char ASM[] = "\x56\x57\x8B\x7C\x24\x0C";<br />
PDWORD pAddress = (PDWORD)&SomeFucntion;<br />
DWORD_PTR pInstructions = *pAddress;<br />
if(memcmp((LPVOID)pInstructions,ASM,sizeof(ASM))<br />
{<br />
}<br />
There is no guarantee that your compiler will produce a specific asm prologue... or the function may become _inline in which case this technique would fail.
I cant really think of anymore methods... and to be honest the ones listed above do not make any guarantees.
Best Wishes,
-Randor (David Delaune)
|
|
|
|
|
Hello Randor,
thanks for your info!
regards
termal!
|
|
|
|
|
Don't use IsBadXxxPtr() , they don't actually do what their names say. Check out Raymond Chen's and Larry Osterman's blogs as they have talked about why you should avoid them.
|
|
|
|
|
Don't use IsBadXxxPtr() , they don't actually do what their names say. Check out Raymond Chen's and Larry Osterman's blogs as they have talked about why you should avoid them.
|
|
|
|
|
The client connect to the sever but a exceptions ocurr in this funtcion.
CAsyncSocket::DoCallBack(WPARAM wParam, LPARAM lParam)
{
pSocket = CAsyncSocket::LookupHandle((SOCKET)wParam, FALSE);
if (pSocket == NULL)
{
// Must be in the middle of an Accept call
pSocket = CAsyncSocket::LookupHandle(INVALID_SOCKET, FALSE);
/***************************************************/
ASSERT(pSocket != NULL);// in this statement
//why the pSocket is NULL
/***************************************************************/
pSocket->m_hSocket = (SOCKET)wParam;
CAsyncSocket::DetachHandle(INVALID_SOCKET, FALSE);
CAsyncSocket::AttachHandle(pSocket->m_hSocket, pSocket, FALSE);
}
}
msn:xxblinux@hotmail.com
|
|
|
|
|
|
|
the diff b/w long pointer to string and pointer to string and where each one is used
|
|
|
|
|
what is your question exactly ?
anyway for what i understand, long pointer specifies that the pointer is as wide as a long
|
|
|
|
|
hari prasad sathpadi wrote: the diff b/w long pointer to string and pointer to string and where each one is used
There is no difference on 32 (or 64) bit Windows. It is a legacy from 16-bit days.
|
|
|
|
|
In modern Windows, none at all. LPSTR and PSTR are fully interchangeable.
On 16-bit Windows (and DOS), you had two sorts of pointer, near and far . A near pointer was 16 bits and could only reference data in the same segment as currently set, while a far pointer was 32 bits and could reference data in any segment. Data in a DLL would never be in your program's segment, so you'd need a far pointer to address it.
Segmented addressing was a really big pain and it was a huge relief to most programmers when 32-bit Windows moved to a flat 32-bit virtual address space.
A 'long' pointer in 16-bit Windows was a far pointer (32-bits, the same size as the long type). LPSTR was a typedef for char far* .
The old datatypes survive in 32-bit and now 64-bit Windows largely for source-level compatibility with 16-bit code. Consider it an odd quirk.
|
|
|
|
|
Hello,
I would like to compare two strings: i used:
if (s1==s2) {
I have no problem to compile, but even when s1=s2 it doesnt work...
|
|
|
|
|
|
ok. thx. but i have look about explanation about this command and it seems that it copies string, it doesn´t compare them.... Am i right? How am i suppose to use it?
|
|
|
|
|
if you want to compare them use strcmp or use a CString object to create your string(the "==" operator is overwritten by CString). Direct comparison doesn't works for arrays.
|
|
|
|
|
s1 is CString
then can use
CompareNoCase
Compare
|
|
|
|