Segmentation fault when trying to clone a function (in C)

Andrew Chan

i'm trying to make conway's game of life and when I have it single threaded, it works perfectly but if I try to use clone() to make it, it causes a segmentation error. If possible, can anyone help me figure out why?

Main

int main(int argc, char *argv[])
{
    const int STACK_SIZE=65536;
    int checkInit=0;
    int i;
    int j;
    int *stack;
    int *stackTop;
    FILE *file1;
    int numThreads;

    if(argc != 3) {
        fprintf(stderr, "Usage: %s <Executable> <Input file> <Threads>\n", argv[0]);
        exit(1);
    }

    file1=fopen(argv[1],"r");
    if(file1==NULL) { //check to see if file exists
        fprintf(stderr, "Cannot open %s\n", argv[1]);
        exit(1);
    }

    fscanf(file1,"%d",&N);

    stack=malloc(STACK_SIZE);
    if(stack==NULL) {
        perror("malloc");
        exit(1);
    }
    stackTop=stack+STACK_SIZE;

    numThreads=atoi(argv[2]);
    calcPerThread=N/numThreads;
    eCalcPerThread=calcPerThread;

    B = malloc(N * sizeof(int *));
    A = malloc(N * sizeof(int *));
    for (i = 0; i < N; i++) {
        A[i] = malloc(N * sizeof(int));
        B[i] = malloc(N * sizeof(int));
    }

    system("clear");
    for(i=0; i<N+2; i++)
        printf("-");
    printf("\n");
    for (i=0; i<N; i++) {
        printf("|");
        for(j=0; j<N; j++) {
            if(A[i][j] == 1)
                printf("x");
            else
                printf(" ");
        }
        printf("|\n");
    }
    for(i=0; i<N+2; i++)
        printf("-");
    printf("\n");

    printf("Press [ENTER] for the next generation.\n");

    fclose(file1);

    while(getchar()) {
        system("clear");
        clone(growDie,stackTop,CLONE_VM|CLONE_FILES,NULL);
        display(N);
    }
}

GDB Error

Program received signal SIGSEGV, Segmentation fault.
clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:72
72      ../sysdeps/unix/sysv/linux/i386/clone.S: No such file or directory.
        in ../sysdeps/unix/sysv/linux/i386/clone.S
Current language:  auto
The current source language is "auto; currently asm".

I THINK the problem is from stackTop, but i'm not exactly sure.

kaylum

You have a problem in your pointer arithmetic.

stack=malloc(STACK_SIZE);

That allocates STACK_SIZE bytes. But..

int *stack;
stackTop=stack+STACK_SIZE;

stack is a pointer to an int. Hence pointer arithmetic will cause each +1 to be 4 bytes not 1 byte (assuming a 32 bit system).

You can fix this in a number of ways:

  • make stack and stackTop "char *" instead of "int *"
  • cast stack in the arithmetic: stackTop = ((unsigned int)stack)+STACK_SIZE.

I prefer the first option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Segmentation fault when trying to clone a function (in C)

From Dev

segmentation fault when trying to deference pointer : C

From Dev

segmentation fault when trying to deference pointer : C

From Dev

Segmentation fault when calling c-function

From Dev

segmentation fault when calling c function on ruby

From Dev

Passing pointer to function causes segmentation fault when trying to retrieve value

From Dev

Segmentation fault in C when trying to import header file and use it

From Dev

Segmentation fault in C when trying to reference first element in char *

From Dev

Segmentation fault in C when trying to import header file and use it

From Dev

Segmentation Fault When Calling Function

From Dev

Segmentation fault when function returns

From Dev

Segmentation fault when calling C function with Python C API twice

From Dev

C : Segmentation Fault when move main function to new file

From Dev

Segmentation fault when returning string from function in c++

From Dev

Segmentation Fault when accessing string array modified by a function in C

From Dev

Segmentation fault when returning string from function in c++

From Dev

C++ segmentation fault when calling function from a new location

From Dev

Segmentation fault in C code function

From Java

Segmentation Fault when trying to access pointer to Node

From Dev

Segmentation fault when trying to free memory

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

"Segmentation fault" when trying to print an integer

From Dev

Segmentation fault when trying to access pointer in struct

From Dev

Segmentation fault when trying to allocate memory for a string via a pointer-to-pointer function call

From Dev

getting error Segmentation fault (core dumped) when trying to pass a string to a function

From Dev

C - trying to assign return value from a function to a variable causes segmentation fault

From Dev

C - trying to assign return value from a function to a variable causes segmentation fault

From Dev

Segmentation fault when assigning values to pointers in function

From Dev

Segmentation fault when calling bound member function

Related Related

  1. 1

    Segmentation fault when trying to clone a function (in C)

  2. 2

    segmentation fault when trying to deference pointer : C

  3. 3

    segmentation fault when trying to deference pointer : C

  4. 4

    Segmentation fault when calling c-function

  5. 5

    segmentation fault when calling c function on ruby

  6. 6

    Passing pointer to function causes segmentation fault when trying to retrieve value

  7. 7

    Segmentation fault in C when trying to import header file and use it

  8. 8

    Segmentation fault in C when trying to reference first element in char *

  9. 9

    Segmentation fault in C when trying to import header file and use it

  10. 10

    Segmentation Fault When Calling Function

  11. 11

    Segmentation fault when function returns

  12. 12

    Segmentation fault when calling C function with Python C API twice

  13. 13

    C : Segmentation Fault when move main function to new file

  14. 14

    Segmentation fault when returning string from function in c++

  15. 15

    Segmentation Fault when accessing string array modified by a function in C

  16. 16

    Segmentation fault when returning string from function in c++

  17. 17

    C++ segmentation fault when calling function from a new location

  18. 18

    Segmentation fault in C code function

  19. 19

    Segmentation Fault when trying to access pointer to Node

  20. 20

    Segmentation fault when trying to free memory

  21. 21

    Segmentation fault when trying to access pointer in struct

  22. 22

    "Segmentation fault" when trying to print an integer

  23. 23

    Segmentation fault when trying to access pointer in struct

  24. 24

    Segmentation fault when trying to allocate memory for a string via a pointer-to-pointer function call

  25. 25

    getting error Segmentation fault (core dumped) when trying to pass a string to a function

  26. 26

    C - trying to assign return value from a function to a variable causes segmentation fault

  27. 27

    C - trying to assign return value from a function to a variable causes segmentation fault

  28. 28

    Segmentation fault when assigning values to pointers in function

  29. 29

    Segmentation fault when calling bound member function

HotTag

Archive