0

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);
}
user3285214
  • 21
  • 1
  • 5

1 Answers1

0

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);
}
odedsh
  • 2,594
  • 17
  • 17
  • Hmm, I see your edit. The edit() functions parameters has to be nothing. – user3285214 Feb 09 '14 at 20:32
  • Can I contact you privately in some way? I want to show you some code but don't want to share it to the public. I can't explain the problem, I have to show it. – user3285214 Feb 09 '14 at 20:39
  • I can try to create a chat room.. one minute – odedsh Feb 09 '14 at 20:43
  • Try this linke http://chat.stackoverflow.com/rooms/47130/program-crashes-when-calling-a-function-from-inside-of-a-function – odedsh Feb 09 '14 at 20:46
  • I have to have 20 rep in order to talk in that chat room, can you come here instead? http://tinychat.com/drrp1j – user3285214 Feb 09 '14 at 20:48
  • It still says I have to have 20 rep in order to chat :/. Can you join the tinychat room? You don't have to register or anything, just click that guest button. – user3285214 Feb 09 '14 at 20:52