Copying a short through memcpy

This is a discussion on Copying a short through memcpy within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> // memcpy #include <iostream> using namespace std; int main () { char *x = new ...

  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
    21,992
    Moved to C++ programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    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, 02:55 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 03: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

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