copying struct to char

This is a discussion on copying struct to char within the C++ Programming forums, part of the General Programming Boards category; Hello.. Im working on a dns resolver program and Im having some problems with making the request for dns server.. ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    copying struct to char

    Hello..

    Im working on a dns resolver program and Im having some problems with making the request for dns server..

    the header structure is:

    Code:
    typedef struct {
    	unsigned short id;
    	unsigned short flags;
    	unsigned short qdc;
    	unsigned short anc;
    	unsigned short nsc;
    	unsigned short arc;
    } dnsheader;
    
    int somefunc() {
    	dnsheader *head = makeheader();	//this will create the header, set the values and pass the pointer to it
    
    	//now i want to copy header to char
    	char *buf = new char[1024];
    	memset(buf, 0, sizeof(buf));
    	memcpy(buf, &head, sizeof(dnsheader));
    
    	char *ptr = buf + sizeof(dnsheader);
    
    	//here i will put the rest of data in the buf char
    	return 0;
    }

    What am I doing wrong?

    Thanks a lot for help

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Without knowing any exact compiler errors or which lines numbers errors occur, I think this could be a problem:
    Code:
    char *ptr = buf + sizeof(dnsheader);
    you can do this..
    Code:
    char *ptr = buf;
    or this:
    Code:
    memcopy(ptr, buf, sizeof(dnsheader));
    Last edited by The Brain; 07-02-2006 at 12:36 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > What am I doing wrong?
    All sorts of things potentially.

    1. Assuming that your struct is packed perhaps.

    2. makeheader();
    Does this return a pointer to local memory, or something more permanent (like allocated memory)?

    3. memcpy(buf, &head, sizeof(dnsheader));
    You probably want to copy what head pointed to (drop the &)

    4. memset(buf, 0, sizeof(buf));
    Tells you the size of the pointer (probably 4), not the amount it points to (1024)
    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.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Salem
    1. Assuming that your struct is packed perhaps.
    Some other code that I took a look at had packed struct, but I dont know what that means. Why would I need that? Where could I read more about it?

    Thanks

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Essentially, a compiler is free to add padding between struct members to better optimise the alignment of each data item (in memory) for efficient access.
    http://c-faq.com/struct/padding.html

    This comes as a surprise when people expect a struct to have a certain size. Mostly they print sizeof(myStruct) and ask why it isn't the same as the sum-of-sizes of the members of the struct.

    Packing is a very compiler specific way of squeezing out the padding, at the expense of more complicated code (generated by the compiler).

    Another issue is http://en.wikipedia.org/wiki/Endian
    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. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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