   how to know if a array changed.
  int *pl = new int[size];void fun(int * array);how to know if pl changed in fun(pl)?  any anwser is good.a simple way:redefine function void fun(const int * array) but it is not good. because void fun(int * array) is usually function that others peole provide you.I offer a stupid way. copy the array and then detect every item after fun() call.   
  Wrong forum [smile]I dont think there is a language-specific way to do what you want - exactly what happens inside fun() is unknown. In a trivial example case you could probably implement something, but in a more "real world" scenario you don't always know what the body of the function did - either because you dont have the code or simply because its complex code that might not always do the same thing.If you're not declaring the input as const then its probably safest to assume that it DID modify it. If a function knows its never going to change the input data then it should declare itself as const - just out of good practice as much as anything else.hthJack   ;  You don't know for sure, but the function should be documented, especially if it changes its parameters. If you really need to know I'm sure you could calculate a hash of the array, and compare the "before" hash and the "after" one.   ;Quote:Original post by rip-offIf you really need to know I'm sure you could calculate a hash of the array, and compare the "before" hash and the "after" one.You would need a unique hash for every unique array, so unless you have some kind compression the hash would be just as big as the actual array. I think it would be much easier to just copy the array before the function and compare the two after the function.   ;  You are all trying to do this the hard way.  See functions have return values for a few reasons, one to return values that you need, the other is to give you information about what a function did.so change it like this:bool fun(int * array){    // if you change the array  return true;    // if you don't change the array  return false;}then all you do is check the return value when you call your function, very easy.theTroll   ;Quote:a simple way:redefine function void fun(const int * array) but it is not good. because void fun(int * array) is usually function that others peole provide you.TheTrollIf you can change not only the functions return type and it's actual implementation, so the return type indicates alteration, then const is the better alternative.   ;Quote:Original post by TheTrollYou are all trying to do this the hard way.  See functions have return values for a few reasons, one to return values that you need, the other is to give you information about what a function did.so change it like this:*** Source Snippet Removed ***then all you do is check the return value when you call your function, very easy.theTrollQuote: edited for spellingbecause void fun(int * array) is usually a function that other people provide you.------------------------------------------------------Quote:You would need a unique hash for every unique array, so unless you have some kind compression the hash would be just as big as the actual array. I think it would be much easier to just copy the array before the function and compare the two after the function.Not really. The chances that any changes the function might make to a given array would make the new hash return the same as the previous hash are quite low for decent hashing algorithms. I am well aware of the limitations of hashing. For many uses, the precision of your method may not be required.   ;Quote:Original post by rip-offNot really. The chances that any changes the function might make to a given array would make the new hash return the same as the previous hash are quite low for decent hashing algorithms. I am well aware of the limitations of hashing. For many uses, the precision of your method may not be required.Of course hashes will work in most cases, the problem is when it actually fails and produce two equal hashes. Using hashes will just introduce a potential bug in your application. As an example he used an array of integers, which most likely isn't so expensive to copy or compare. Unless his profiler tells him that it's a bottleneck he should keep the version which always work, even if it's a little slower.I can't think of a real situation where I need to know if a function actually modifies its parameter. Just assume int * array gets modified and const int * array doesn't, that should be the case anyway if your user's code is const-correct.   ;  Point taken, if you depend on the hashes you will get burned. However, what is the purpose of the function. As I stated originally, its documentation should really mention it if it modifies its arguments. I do know that even if it says it doesn't that its not a guarentee that the code will follow suit.Using other peoples code means looking at the documentation, and if the documentation fails and you have access to it, the source. If you cannot rely on the function not to change its arguments then you may end up reproducing the functionality by yourself if it is vital the array remains as it was.I'm not sure which the OP wants to know more:0) does the function change the array? ( any ways to find out )1) what to do with a function that may modify its arguments.   