Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
void copy(int *s,int *d,int slength){
int i=0;
for(i=0;i<slength;i++){>
d[i]=s[i];
}

}
Posted
Updated 26-May-12 0:02am
v2
Comments
Mehdi Gholam 26-May-12 6:02am    
Sound like homework.
Sandeep Mewara 26-May-12 13:19pm    
+1 homework...

The function (apparently it is not a method) copies slength int values from the source memory area (pointed by s) to the destination memory area ( pointed by d).
The function (implicitely) assumes that:
  • s is a valid pointer to a memory area of size equal to (or greater than) slength.
  • As well, d is a valid pointer to a memory area of size equal to (or greater than) slength.
  • The memory areas pointed by s and d don't overlap.

If one of these requirements does not hold then the caller is in big troubles.

Note that slength name is a bit misleading.
 
Share this answer
 
Comments
Chuck O'Toole 26-May-12 9:21am    
That covers it nicely +5
CPallini 27-May-12 13:24pm    
Thank you.
nv3 26-May-12 9:57am    
Thorough answer. Got my 5 although I think that homework should be done by the student ;-)
CPallini 27-May-12 13:24pm    
Thank you.
(I'm not sure it is homework. It would be a poor example)
Code-o-mat 27-May-12 4:21am    
My 5 also, completely agree with nv3.
Ignoring CPallini's rather complete answer and the fact it's either homework or a job interview question how would YOU solve the problem? Apart from asking online about it that is.

Say you can't work out what the functionality or problems with this code are. Do what most programmers would do who haven't been able to reason about what happens with the source code and:

- write a set of tests for the function. Be systematic - try sticking zeroes in for -each parameter in turn, maybe a few random values. As there are pointer parameters try pointing at different arrays of various sizes. Try small values of slength, try massive values.

- run the tests and see where they fall over. When you see what crashes see what happens to the code under a debugger.

- look at the function name, bit of a pointer (pardon the pun) there. Find standard functions in the C or C++ runtime libraries with similar signatures and functionality. Look them up online and see what their limits are.

FFS read about it, think about it, try it!

End of sermon, you can stop reading now.

PS: This is the sort of problem that mothers give their children when they want them to grow up to become Java programmers.
 
Share this answer
 
Certainly there is no problem with this function. What this function does is it copies elements(here integers)of a source array to a destination array.
So, tell us what happened, I mean if you have any problem with this function what is it?
I am sure the error would occur only because of parameters passed into the function, since they are pointers. You need to handle them carefully. You can face problems if your pointer was not initiated properly or and many more issues.
So tell us what problem have you encountered in using this function?
 
Share this answer
 
Comments
eng.Fadialia 26-May-12 8:23am    
Thank you very much..but I haven't any problems,just I want to know generally possible problems(bugs).
Can you demonstrate with example how pointer is a possible problem?
stib_markc 28-May-12 0:34am    
Pointer or any possible entity in a language or language itself is never a problem. The problem is human perception and less knowledge. If you use pointer correctly then no problem will arise and also logic will be easy to implement. So, if you want to know what possible errors are with pointers, start writing some programs and find out if any errors and then post here if you cannot understand why error is happening. For more about pointers, try googling and after some basic tutorials look up this link
Pointers Usage in C++: Beginners to Advanced[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900