c - 是否可以在运行时找到结构变量和全局变量的 sizeof 和地址?

标签 c visual-studio-2008

有 4 个 .c/.h 文件。每个都有一些全局变量和结构变量。我从 .map 文件中获取了所有变量名称。我将所有内容提取到二维数组/*array[](char类型)中。现在我想传递每个变量的大小及其地址

最佳答案

好的。如果需要在变量中分配动态内存,则需要使用“stdlib.h”中的malloc函数来读取该文件,可以将其存储到链表中逐一处理。

检查示例... 我们有一个这样的语义的“variables.map”:

eua 40
brasil 30
paris 15
horse 8

其中第一列是变量的名称(最多 40 个字符参见结构原型(prototype)),第二列是变量的大小。 (注:用换行符“\n”分隔)

程序将文件加载到内存中的链表中,并分配相应的内存空间(检查main())。

查看示例程序...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//MAP prototype
typedef struct map {
    char name[40];
    unsigned int size;

    struct map *next;
} map;
//---

//Prototypes
void createList();
void insert(char[], int, map*);
void showList(map*);
unsigned int searchVariable(char[], map*);
void parseMapFile(char[]);
//---

//List, Linked list pointer
map *list_variables;
//---

//Main!
int main(int argc, const char * argv[])
{
    //Create list!
    createList();

    //Parse the .Map file into struct
    parseMapFile("/Users/gabriel/Documents/variables.map");

    //Show linked list
    //showList(list_variables);

    //------
    //Lets go now allocate the variables with the size in .map file

    //Allocate space!
    //Tipical types
    int   *eua    = malloc(searchVariable("eua", list_variables) * sizeof(int));
    char  *brasil = malloc(searchVariable("brasil", list_variables) * sizeof(char));
    float *paris  = malloc(searchVariable("paris", list_variables) * sizeof(float));

    //Alloc the void type (Undefined)
    void  *horse  = malloc(searchVariable("horse", list_variables) * sizeof(void));
    //---

    //Set values
    *eua = 5; //Integer
    strcpy(brasil, "Cool!"); //String
    *paris = 3.14; //Float
    *(int*)horse = (int) 7; //Set a integer value to void pointer!
    //---

    //Show up!
    printf("Variable eua: %d \n", *eua);
    printf("Variable brasil: %s \n", brasil);
    printf("Variable paris: %f \n", *paris);
    printf("Variable horse: %d \n", *((int*)horse));

    return EXIT_SUCCESS;
}


//Linked list functions...
//Allocate the linked list on memory
void createList() {
    list_variables = malloc( sizeof (map));
    list_variables->next = NULL;
}

//Insert the .MAP values into linked list
void insert(char name[], int size, map *p)
{
    map *new;

    new = malloc( sizeof (map));

    new->size = size;
    strcpy(new->name, name);
    new->next = p->next;
    p->next = new;
}

//Show variables loaded from .MAP file
void showList(map *list)
{
    map *p;
    for (p = list->next; p != NULL; p = p->next)
        printf("Variable: %s, Size: %d \n", p->name, p->size);
}

//Search variable in memory and return the size respective
unsigned int searchVariable(char name[], map *list)
{
    map *p;
    p = list->next;

    while (p != NULL && strcmp(name, p->name) != 0)
        p = p->next;

    return p->size;
}
//---

//Procedure to parse the map file in the specified structure!
void parseMapFile(char path[]) {
    char line[80];
    char name[40];
    unsigned int size;

    FILE *fp = fopen(path, "r");

    while(fgets(line, 80, fp) != NULL)
    {
        sscanf (line, "%s %d", name, &size);

        insert(name, size, list_variables);
    }

    fclose(fp);
}

有了这个,您确实可以使用存储在文件中的值来分配动态空间。

关于c - 是否可以在运行时找到结构变量和全局变量的 sizeof 和地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978699/

相关文章:

c++ - 'printf' 中的后/前增量

c - 构建 makefile 依赖/继承树

c++ - 无效的指针会强制应用程序崩溃

c++ - 与 VS6 相比,VS2008 C++ 编译中的巨大 OBJ 文件

visual-studio-2008 - Visual Studio,每个解决方案缩进设置

c++ - Visual Studios 2008 中 C++ 开发中的链接错误

c++ - 关于如何使用简单的 C++ 库(不使用 .net)和使用该库的 C++ 项目创建 .sln 的基本说明?

c++ - 免费(指针)方法的性能?

c - "int *[5]"在c中表示什么?

C 程序设计 - XOR 按位运算