简体   繁体   中英

Program crashes when calling a function from inside of a function

So basically the program runs fine if I call array_print(array); from main . But when I call array_print(array); from edit.c the program crashes!

array.c

struct array* array_create()
{
    struct array* array = (struct array*) malloc(sizeof(struct array));
    array->data = (int*) malloc(sizeof(int) * 10000);
    array->size = 10000;
    return array;
}

void array_print(ARRAY array)
{
    int i;

    for (i = 0; i < array->size; i++)
        printf("%d ", array->data[i]);

}

array.h

struct array {
    int* data;
    int size;
};


typedef struct array* ARRAY;


ARRAY array_create();
void array_print(ARRAY array);

edit.c

ARRAY array;  // Which array is which?! I have one array in main too...

void edit()
{
    array_print(array);
}

main.c

ARRAY array;
array = array_create();
edit(); // This makes the program crash

EDIT What does this mean in edit.c? When does the array become NULL? And what is calling the code in edit.c, nothing?!:

ARRAY array = NULL;  // When is this being called and why? I don't want to reset it, I want to work with the array in main...

void edit()
{
    array_print(array);
}

It seems to me that you are wrong to assume that Array array defined in main.c is the same as array in edit.c

Without seeing the includes in edit.c it is impossible to tell. But the Array defined in main.c seems local to that compilation unit.

It is likely that you have a different array in edit.c with the same 'global' name.

Your simplest solution is to avoid functions that operate on global arrays. Make sure that the function 'edit' accepts and struct array pointer and work on the array it was given. Every function that works on an Array should accept a pointer to the structure never try to work on a magic array.

If you really MUST have a single global array that every one works on then you define it in a header file as 'extern ARRAY '. Then every edit.c file will use the same ARRAY that was initialized in main.c but then edit.c will only work on one array which is really usually quite pointless

EDIT

The code you edited:

ARRAY array = NULL;  // When is this being called and why? I don't want to reset it, I want to work with the array in main...
void edit() {
    array_print(array);
}

in edit.c basically says: I have a new ARRAY pointer called array. Initialize it with a NULL Then the call to array_print passes that NULL to be printed which obviously crashes.

OPTION 1: Not Using Globals: In its header file: void edit(ARRAY array) In the edit.c file:

void edit(ARRAY array) {
    array_print(array);
}

Finally when you call it in main.c

ARRAY my_main_array = NULL;
my_main_array = array_create();
edit(my_main_array );

Option 2. Using Globals:

In a header file that both main.c and edit.c include:

  extern ARRAY my_global_array;

In main.c

ARRAY my_global_array = array_create();
edit();

In edit.c

void edit() {
    array_print(my_global_array);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM