Skip to main content
replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

As pointed out by Edgar BonetEdgar Bonet in the comments, local variables like status in the code above are not implicitly initialized by the C++ compiler. So, the outcome of the code above is indeterminate. To avoid that, make sure you always assign values to your local variables.

As pointed out by Edgar Bonet in the comments, local variables like status in the code above are not implicitly initialized by the C++ compiler. So, the outcome of the code above is indeterminate. To avoid that, make sure you always assign values to your local variables.

As pointed out by Edgar Bonet in the comments, local variables like status in the code above are not implicitly initialized by the C++ compiler. So, the outcome of the code above is indeterminate. To avoid that, make sure you always assign values to your local variables.

Corrected statements about uninitialized local variables as per Edgar Bonet's comments.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55

Uninitialized local variables

void setup() { int status;  pinMode(13, OUTPUT);  digitalWrite(13, status); }  

Although the variable status is not explicitly initializedAs pointed out by Edgar Bonet in the comments, local variables like status in the code above, are not implicitly initialized by the C++ compiler does that for you. It sets it as zero. So, even though it's not explicit in the code, we know thatoutcome of the led won't be litcode above is indeterminate. However, it's not considered good practice to rely onTo avoid that behaviour. Try and, make sure you always assign values to your local variables as.

Things are a bit different with global and static variables:

Global and static variables are guaranteed to be initialized to 0 by the C standard.

Source: AVR Libc Reference Manual - Frequently Asked Questions - Shouldn't I initialize all my variables?

That means you shouldn't worry about initializing them to 0 in your code. In fact, you should really avoid it makes, as the code clearerinitialization may waste memory. Only initialize them to values other than 0.

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the Flash memory, thus your program is safe.

The first call to funcPtr() will actually be a call to doSomething(). Calls like the second one may lead to undefined behavior. In this case, it will continue execution from memory address 0, which seems to be the start of the sketch (I just tested and that's equivalent to a soft boot - it doesn't even go through the bootloader).

Uninitialized variables

int status; pinMode(13, OUTPUT); digitalWrite(13, status); 

Although the variable status is not explicitly initialized by the code above, the C++ compiler does that for you. It sets it as zero. So, even though it's not explicit in the code, we know that the led won't be lit. However, it's not considered good practice to rely on that behaviour. Try and assign values to your variables as it makes the code clearer.

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the Flash memory, thus your program is safe.

The first call to funcPtr() will actually be a call to doSomething(). Calls like the second one may lead to undefined behavior. In this case, it will continue execution from memory address 0, which seems to be the start of the sketch (I just tested and that's equivalent to a soft boot - it doesn't even go through the bootloader).

Uninitialized local variables

void setup() { int status;  pinMode(13, OUTPUT);  digitalWrite(13, status); }  

As pointed out by Edgar Bonet in the comments, local variables like status in the code above are not implicitly initialized by the C++ compiler. So, the outcome of the code above is indeterminate. To avoid that, make sure you always assign values to your local variables.

Things are a bit different with global and static variables:

Global and static variables are guaranteed to be initialized to 0 by the C standard.

Source: AVR Libc Reference Manual - Frequently Asked Questions - Shouldn't I initialize all my variables?

That means you shouldn't worry about initializing them to 0 in your code. In fact, you should really avoid it, as the initialization may waste memory. Only initialize them to values other than 0.

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array.

The first call to funcPtr() will actually be a call to doSomething(). Calls like the second one may lead to undefined behavior.

Fixed conceptual problem as pointed out by Connor Wolf.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the EEPROMFlash memory, thus your program is safe.

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the EEPROM, thus your program is safe.

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the Flash memory, thus your program is safe.

Removed reference to uninitialized variables as per Fake Name comments.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55
Loading
Tested what happens if you call a null function pointer and reported it here.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55
Loading
Typos; factual correction
Source Link
asheeshr
  • 3.8k
  • 3
  • 26
  • 61
Loading
Added answers more clearly.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55
Loading
Added answers more clearly.
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55
Loading
Source Link
Ricardo
  • 3.4k
  • 2
  • 26
  • 55
Loading