M
Maciej Labanowicz
Hello,
Is following code correct (ANSI C89) ?
----
01: #include <stdio.h>
02: #include <stdlib.h>
03: struct { char array1 [100]; } * fun1(int x1);
04: struct { char array2 [200]; } fun2(int x2);
05: int main(void) {
06: printf("sizeof(array1) = %lu\n", (unsigned long)(sizeof(fun1(23)-
(sizeof(fun2(45).array2)));
08: return EXIT_SUCCESS;
09: }
----
If NO then rest of this post may be discarded.
If YES then it seems that I have found an issue
with compiler (tcc version 0.9.26).
Following example is compiled without any error/warning,
but final application generates "Segmentation Fault" in line 31.
I would like to have confirmation about correctness of this code.
--[beg]-----------------------------------------------------
C:\tcc> gawk '{printf("%02u: %s\n", NR, $0);}' main.c
01: #include <stdio.h>
02: #include <stdlib.h>
03: #include <string.h>
04: typedef struct {
05: void * ptr;
06: } bar_t;
07: typedef struct {
08: bar_t bar;
09: int * iptr;
10: } foo_t;
11: bar_t * fun(foo_t * foo_ptr) {
12: bar_t * result = NULL;
13: printf("++ fun(foo_ptr = %p)\n", (void *)foo_ptr);
14: result = &(foo_ptr->bar);
15: printf("-- fun(...) = %p\n", (void *)result);
16: return result;
17: }
18: #define TEST_MACRO(foo_ptr) \
19: ((sizeof(*((1 ? foo_ptr : foo_ptr)->iptr)) == 123) \
20: ? 12 : 34)
21: int main(void) {
22: foo_t foo;
23: memset(&foo, 0, sizeof(foo));
24: foo.bar.ptr = &foo;
25: printf("Hello World\n");
26: printf("foo.bar.ptr = %p\n", foo.bar.ptr);
27: printf("foo.iptr = %p\n", (void *)foo.iptr);
28: printf("fun(&foo) = %p\n", (void *)fun(&foo));
29: printf("fun(&foo)->ptr = %p\n", fun(&foo)->ptr);
30: printf("+++++++++++\n");
31: printf("%d\n", TEST_MACRO(((foo_t *)((fun(&foo))->ptr))));
32: printf("-----------\n");
33: return EXIT_SUCCESS;
34: }
C:\tcc>tcc -c -o main.o main.c
C:\tcc>tcc -o main.exe main.o
C:\tcc>main.exe
Hello World
foo.bar.ptr = 0012FF78
foo.iptr = 00000000
++ fun(foo_ptr = 0012FF78)
-- fun(...) = 0012FF78
fun(&foo) = 0012FF78
++ fun(foo_ptr = 0012FF78)
-- fun(...) = 0012FF78
fun(&foo)->ptr = 0012FF78
+++++++++++
<:seg-fault:>
--[eof]-----------------------------------------------------
NOTE: Simplifying this code for example in line 19:
- ((sizeof(*((1 ? foo_ptr : foo_ptr)->iptr)) == 123) \
+ ((sizeof(*(( foo_ptr )->iptr)) == 123) \
makes that issue disappears.
Regards
Is following code correct (ANSI C89) ?
----
01: #include <stdio.h>
02: #include <stdlib.h>
03: struct { char array1 [100]; } * fun1(int x1);
04: struct { char array2 [200]; } fun2(int x2);
05: int main(void) {
06: printf("sizeof(array1) = %lu\n", (unsigned long)(sizeof(fun1(23)-
07: printf("sizeof(array2) = %lu\n", (unsigned long)array1)));
(sizeof(fun2(45).array2)));
08: return EXIT_SUCCESS;
09: }
----
If NO then rest of this post may be discarded.
If YES then it seems that I have found an issue
with compiler (tcc version 0.9.26).
Following example is compiled without any error/warning,
but final application generates "Segmentation Fault" in line 31.
I would like to have confirmation about correctness of this code.
--[beg]-----------------------------------------------------
C:\tcc> gawk '{printf("%02u: %s\n", NR, $0);}' main.c
01: #include <stdio.h>
02: #include <stdlib.h>
03: #include <string.h>
04: typedef struct {
05: void * ptr;
06: } bar_t;
07: typedef struct {
08: bar_t bar;
09: int * iptr;
10: } foo_t;
11: bar_t * fun(foo_t * foo_ptr) {
12: bar_t * result = NULL;
13: printf("++ fun(foo_ptr = %p)\n", (void *)foo_ptr);
14: result = &(foo_ptr->bar);
15: printf("-- fun(...) = %p\n", (void *)result);
16: return result;
17: }
18: #define TEST_MACRO(foo_ptr) \
19: ((sizeof(*((1 ? foo_ptr : foo_ptr)->iptr)) == 123) \
20: ? 12 : 34)
21: int main(void) {
22: foo_t foo;
23: memset(&foo, 0, sizeof(foo));
24: foo.bar.ptr = &foo;
25: printf("Hello World\n");
26: printf("foo.bar.ptr = %p\n", foo.bar.ptr);
27: printf("foo.iptr = %p\n", (void *)foo.iptr);
28: printf("fun(&foo) = %p\n", (void *)fun(&foo));
29: printf("fun(&foo)->ptr = %p\n", fun(&foo)->ptr);
30: printf("+++++++++++\n");
31: printf("%d\n", TEST_MACRO(((foo_t *)((fun(&foo))->ptr))));
32: printf("-----------\n");
33: return EXIT_SUCCESS;
34: }
C:\tcc>tcc -c -o main.o main.c
C:\tcc>tcc -o main.exe main.o
C:\tcc>main.exe
Hello World
foo.bar.ptr = 0012FF78
foo.iptr = 00000000
++ fun(foo_ptr = 0012FF78)
-- fun(...) = 0012FF78
fun(&foo) = 0012FF78
++ fun(foo_ptr = 0012FF78)
-- fun(...) = 0012FF78
fun(&foo)->ptr = 0012FF78
+++++++++++
<:seg-fault:>
--[eof]-----------------------------------------------------
NOTE: Simplifying this code for example in line 19:
- ((sizeof(*((1 ? foo_ptr : foo_ptr)->iptr)) == 123) \
+ ((sizeof(*(( foo_ptr )->iptr)) == 123) \
makes that issue disappears.
Regards