Passing strange parameters - is it safe?

I am working on some outdated code, and I came across something that I am not sure is safe - in fact, I am sure it is undefined, but I'm not quite sure why (more or less bad feeling).

For some reason, this code has a class, we will call it A. Class A has an overloaded pre-increment (++) operator, which seems to perform some operations on the value of the pointer it contains (we, ll call this pointer B)

I found a function call in which the user goes to A, and a copy of the link to pointer B with the link removed when using the overloaded pre-increment operator.

foo(++A, *B); 

Since the pre-increment changes the value pointed to by B, and B is dereferenced and used as a parameter in the same call ... is there a problem or should I just leave it that way?

Sorry if this sounds confusing - the code is too complicated to insert, but I did my best to explain the situation. If necessary, I will make some fake code representing the situation, but I hope this will be clear.

In addition, I know that this poor design for this code is passed in a parameter already contained in a separate one, but I would like to understand if this is a problem.

+4
source share
2 answers

The evaluation order of the function arguments is not specified, therefore, if any operation affects the value of another, then this is really a problem.

(In your code, however, this would somehow require B to refer to member A As he wrote, no one suspected that there might be a problem (but you noticed it yourself).)

+7
source

The value of the parameters is calculated before entering the function. The calculation order is not specified. Thus, the code you provided may lead to unexpected results. You can fix it with

 ++A; foo(A, *B) 

but of course, as you said, if B is part of A, then only A. should be passed.

+5
source

Source: https://habr.com/ru/post/1383688/


All Articles