1

I'm trying to declare a variable that will be available to multiple .c files. It's a malloced array of pointers to structs.

From what I understand I'll have to declare it with extern in a header file, but since I can't malloc in the header file do I declare it in the header file and then malloc in the .c file? I can't see how this would be done.

1
  • 2
    Only the pointer (plus maybe the count or size) needs to be public. Where it points to can be obtained by dereferencing the pointer, and you can obtain memory by calling malloc() and assigning its result to the pointer. Commented May 1, 2013 at 11:03

7 Answers 7

4

You put the declaration in the header, the definition inside a .c file and the call to malloc in a function that gets executed before the first time you dereference the pointer (like the main function for example). So it will look something like this:

foo.h:

extern struct your_struct** pointer;
void foo_init();
void foo_cleanup();

foo.c:

#include <stdlib.h>
#include "foo.h"

struct your_struct** pointer;

void foo_init() {
    pointer = malloc(sizeof(your_struct*) * some_size);
    // initialize the pointer in the array
}

void foo_cleanup() {
    // free the pointers in the array if you used malloc to initialize them
    free(pointer);
}

main.c:

#include "foo.h"

int main() {
    foo_init();
    // do other stuff
    foo_cleanup();
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why is the declaration using ** in the header and * in the C file?
@MatsPetersson That was a typo. I've fixed it to be ** in both cases now (because the OP said he wanted an array of pointers).
1

Since you want to use a dynamic variable, you have to use a pointer. And beside declaration, you need to define it somewhere. In your case, you in addition should allocate the memory as early as possible in your code, i.e. in the first lines of main.

Comments

0

Yes, you declare it in the header and malloc it in the .c file. Malloced pointers don't have a scope as such and are only de-allocated when they are freed. So you can use the pointer throughout your entire program.

Comments

0
  1. Declare as extern in header file
  2. Define it as global in one of your .c files
  3. Allocate the pointed array in the same .c file

You're good to go.

Note though, if you access it in different files, you need to ensure that at the point of each access the data has been already allocated (and not freed yet).

Comments

0

In the header file declare the pointer. Include the header in all .c modules that refer to the pointer.

extern struct yourstruct *ps;

Then define the variable in the most pertinent .c module

struct yourstruct *ps;

ps = malloc(....

Comments

0

You will need to malloc it in c file and declare it in header file. You will also need to take care about its access and deallocation. It has to be de-allocated with free and only once.Probably at the end of you program execution. Also you need to put guard condition (check for null) before accessing it in multiple files.

Comments

0
FILE **file_array=NULL;

function(){
    file_array = malloc(size*sizeof(FILE*));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.