Posts

Showing posts with the label segmentation-fault

Copying char arr[64] to char arr[] can cause a segmentation fault?

Image
Clash Royale CLAN TAG #URR8PPP Copying char arr[64] to char arr can cause a segmentation fault? typedef struct { int num; char arr[64]; } A; typedef struct { int num; char arr; } B; I declared A* a; and then put some data into it. Now I want to cast it to a B* . A* a; B* A* a; a->num = 1; strcpy(a->arr, "Hi"); B* b = (B*)a; Is this right? I get a segmentation fault sometimes (not always), and I wonder if this could be the cause of the problem. I got a segmentation fault even though I didn't try to access to char arr after casting. char arr 1 Answer 1 This defines a pointer variable A* a; There is nothing it is cleanly pointing to, the pointer is non-initialised. This accesses whatever it is pointing to a->num = 1; strcpy(a->arr, "Hi"); Without allocating anything to the pointer beforehand (by e.g. using malloc() ) this is asking for segfaults as ...

Why a segmentation fault occurs calling a function inside setjmp()?

Image
Clash Royale CLAN TAG #URR8PPP Why a segmentation fault occurs calling a function inside setjmp()? I do not understand why in the function middleFunc() , a segmentation fault is raisen when entry_point(arg) is invoked inside the if ( setjmp(middle) ) statement. middleFunc() entry_point(arg) if ( setjmp(middle) ) #include <stdio.h> #include <setjmp.h> jmp_buf start,middle,end; void finalFunc(void *v) { printf("hellon"); return ; } void middleFunc(void (*entry_point)(void *), void *arg) { //just debug : this does not cause segmentation fault entry_point(arg); if ( setjmp(middle) ){ //this casues the segmentation fault entry_point(arg); //once the entry point (finalFunc) is executed go to jmp_buffer end longjmp(end,1); } else { longjmp(start,1); } } int main(){ if (setjmp(end)){ //exit since finalFunc has been executed return ...