C++ 2个结构体重名,g++有办法发现吗?

我有2个cpp文件,2个里面各自有一个结构体,成员不一样,但是名字恰好一样。
我使用的时候,就会出问题,2个结构体混淆了,可能会导致程序崩溃。
用g++编译的时候,g++并没有报错。
有没有方法,能主动发现类似的错误?
main.cpp

typedef struct key_info_s{
    std::string key;
    std::string value;
    std::string key2;
    int a[10];

     ~key_info_s(){
        log_info("main::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
    }
}KEY_INFO_T;

test_fun.cpp

typedef struct  key_info_s{
    public:
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
    ~key_info_s(){
        log_info("test_fun::key_info_s: delete");
    }

}KEY_INFO_T;

下面的源码,运行时会直接崩溃。
而且,看打印,test_fun.cpp里面的变量,析构时,会调用main.cpp里面的析构函数。

main.cpp

#include <stdio.h>
#include <string>
#include <vector>
#include <unistd.h>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)


int test_key_fun1();
int test_key_fun2();
int test_key_fun3();

int a;
typedef struct key_info_s{
    std::string key;
    std::string value;
    ~key_info_s(){
           ht_log_info("main::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};


int main()
{


   // test_key_fun2();
    test_key_fun3();
    test_key_fun1();
     KEY_INFO_T aaaa;
    while(1)
    {
        usleep(1e6);

        int a = 34;
        int b= 34;
        int c = a + b;
        ht_log_info("c=%d", c);
    }

    return 0;
}

test_fun.cpp

static int m_test_key1=13;
#include <stdio.h>
#include <string>
#include <vector>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)


typedef struct key_info_s{
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;

    ~key_info_s(){
           ht_log_info("test_fun.cpp::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    public:
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};

static std::vector<KEY_INFO_T>   m_key_info_list;
extern int a;
int test_key_fun1()
{
    KEY_INFO_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";
    key_info.a = 21;
    key_info.b = 22;
    key_info.c = 23;

    m_key_info_list.push_back(key_info);

    KEY_INFO_T &key_info2 = m_key_info_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]", sizeof(KEY_INFO_T), key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());
    ht_log_info("size=%d, [%d][%d][%d]", sizeof(KEY_INFO_T), key_info2.a, key_info2.b, key_info2.c);

    return 0;
}


int test_key_fun2()
{
    ht_log_info("test_key_fun2:m_test_key1=%d", m_test_key1);
    ht_log_info("a=%d", a);
    ht_log_info("a=%lf", a);
    return 1;
}

static std::vector<KEY_ITEM_T>   m_key_item_list;
int test_key_fun3()
{
    KEY_ITEM_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";

    m_key_item_list.push_back(key_info);

    KEY_ITEM_T &key_info2 = m_key_item_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]",sizeof(KEY_ITEM_T),  key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());

    return 0;
}

阅读 1.4k
1 个回答
新手上路,请多包涵

2个源文件的具体内容,写在这里。

main.cpp

#include <stdio.h>
#include <string>
#include <vector>
#include <unistd.h>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)

int test_key_fun1();
int test_key_fun2();
int test_key_fun3();

int a;
typedef struct key_info_s{
    std::string key;
    std::string value;
    ~key_info_s(){
           ht_log_info("main::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};


int main()
{


   // test_key_fun2();
    test_key_fun3();
    test_key_fun1();
     KEY_INFO_T aaaa;
    while(1)
    {
        usleep(1e6);

        int a = 34;
        int b= 34;
        int c = a + b;
        ht_log_info("c=%d", c);
    }

    return 0;
}

test_fun.cpp


static int m_test_key1=13;
#include <stdio.h>
#include <string>
#include <vector>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)


typedef struct key_info_s{
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;

    ~key_info_s(){
           ht_log_info("test_fun.cpp::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    public:
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};

static std::vector<KEY_INFO_T>   m_key_info_list;
extern int a;
int test_key_fun1()
{
    KEY_INFO_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";
    key_info.a = 21;
    key_info.b = 22;
    key_info.c = 23;

    m_key_info_list.push_back(key_info);

    KEY_INFO_T &key_info2 = m_key_info_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]", sizeof(KEY_INFO_T), key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());
    ht_log_info("size=%d, [%d][%d][%d]", sizeof(KEY_INFO_T), key_info2.a, key_info2.b, key_info2.c);

    return 0;
}


int test_key_fun2()
{
    ht_log_info("test_key_fun2:m_test_key1=%d", m_test_key1);
    ht_log_info("a=%d", a);
    ht_log_info("a=%lf", a);
    return 1;
}

static std::vector<KEY_ITEM_T>   m_key_item_list;
int test_key_fun3()
{
    KEY_ITEM_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";

    m_key_item_list.push_back(key_info);

    KEY_ITEM_T &key_info2 = m_key_item_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]",sizeof(KEY_ITEM_T),  key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());

    return 0;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题