简体   繁体   English

具有文件作用域(C)的已分配变量

[英]Malloced variables with file scope (C)

I'm trying to declare a variable that will be available to multiple .c files. 我正在尝试声明一个可用于多个.c文件的变量。 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? 据我了解,我必须在头文件中用extern声明它,但是由于我不能在头文件中使用malloc,所以我应该在头文件中声明它,然后在.c文件中声明它吗? I can't see how this would be done. 我看不到如何做到这一点。

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). 您将声明放在标头中,将定义放在.c文件中,并将对malloc的调用放在第一次取消引用指针之前便已执行的函数中(例如,例如main函数)。 So it will look something like this: 因此它将看起来像这样:

foo.h: foo.h:

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

foo.c: 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: main.c:

#include "foo.h"

int main() {
    foo_init();
    // do other stuff
    foo_cleanup();
    return 0;
}

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, ie in the first lines of main. 在您的情况下,您还应该在代码中,即在main的第一行中,尽早分配内存。

Yes, you declare it in the header and malloc it in the .c file. 是的,您在标头中声明它,并在.c文件中对其进行malloc Malloced pointers don't have a scope as such and are only de-allocated when they are free d. 分配的指针没有作用域,只有在free d时才被取消分配。 So you can use the pointer throughout your entire program. 因此,您可以在整个程序中使用指针。

  1. Declare as extern in header file 在头文件中声明为extern
  2. Define it as global in one of your .c files 在您的一个.c文件中将其定义为全局
  3. Allocate the pointed array in the same .c file 在同一.c文件中分配指向数组

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). 但是请注意,如果以不同的文件访问它,则需要确保在每次访问时已经分配了数据(尚未释放)。

In the header file declare the pointer. 在头文件中声明指针。 Include the header in all .c modules that refer to the pointer. 在所有引用该指针的.c模块中包含头。

extern struct yourstruct *ps;

Then define the variable in the most pertinent .c module 然后在最相关的.c模块中定义变量

struct yourstruct *ps;

ps = malloc(....
FILE **file_array=NULL;

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

You will need to malloc it in c file and declare it in header file. 您将需要在c文件中对其进行malloc分配,并在头文件中对其进行声明。 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. 另外,您还需要在多个文件中访问之前设置防护条件(检查是否为空)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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