memcpy working strangely in msvc 2005

This is a discussion on memcpy working strangely in msvc 2005 within the C++ Programming forums, part of the General Programming Boards category; Hi, im trying to create a file mapped object, creating it works fine, and using lstrcpy i can copy characters ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    memcpy working strangely in msvc 2005

    Hi, im trying to create a file mapped object, creating it works fine, and using lstrcpy i can copy characters to the file object with no problems. However, when i use memcpy I run into problems. This is the exact code I am using in Dev-Cpp and it works bueatifully
    Code:
    int main(int argc, char *argv[])
    {
        int error = 0;
        initialize();
        TCHAR s1[5] = {1,2,3,4,5};
    	switch (error)
    	{
    		case 0:
    		{
    			if ( WaitForSingleObject(m_hMutex, dwMaxWait) == WAIT_OBJECT_0 )
    			{
    				memcpy(m_pViewOfFile, s1,5);
    				ReleaseMutex(m_hMutex);
    			}
    			break;
    		}
    		case 4:
    		{
    			printf("yay\n");
    			break;
    		}
    	}
    
        TCHAR s2[dwMemoryFileSize];
        if ( WaitForSingleObject(m_hMutex, dwMaxWait) == WAIT_OBJECT_0 )
        {
            memcpy(s2, m_pViewOfFile, 5);
            ReleaseMutex(m_hMutex);
        }
        for (int i = 0; i < 5; i++)
        {
            printf("%d ",s2[i]);
        }
        printf("\n");
        getchar();
    	destroy();
        return 0;
    }
    the console shows 1 2 3 4 5 as expected
    However when I try to use this exact code in mscv (the initialization is slightly different, however it did work when i was using lstrcpy, the only thing changed is lstrcpy to memcpy) i get a rather garbled result.

    1 2 52227 542428 542428

    it worked fine when i was using lstrcpy, and works fine in devcpp. Anyone have any ideas.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    TCHAR varies between char and short int depending on whether you're compiling with UNICODE or not.

    If you use the UNICODE aware macro names for the relevant functions, then it should do the right thing with either compilation mode.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  2. If you must port to .NET 2005, read this thread.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-22-2007, 06:51 AM
  3. VC++ 2005 /FC apparently not working
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2006, 09:02 AM
  4. Warning to all those wishing to port to .NET 2005
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-07-2006, 09:29 PM
  5. Visual C++ 2005 Express Edition: Breakpoints not working
    By Bird Killer in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2006, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21