Thread: Copying a short through memcpy

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    Copying a short through memcpy

    Code:
    #include <stdio.h>
    #include <string.h> // memcpy
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
      char           *x = new char [20];
      unsigned short y  = 150;
    
      memcpy (x, (const void *)&y, 2);
    
      printf ("\n%d\n", x[0]);
      printf ("\n%d\n", x[1]);
    }
    The output of the above program is -106,

    and when I allocate memory to y and then copy it using memcpy, everything works properly.

    but I want to use y without allocating memory to it. What is wrong with the above program ?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check your headers. I'm not sure about C++ (you're in the wrong forum btw), but in C, if you want to malloc() memory, you have to include stdlib.h, not string.h.

    Edit: I was referring to new in your program, being approximately the same as malloc() in C. Malloc needs stdlib.h, which you did not have.

    Glad you got it working, though.
    Last edited by Adak; 09-14-2010 at 06:07 AM.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Thanks for replying,

    string.h has been included for memcpy !

    Yes by mistake I posted this in the wrong forum, but IMO this question has some pointer logic problem and writing the same thing in C will not solve it.
    Last edited by AnishaKaul; 09-14-2010 at 04:49 AM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    The binary code of one of the 2 bytes in y is 10010110. If this byte is regarded as a signed char, its value is just -106.

    BTW: A plain char might be regarded as signed char or unsigned char, depending on the compiler you are using.
    Last edited by orientuser; 09-14-2010 at 05:07 AM.

  5. #5
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by orientuser View Post
    The binary code of one of the 2 bytes in y is 10010110. If this byte is regarded as a signed char, its value is just -106.

    BTW: A plain char might be regarded as signed char or unsigned char, depending on the compiler you are using.
    Many thanks to you for the pointer !
    I modified the program as follows:
    Code:
    #include <stdio.h>
    #include <string.h> // memcpy
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
      unsigned char  *x = new unsigned char [20];
      unsigned short *y = new unsigned short [2];
    
      *y = 255;
    
      memcpy (x, (const void *)&y, 2);
    
      printf ("\n%d\n", x[0]);
      printf ("\n%d\n", x[1]);
    }
    Now the output is 48 and 32 !!
    Shouldn't the output be 255 and 0 ?

  6. #6
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    memcpy (x, (const void *)&y, 2);

    & is redundant.

  7. #7
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by orientuser View Post
    memcpy (x, (const void *)&y, 2);

    & is redundant.
    I am sorry, I actually had posted the incorrect code.
    The correct code is this one:
    Code:
    #include <stdio.h>
    #include <string.h> // memcpy
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
      unsigned char  *x = new unsigned char [20];
      unsigned short y  = 255;
    
      memcpy (x, (const void *)&y, 2);
    
      printf ("\n%d\n", x[0]);
      printf ("\n%d\n", x[1]);
    }
    and it is working very fine.

    Thanks to you

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C++ programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    WHY are you including iostream at all? Geez, pick a language...C or C++! If you're going to use C++ and insist on using C I/O, then use the C++ header cstdio. If you're going to use memcpy, use the C++ header cstring.

  10. #10
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by rags_to_riches View Post
    If you're going to use C++ and insist on using C I/O, then use the C++ header cstdio. If you're going to use memcpy, use the C++ header cstring.
    I did a man memcpy. It showed string.h. I was not aware of the cstdio and cstring headers, otherwise I would have used them instead. Thanks for pointing out !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Structures Inside Structures
    By Mellowz in forum C Programming
    Replies: 1
    Last Post: 01-13-2008, 03:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. copying an object using memcpy
    By RoshanX in forum C++ Programming
    Replies: 1
    Last Post: 04-03-2007, 04:19 PM
  4. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM