You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ zig run localaddress.zig
Address: i32@7fff157f79a4
Value: 5
The equivalent C code:
#include <stdio.h>
int* test_func() {
int test_var = 5;
return &test_var;
}
int main(void) {
int *test_ptr = test_func();
printf("Address: %ls\n", test_ptr);
printf("Value: %d\n", *test_ptr);
return 0;
}
Produces a compile-time warning:
$ gcc localaddr.c -o localaddr
localaddr.c: In function ‘test_func’:
localaddr.c:5:9: warning: function returns address of local variable [-Wreturn-local-addr]
5 | return &test_var;
| ^~~~~~~~~
And produces:
$ ./localaddr
Address: (null)
Segmentation fault
The text was updated successfully, but these errors were encountered:
Contents of a function are still accessible even after their lifetime. For example:
Results in:
The equivalent C code:
Produces a compile-time warning:
And produces:
The text was updated successfully, but these errors were encountered: