Thread: Copying array's

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Copying array's

    I need help with this. Im trying to copy one array into another, one by array subscript notation and the other by pointer notation.
    I keep getting the error during runtime "stack around variable array was corrupted" My JIT does not work.keeps telling me registration is incorrect so i cant use it,nor can I fix it. Anyway heres the code. any help would be appreciated.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void copy1( double *, const double * );
    void copy2( double *, const double * );
    
    int main()
    {
        double array[10] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 };
    	double array1[10] = {0.0};
    	double array2[10] = {0.0};
    
    	cout << fixed << setprecision(1);
    
    	copy1 ( array1, array );
    	cout << "array1 = " << *array1 << ' ' << endl;
    
    	copy2 ( array2, array );
    	cout << "array2 = " << *array2 << ' ' << endl;
    
    	return 0;
    }
    //copy array to array1 using array notation
    void copy1( double *s1, const double *s2 )
    {
    	for ( int i = 0; ( s1[i] = s2[i] ) != s2[10]; i++ )
    		;
    }
    //copy array to array2 using pointer notation
    void copy2( double *s1, const double *s2 )
    {
    	for ( ; ( *s1 = *s2 ) != s2[10]; s1++, s2++ )
    		;
    }
    big146

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for ( int i = 0; ( s1[i] = s2[i] ) != s2[10]; i++ )

    > for ( ; ( *s1 = *s2 ) != s2[10]; s1++, s2++ )

    Your array only has 10 elements, running from 0 to 9. There is no s2[10].

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Do you know about the function memcpy()? That would certainly make your job easier. If you are doing this as a pure educational exercise then ignore me.
    "...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

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    No I havent gotten that far yet. I am doing this as an educational thing.Im just learning. The book Im reading has an exercise that says to copy one array into another using your own functions. So im trying to come up with my own function. So far no luck. It starts to copy but then says " stach around variable array has been corrupted". So right now im a little lost.

    Ill take a look at that s2[10]. Maybe thats where im getting the prob.

    thks for the reply.
    big146

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by big146
    No I havent gotten that far yet. I am doing this as an educational thing.Im just learning. The book Im reading has an exercise that says to copy one array into another using your own functions. So im trying to come up with my own function. So far no luck. It starts to copy but then says " stach around variable array has been corrupted". So right now im a little lost.

    Ill take a look at that s2[10]. Maybe thats where im getting the prob.

    thks for the reply.
    Okay, that's what I was thinking. As previously pointed out you are going past the array boundary which is a very big problem. Remember we are dealing with zero-based indexing so if you store 10 elements the indices run from 0 to 9 inclusive. Also, just for fun, think about adding a third parameter to your function that dictates how many elements to copy.
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Is this poor coding? copying arrays of doubles
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 02:34 PM
  3. Copying character arrays
    By neandrake in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 06:32 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 04:09 PM
  5. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 04:22 PM