C 如何为头文件中定义(声明)的数组分配内存?

C 如何为头文件中定义(声明)的数组分配内存?,c,C,比如说,有一个proc.h定义了一个类似 程序h: struct proc { char p_stat; char p_flags; int p_pid; } proc[100]; struct proc { char p_stat; char p_flags; int p_pid; } proc[100]; 还有另外一个5.c文件,包括这个头文件。我没有看到任何这样的.c文件为这个数组分配内存。我认为是链接器为它分配内存。然而,出现在头文件中的

比如说,有一个proc.h定义了一个类似

程序h:

struct proc
{
 char p_stat;
 char p_flags;
 int p_pid;
} proc[100];
struct proc 
{  
    char p_stat;  
    char p_flags;  
    int p_pid; 
} proc[100];
还有另外一个5.c文件,包括这个头文件。我没有看到任何这样的.c文件为这个数组分配内存。我认为是链接器为它分配内存。然而,出现在头文件中的应该只是一个声明。声明如何导致内存分配?我认为只有定义才会导致内存分配


谢谢,

include指令只不过是将包含的文件插入包含它的文件中。换句话说,所有
.c
文件分别声明结构,并为变量
proc
声明(分配内存)。它们都分别为它定义了内存。如果以后将它们全部编译在一起,我不知道这可能如何正常工作。

include指令只不过是将包含的文件插入包含它的文件中。换句话说,所有
.c
文件分别声明结构,并为变量
proc
声明(分配内存)。它们都分别为它定义了内存。如果以后将它们全部编译在一起,我不知道这可能如何正常工作

我没有看到任何这样的.c文件为这个数组分配内存

实际上,它们都是这样做的:声明和定义被逐字复制到包含标题的每个翻译单元中,因此每个翻译单元都有自己的
proc[100]
数组定义

如果您尝试将翻译结果链接在一起,您应该会看到链接器产生的重新定义错误,除非实际标题声明
proc
static

当然,创建
proc[100]
的正确方法是在标题中声明它
extern
,并在其中一个
.c
文件中提供定义

我没有看到任何这样的.c文件为这个数组分配内存

实际上,它们都是这样做的:声明和定义被逐字复制到包含标题的每个翻译单元中,因此每个翻译单元都有自己的
proc[100]
数组定义

如果您尝试将翻译结果链接在一起,您应该会看到链接器产生的重新定义错误,除非实际标题声明
proc
static

当然,创建
proc[100]
的正确方法是在头文件中声明它
extern
,并在其中一个
.c
文件中提供定义。

头文件包括(成为)一个c文件。您已经在头文件中声明并定义了数组,然后将该声明包含在五个C文件中,因此空间被分配了5次。您要做的是在头文件中使
proc
数组成为
extern
声明,然后在一个C文件中复制完全相同的声明(没有
extern

或者,您可以使用宏完成大致相同的任务,如下所示:

// foo.h
#ifndef MAIN
  /*
   * this declaration is extern for everybody except the 
   * MAIN module, (typically main.c) which defines
   * the macro MAIN -- this ensures that all C files see the same 
   * structure definition, but space is allocated exactly once.
   */
   extern struct proc {
#else
          struct proc {
#endif
     … 
    } proc[100];
 typedef struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
在其中一个C文件(例如main)中,可以执行以下操作:

// main.c
#define MAIN
// when you include foo.h, the 'extern' will be left out, and space will be allocated
#include "foo.h"
在其他C文件中,只需执行以下操作:

// other.c
#include "foo.h"
这将导致数组的空间分配一次(在main.o中),其他.o文件将包含对proc[]的外部引用,该引用将由链接器解析。

头文件包含(成为)C文件的一部分。您已经在头文件中声明并定义了数组,然后将该声明包含在五个C文件中,因此空间被分配了5次。您要做的是在头文件中使
proc
数组成为
extern
声明,然后在一个C文件中复制完全相同的声明(没有
extern

或者,您可以使用宏完成大致相同的任务,如下所示:

// foo.h
#ifndef MAIN
  /*
   * this declaration is extern for everybody except the 
   * MAIN module, (typically main.c) which defines
   * the macro MAIN -- this ensures that all C files see the same 
   * structure definition, but space is allocated exactly once.
   */
   extern struct proc {
#else
          struct proc {
#endif
     … 
    } proc[100];
 typedef struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
在其中一个C文件(例如main)中,可以执行以下操作:

// main.c
#define MAIN
// when you include foo.h, the 'extern' will be left out, and space will be allocated
#include "foo.h"
在其他C文件中,只需执行以下操作:

// other.c
#include "foo.h"
这将导致数组的空间分配一次(在main.o中),其他.o文件将包含对proc[]的外部引用,该引用将由链接器解析

比如说,有一个proc.h定义了一个类似

程序h:

struct proc
{
 char p_stat;
 char p_flags;
 int p_pid;
} proc[100];
struct proc 
{  
    char p_stat;  
    char p_flags;  
    int p_pid; 
} proc[100];
上面的代码实际上不仅定义了数据结构
struct proc
,还为每个文件(包括头文件)声明/分配了一个数组

一个广泛使用的习惯用法是用typedef定义数据结构,如下所示:

// foo.h
#ifndef MAIN
  /*
   * this declaration is extern for everybody except the 
   * MAIN module, (typically main.c) which defines
   * the macro MAIN -- this ensures that all C files see the same 
   * structure definition, but space is allocated exactly once.
   */
   extern struct proc {
#else
          struct proc {
#endif
     … 
    } proc[100];
 typedef struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
或者只是简单地定义没有typedef的结构:

 struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
然后在匹配头的c文件中,您将实例化结构数组:

proc_t proc[100];        // if you've typedef'd the struct
struct proc_t proc[100]; // if you haven't typedef'd
然后在所有的c文件中,包括头文件,想要访问这个全局结构,您将声明变量extern。这样,所有c文件将共享对单个数据结构的访问

我更喜欢在单个头中定义一个结构,并通过函数调用(通过地址传递的对象)访问/操作该数据结构的实例化来封装它,但这是个人喜好的问题

比如说,有一个proc.h定义了一个类似

程序h:

struct proc
{
 char p_stat;
 char p_flags;
 int p_pid;
} proc[100];
struct proc 
{  
    char p_stat;  
    char p_flags;  
    int p_pid; 
} proc[100];
上面的代码实际上不仅定义了数据结构
struct proc
,还为每个文件(包括头文件)声明/分配了一个数组

一个广泛使用的习惯用法是用typedef定义数据结构,如下所示:

// foo.h
#ifndef MAIN
  /*
   * this declaration is extern for everybody except the 
   * MAIN module, (typically main.c) which defines
   * the macro MAIN -- this ensures that all C files see the same 
   * structure definition, but space is allocated exactly once.
   */
   extern struct proc {
#else
          struct proc {
#endif
     … 
    } proc[100];
 typedef struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
或者只是简单地定义没有typedef的结构:

 struct 
 {  
     char p_stat;  
     char p_flags;  
     int p_pid; 
 } proc_t;
然后在匹配头的c文件中,您将实例化结构数组:

proc_t proc[100];        // if you've typedef'd the struct
struct proc_t proc[100]; // if you haven't typedef'd
然后在所有的c文件中,包括头文件,想要访问这个全局结构,您将声明变量extern。这样,所有c文件将共享对单个数据结构的访问

我更喜欢在一个标题中定义一个结构,并用o封装它