Thread: memcpy strange

  1. #1
    -
    Join Date
    Feb 2003
    Posts
    47

    memcpy strange

    Here is the code:
    Code:
        
    	// set the array with values of 0
    	for (int index = 0; index < NumElements -1; index++) {
    		OriginalArray[index] = 0;
    	}
    	
    	// forget about pure random at the moment
    	for (int index = 0; index < NumElements -1; index++) {
    		OriginalArray[index] = rand() % NumElements;
    	}
      
        // stub for displaying numbers
    	for (index = 0; index < NumElements -1; index++) {
    	    cout << OriginalArray[index] << '\t';
        }
    	
        // copy the OriginalArray to the global RandomArray
        memcpy(RandomArray, OriginalArray, NumElements-1);
        cout << "\n\n\n\n";
        
        // stub for displaying numbers
    	for (index = 0; index < NumElements -1; index++) {
    	    cout << RandomArray[index] << '\t';
        }
    Here is some sample output:
    Code:
    6    4    3    9    6    4    1    0    6
    
    // after 'copy'
    6    4    3    0    0    0    0    0    0
    What the? - rove

    I cant figure out what is wrong, anyone see anything i cant?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > memcpy(RandomArray, OriginalArray, NumElements-1);
    memcpy copies bytes, not ints
    Try
    memcpy(RandomArray, OriginalArray, sizeof RandomArray);
    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.

  3. #3
    -
    Join Date
    Feb 2003
    Posts
    47
    Thanks for the input, but that seems to make it worse, now only displaying 1 random number and following 0's. Is there a way to copy one element at a time and just loop it?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The third argument to memcpy is the number of bytes to copy so you want:
    Code:
        memcpy(RandomArray, OriginalArray, NumElements * sizeof(RandomArray[0]));
    I'm suspicious of your use of NumElements - 1. Typically we don't want the - 1 because we are using the < operator and therefore for an array with 5 elements we get:
    Code:
    	for (int index = 0; index < 5; index++) {
    		// index goes from 0 to 4
    		OriginalArray[index] = rand() % NumElements;
    	}
    while with - 1 we get:
    Code:
    	for (int index = 0; index < 5 -1; index++) {
    		// Index goes from 0 to 3
    		OriginalArray[index] = rand() % NumElements;
    	}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Thanks for the input, but that seems to make it worse, now only displaying 1 random number and following 0's.
    See, I thought from your variable name that it was an array in scope and not just a pointer to an array
    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.

  6. #6
    -
    Join Date
    Feb 2003
    Posts
    47
    Thanks that works now, yes your right the -1 was not ment to be there, i copied a stub from somewhere else in the code nice job saved me lots of headaches.

    edit: salem your on the money 99% of the time ill forgive you this once!
    Last edited by robid1; 08-22-2004 at 12:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  2. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 11:35 AM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 04:44 PM