Thread: C4789: destination of memory copy is too small - related to pointers

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    36

    C4789: destination of memory copy is too small - related to pointers

    Code:
    int tube_mag[6] = {0,0,0,0,0,0};
    int *pMagPointer = &tube_mag[5];
    
    
    
    
    void LoadBall() {
    	if (tube_mag[6] == 0)	{
    	
    	pMagPointer = &tube_mag[5];
    	tube_mag[6] = *pMagPointer;      //Error this line
    	pMagPointer = &tube_mag[4];
    	tube_mag[5] = *pMagPointer;
    	pMagPointer = &tube_mag[3];
    	tube_mag[4] = *pMagPointer;
    	pMagPointer = &tube_mag[2];
    	tube_mag[3] = *pMagPointer;
    	pMagPointer = &tube_mag[1];
    	tube_mag[2] = *pMagPointer;
    
    	tube_mag[1] = 1;
    
    	DisplayMenu();
    	}
    While trying to understand why pointers are actually used and for what, I created a little test program. The above code is supposed to simulate a tube magazine for a tube that can be filled with tennis balls. Basically if a new Ball is loaded, the complete row is being pushed back. The code works as intended, however I get the following error in the line marked:

    C4789: destination of memory copy is too small
    However the compiler lists the compilation as "successful". Also I did only post the relevant parts of the code (initialization of the pointer and array) and the function they are used in.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Your "error" message was actually a warning message -- you should distinguish between the two. If it had been an error message the code would not have been compiled.

    The reason for the warning is that you are straying beyond the end of your array. The 6 elements of your array are numbered tube_mag[0] through tube_mag[5]. You are assigning a value to tube_mag[6] which is the next memory location following the array.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Ah now I see. Guess I mixed up amount of array positions with their actual position names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory and pointers
    By tull2 in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 09:15 AM
  2. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  3. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  4. Faster way to copy memory?
    By @nthony in forum C Programming
    Replies: 4
    Last Post: 09-16-2007, 03:47 PM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM