Incompatible integer to pointer conversion assigning to 'BOOL - xcode

I just inherent a new app from a previous programmer and keep on running into a "Incompatible integer to pointer conversion assigning to 'BOOL *' (aka 'bool *') from 'BOOL' (aka 'bool') ".
Code like such as
_backButtonPressed = YES;
_isEdited = YES;
come up with the same error. I tried *(_backButtonPressed) = YES; and
*_backButtonPressed = Yes, but both scenarios crashes the program.
Program still work if I leave it alone, but I wanted to keep the code clean. Any suggestion?

My guess is that the two variables in question were declared incorrectly. For example, _backButtonPressed is probably declared as
BOOL* _backButtonPressed;
but should be
BOOL _backButtonPressed;
The crash occurs because _backButtonPress is not a pointer: if it was, there would be a statement like
_backButtonPressed = (BOOL*)malloc(sizeof(BOOL));
somewhere. If this was not done before you try to assign to
*(_backButtonPressed), then you will see a crash.
It is easy to make the mistake of declaring BOOL* instead of BOOL, because Cocoa objects are always declared with the *
I recommend that you fix the declarations. I am pretty sure everything will work if you do that.

Related

Thread Declaration in C++

I was going through a project code in C. In there, I saw this declaration of thread :
pthread_t ui_thread = (pthread_t) 0;
I didn't understand the part starting from '=' operator. What is it and how can I code the same declaration in C++.
(pthread_t) 0 converts the literal integer value 0 to a thread handle pthread_t. This assumes that such a conversion is possible, and valid, and that this is a meaningful value (probably expected to be "no thread").
The full statement creates a variable ui_thread which is a thread handle of type pthread_t, and initializes it with this value.
In C++, you could probably write the same if you were on a platform where it was valid for C. However, you would be better to use the C++ thread library.
std::thread t;
will create a default-constructed thread handle with no associated thread, which is likely the equivalent to the above.
The (pthread_t) part is known as type casting in C. Also called explicit type conversion. It is just a way for the programmer to inform the compiler that the programmer means for the value (0 in this case) to be treated as the type pthread_t.
The code you have is still valid C++.
In C++11 you can probably just do this:
pthread_t ui_thread{nullptr};

Copy constructor called on *this

I defined a copy constructor for a class A. Due to an unfortunate macro expansion, I ended up compiling the following:
A a = a;
I (eventually) realized this results in a call to A::A(const A& rhs) with this==&rhs.
Why does the compiler allow this? Conceptually I would assume that since a is declared in this statement, it wouldn't yet be available for use on the RHS.
Should I defensively check this==&rhs whenever I define a copy constructor?
I am using gcc version 5.4.0 with -std=c++11.
In a declaration, the identifier being declared is in scope as soon as it appears. There are some valid uses of this, e.g. void *p = &p;
It's normal for the copy-constructor to assume no self-copy, leaving it up to the caller to not make this mistake. Preferably, follow the rule of zero.
It would be better to not write A a = a; in the first place. To avoid this you could get in the habit of using auto, e.g.
#define F(x) auto a = A(x)
#define G(x) A a = x
Now if you write G(a); you silently get the bug, but F(a); fails to compile.

Unreference shared_ptr

I know 2 ways:
// Declared somewhere but not on stack
shared_ptr<X> xptr = make_shared<X>();
xptr = nullptr; //#1
xptr.reset(); //#2
As for me #1 looks better, but what is better from the point of C++
Neither is objectively better. In almost every case, the difference is purely aesthetic.
The only exception that I can think of is writing a generic function template that works with both smart, and bare pointers. only ptr = nullptr is valid syntax for bare pointers.
The assignment and reset member function differ in behaviour only when the pointer is not null.

Uniform initialization syntax or type conversion?

Changing the parens to curly braces seems to produce the exact same behavior in my program, even though semantically they seem to be quite different beasts. Is there a reason (memory usage, performance, etc.) to prefer one?
double pie = 3.14159;
myVal = int(pie); // type conversion using operator()
myVal = int{pie}; // uniform initialization syntax
[edit]
My actual code is a little different from the above example, perhaps that explains the narrowing issues:
int32_t result;
myVal = uint16_t(result); // myVal is between 0 and 65535
myVal = uint16_t{result}; // myVal is between 0 and 65535
First note that what you are doing there is not initialization, is a type conversion followed by an assignment. I strongly recommend C++ casting operators (static_cast in this case) over C casts and these constructor-based castings.
That said, the main difference between uniform initialization and the other is that uniform initialization doesn't allow (See the note) narrowing conversions such these you are doing, float to int. This is helpful when writting constants or initializing variables, since initializing an int with 3.141592654 has no sense at all because the fractional part will be stripped out.
NOTE: I remember the initial proposal for uniform-initialization explicitly stating that it disallows narrowing conversions, so if I had understood it correctly, code like yours should not compile.
I have tested it and seems like compilers emmit warnings about the narrowing conversions instead of aborting compilation. Indeed, that warnings are useful too, and you could allways use a -Werror flag.

General programming - calling a non void method but not using value

This is general programming, but if it makes a difference, I'm using objective-c. Suppose there's a method that returns a value, and also performs some actions, but you don't care about the value it returns, only the stuff that it does. Would you just call the method as if it was void? Or place the result in a variable and then delete it or forget about it? State your opinion, what you would do if you had this situation.
A common example of this is printf, which returns an int... but you rarely see this:
int val = printf("Hello World");
Yeah just call the method as if it was void. You probably do it all the time without noticing it. The assignment operator '=' actually returns a value, but it's very rarely used.
It depends on the environment (the language, the tools, the coding standard, ...).
For example in C, it is perfectly possible to call a function without using its value. With some functions like printf, which returns an int, it is done all the time.
Sometimes not using a value will cause a warning, which is undesirable. Assigning the value to a variable and then not using it will just cause another warning about an unused variable. For this case the solution is to cast the result to void by prefixing the call with (void), e.g.
(void) my_function_returning_a_value_i_want_to_ignore().
There are two separate issues here, actually:
Should you care about returned value?
Should you assign it to a variable you're not going to use?
The answer to #2 is a resounding "NO" - unless, of course, you're working with a language where that would be illegal (early Turbo Pascal comes to mind). There's absolutely no point in defining a variable only to throw it away.
First part is not so easy. Generally, there is a reason value is returned - for idempotent functions the result is function's sole purpose; for non-idempotent it usually represents some sort of return code signifying whether operation was completed normally. There are exceptions, of course - like method chaining.
If this is common in .Net (for example), there's probably an issue with the code breaking CQS.
When I call a function that returns a value that I ignore, it's usually because I'm doing it in a test to verify behavior. Here's an example in C#:
[Fact]
public void StatService_should_call_StatValueRepository_for_GetPercentageValues()
{
var statValueRepository = new Mock<IStatValueRepository>();
new StatService(null, statValueRepository.Object).GetValuesOf<PercentageStatValue>();
statValueRepository.Verify(x => x.GetStatValues());
}
I don't really care about the return type, I just want to verify that a method was called on a fake object.
In C it is very common, but there are places where it is ok to do so and other places where it really isn't. Later versions of GCC have a function attribute so that you can get a warning when a function is used without checking the return value:
The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as realloc.
int fn () __attribute__ ((warn_unused_result));
int foo ()
{
if (fn () < 0) return -1;
fn ();
return 0;
}
results in warning on line 5.
Last time I used this there was no way of turning off the generated warning, which causes problems when you're compiling 3rd-party code you don't want to modify. Also, there is of course no way to check if the user actually does something sensible with the returned value.

Resources