pointer question

This is a discussion on pointer question within the C++ Programming forums, part of the General Programming Boards category; my program kept crashing for some reason and i narrowed it down to the following code (i know it has ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    pointer question

    my program kept crashing for some reason and i narrowed it down to the following code (i know it has something to do with the pointer):

    Code:
    struct _cTemp
    {
    	unsigned int nSize;
    	char *sData;
    };
    
    int main()
    {
    	_cTemp cTemp;
    	cTemp.nSize = 0x41414141; // set uint to 'AAAA'
    
    	cout << sizeof(_cTemp) << endl;
    
    	cTemp.sData = new char [100];
    	memset(cTemp.sData, 'A', 100); // set char array to A's
    
    	cout << sizeof(_cTemp) + 100 << endl;
    
    	cout.write((char *)&cTemp, sizeof(_cTemp) + 100);
    	cout << endl;
    
    	delete[] cTemp.sData;
    
    	return 0;
    }
    the cout.write function always returns a bunch of random garbage when its meant to return 'AAAAAA....', also im not 100% sure if the size im using is right.

    i know the sizeof(_cTemp) = 8 (uint+pointer), when i assign the pointer 100 chars, would the size be 108? (im assuming that the size of the pointer doesnt change?).

    anyway, why isnt it displaying 'AAAA..'? it shows the first 4 A's for the uint but not for the char array.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is what is going on. You are casting the address of cTemp to a pointer to a char, and then writing that data to the stream. So your first four bytes are going to be the nSize data member, then your next four will be the address of your allocated memory. Beyond that, you are just outputting whatever is in memory past that. Could be anything. Run it in debug and then release and you should see different results. Do you use Visual Studio? If so when you're debugging, use the memory watch window and go to the address of your object and watch it as you step over the lines. You will see what I am talking about. I think what you meant to do is:

    Code:
    cout.write(cTemp.sData, 100);
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    I'm not really familiar with the way you would set everything to AAA. But I would do it like this.
    Code:
    for(int i=0;i<99;i++)
          cTemp.sData[i] = 'A';
    cTemp.sData[100] = '\0'; //terminate char array
    /

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    oh i always thought that the pointer and the data being pointed to was always sequential..

    thanks for pointing that out, saved me quite a bit of trouble

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    actually, what im trying to do is convert a struct into a char array so i can send send() it using winsock.

    it works fine when i use char sData[100], but if i use a pointer, the memory isnt sequential so its not working as i expected.

    oh and btw:

    Code:
    for(int i=0;i<99;i++)
          cTemp.sData[i] = 'A';
    cTemp.sData[100] = '\0'; //terminate char array
    is wrong, it should be

    Code:
    // the array is 0-99 not 0-100
    for(int i=0;i<99;i++)
          cTemp.sData[i] = 'A';
    cTemp.sData[99] = '\0'; //terminate char array

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If you want to send this structure do this
    Code:
    send(Socket,  &cTemp.nSize, sizeof(int), 0);
    Send(Socket, cTemp.sData, 100, 0);
    You also might want to use htonl() on the size to convert it to network byte order and then use ntohl on the receiving end. You only need to do this if the other application is expecting this.
    Last edited by Quantum1024; 11-21-2005 at 05:00 AM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    i wasnt sure if the sent messages were recv'ed in sequential order, so i tried to limit every send to 1 packet. took me a few hours to get everything working, but its all good now.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by X PaYnE X
    i wasnt sure if the sent messages were recv'ed in sequential order, so i tried to limit every send to 1 packet. took me a few hours to get everything working, but its all good now.
    tcp will make sure your data arives in the same order that it was sent but you shouldn't depend on it being all recv()ed in one call.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    thats strange, in a different client/server program i had, i sent 2 packets and it was received in the wrong order and since then i basically assumed i had to handle the sequencing manually.

    not sure what could have caused it, probably the same thread accessing a global var at the same time.

    another question if possible, is there a maximum limit on send/recv? (im currently limiting it to 1kb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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