copy one array to another

hey guys i need a litle help
why this code will not work??
plz help me
a = b works but it is a shallow copy ??

how to make a deep copy of this ...plz help

1
2
3
4
5
  void copy(int *a, int *b, int b_size) {
     if(b_size == 0) return;    
     *a = *b;
     copy(++a, ++b, b_size-1);
}
What specifically does not work? Does it crash? Does it break the speed limit and get a ticket? Does it steal your wallet? You need to be specific.
when i call this function all the code after this function is skipped and the program closes with a return value that is very large
Have you tried debugging it? At the very least, put a statement to output the parameters when the function starts.

Do you need this function to be recursive? If not, you should just use a normal loop, or use std::copy.
no i need to solve it via recursion
yes the function is called but i think there is some problem......
i wrote an output statement and that was shown to the console but the rest of the function does not
How do you define the arrays which you feed to copy and how do you determine the initial size fed to the function?
Please show the code for your calling function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>

using namespace std;

void copy(int *a, int *b, int b_size) {
	cout << "Copy Function called \n";
     if(b_size == 0) return;    
     *a = *b;
     copy(++a, ++b, b_size-1);
}


int main() 
{
int a[] = {1,2,3,4,5};
int *d = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
cout << sum;


int *b;
cout << "Copying array \n";
copy(b,a,5);//this does not work
//b = d;this works
cout << "Array copied \n";

for (int j = 0; j < 5; ++j)
cout << *(b+j) << " ";

system("pause");
return 0;
} 
b is a pointer which points to... nothing. Dereferencing it results in undefined behavior, and that is what you are experiencing. You can't store something in some place that doesn't exist.
As cire mentions, the pointer b is not pointing to anything. You need to allocate memory for the new array:

1
2
3
4
5
6
7
int *b = new int[5];

//
//
// and later don't forget to free the memory by calling delete.

delete [] b;


thanks bro
can you plz tell me about shared_pointers in c++
actually i'm making a stack class and i made it using pointers and it works fine for me ...
but now i want to make this via shares_ptr to get rid of possible memory leaks
plzzzz guys help me .....

i m building the stack class by using linkedlist
Topic archived. No new replies allowed.