C代码中的错误

C代码中的错误,c,C,我已经为我的项目编写了以下代码。代码分散在不同的文件中,相当长,因此我发布了最短的代码 #include<stdio.h> #include<stdbool.h> struct TwoPoint { int width; int value; }; struct Module { int categ; void *ptr; }; struct Rect { struct TwoPoint val; struct Two

我已经为我的项目编写了以下代码。代码分散在不同的文件中,相当长,因此我发布了最短的代码

#include<stdio.h>
#include<stdbool.h>
struct TwoPoint
{
    int width;
    int value;
};

struct Module 
{
    int categ;
    void *ptr;
};

struct Rect
{
    struct TwoPoint val;
    struct TwoPoint val_new;
    bool is_changed;
};

struct S
{
    int numInstances;
    struct Module instances[20];
    struct Rect RectList[40];
    int numRect;
}s1;

struct Test
{
    int categ;
    struct Rect state;
};

struct TwoPoint initPVal(int v,int w)
{
struct TwoPoint temp;
temp.value=v;
temp.width=w;
return temp;
}
int getValue(struct TwoPoint *b)
{
    return (b->value);
}

struct TwoPoint get(struct Rect *r)
{
    return (r->val);
}

void initialize()
{
     s1.numInstances=0;
     s1.numRect=0;
}

void addRect(struct Rect *r)
{
     if(s1.numRect<40)
     {
     s1.RectList[s1.numRect].val=r->val;
     s1.RectList[s1.numRect].val_new=r->val_new;
     s1.RectList[s1.numRect].is_changed=r->is_changed;
     s1.numRect++;
     }
}

struct Rect initRect(struct TwoPoint initval) 
{
struct Rect temp;
struct TwoPoint tempP;
tempP=initPVal(0,0);
temp.val=initval;
temp.val_new=tempP;
temp.is_changed=false;
addRect(&temp);
return temp;
}

void copy(struct Rect *r)
{
    if(r->is_changed)
    {
    r->val= r->val_new;
    r->is_changed=false;
    }
}
void copyRect()
{
   int i=0;
   for(i=0;i<s1.numRect;i++)
   {    
    copy(&s1.RectList[i]);
   }
}

void setInstance(struct Module *m)
{
    s1.instances[s1.numInstances].categ=m->categ;
    s1.instances[s1.numInstances].ptr=m->ptr;
    s1.numInstances++;
    if (s1.numInstances >= 20)
    {
    printf("Too many instances");
    }
}

void setModule(struct Test *t)
{
    struct Module m;
    m.categ=t->categ;
    m.ptr=&t;
    setInstance(&m);
}



void init(struct Test *t)
{
    t->categ=2;
    struct Rect tr;
    struct TwoPoint tb1=initPVal(0,5);
    tr=initRect(tb1);
    t->state=tr;
}

void actions(struct Test *t)
{
    struct TwoPoint tb=get(&t->state);
    int y=getValue(&tb);
    printf("%d\n",y);
    unsigned int x=getValue(&tb);
    printf("%u\n",x);
    switch(y)
    {
      ....
    }
}

void initS() 
{   
     init(s1.instances[0].ptr);
}   

void act() 
{       
     actions(s1.instances[0].ptr);
}

void setup()
{
    struct Test t;
    initialize();
    init(&t);
    setModule(&t);
}

void run()
{ 
    initS();
    act();
    copyRect();
}


int main()
{
    printf("foo\n");
    setup();
    printf("bar\n");
    run();
    return 0;
}
#包括
#包括
结构两点
{
整数宽度;
int值;
};
结构模块
{
int categ;
无效*ptr;
};
结构矩形
{
结构两点val;
结构两点法新;
布尔变了;
};
结构
{
国际努米斯坦斯;
结构模块实例[20];
struct Rect RectList[40];
国际货币基金组织;
}s1;
结构测试
{
int categ;
结构状态;
};
结构两点初始值(int v,int w)
{
结构两点温度;
温度值=v;
温度宽度=w;
返回温度;
}
int getValue(结构两点*b)
{
返回(b->value);
}
struct两点获取(struct Rect*r)
{
返回(r->val);
}
void initialize()
{
s1.努米斯坦=0;
s1.numRect=0;
}
void addRect(struct Rect*r)
{
if(s1.numRectval;
s1.RectList[s1.numRect].val\u new=r->val\u new;
s1.RectList[s1.numRect].is\u changed=r->is\u changed;
s1.numRect++;
}
}
struct Rect initRect(struct两点initval)
{
结构矩形温度;
结构两点tempP;
tempP=initPVal(0,0);
温度值=初始值;
临时值=临时值;
温度改变=错误;
addRect(&temp);
返回温度;
}
无效副本(struct Rect*r)
{
如果(r->已更改)
{
r->val=r->val\u新建;
r->is_changed=false;
}
}
void copyRect()
{
int i=0;
对于(i=0;icateg;
s1.实例[s1.numInstances].ptr=m->ptr;
s1.numInstances++;
如果(s1.numInstances>=20)
{
printf(“太多实例”);
}
}
void setModule(结构测试*t)
{
结构模块m;
m、 categ=t->categ;
m、 ptr=&t;
setInstance&m;
}
void init(结构测试*t)
{
t->categ=2;
struct Rect tr;
结构两点tb1=initPVal(0,5);
tr=initRect(tb1);
t->state=tr;
}
无效操作(结构测试*t)
{
struct twoint tb=get(&t->state);
int y=获取值(&tb);
printf(“%d\n”,y);
无符号整数x=getValue(&tb);
printf(“%u\n”,x);
开关(y)
{
....
}
}
void initS()
{   
init(s1.instances[0].ptr);
}   
无效法案
{       
操作(s1.instances[0].ptr);
}
无效设置()
{
结构测试t;
初始化();
初始化(&t);
setModule&t;
}
无效运行()
{ 
initS();
act();
copyRect();
}
int main()
{
printf(“foo\n”);
设置();
printf(“bar\n”);
run();
返回0;
}
有两个错误:

  • 当通过initS()函数调用init()函数时,会导致堆栈过度转储错误,而当我在setup()中调用它时,它工作正常。我认为在执行action()函数时,调用是正确的
  • 第二个问题是actions()函数。当我计算y的值作为开关条件而不是0、1、2或3时,它是我在尝试调试时通过打印找到的某个内存地址

  • 我假设在显示
    run()
    调用
    actions()
    的地方,它实际上是在调用
    act()
    ,因为前一个函数需要一个参数

    setModule()
    中,您有

    m.ptr=&t;
    
    其中
    t
    已经是指向
    struct Test
    的指针。因此,您在
    m.ptr
    中存储的是指向结构的指针。在
    act()
    中,您将存储的指针传递给
    actions()
    ,它期望的是指向结构的指针,而不是指向结构的指针。因此,当函数取消对指针的引用并将结果视为
    结构测试时,
    ,它得到的是谁知道是什么

    很可能,您只需将
    setModule()
    中的行更改为

    m.ptr=t;
    
    因为
    t
    已经是一个指针。但是可能是因为某些原因,您确实希望有一个指向指针的指针,在这种情况下,您需要更改使用指针的代码以正确使用它。这里的一个问题是,编译器无法帮助您确保类型安全,因为您将指针存储为
    void*
    并隐式它可以将其转换为其他类型


    作为旁注,您使用的
    &
    运算符似乎比我想象的要多得多。在我看来,您的函数是只读的,例如
    getValue()
    ,应该只取一个结构参数,而不是指向结构的指针,这样就不需要太多地获取变量的地址。但这可能是一个品味问题。

    我假设在显示
    run()
    调用
    actions()
    的地方,它实际上调用的是
    act()
    ,因为前一个函数需要一个参数

    setModule()
    中,您有

    m.ptr=&t;
    
    其中
    t
    已经是指向
    struct Test
    的指针。因此,您在
    m.ptr
    中存储的是指向结构的指针。在
    act()
    中,您将存储的指针传递给
    actions()
    ,它期望的是指向结构的指针,而不是指向结构的指针。因此,当函数取消对指针的引用并将结果视为
    结构测试时,
    ,它得到的是谁知道是什么

    很可能,您只需将
    setModule()
    中的行更改为

    m.ptr=t;
    
    因为
    t
    已经是一个指针。但是可能是因为某些原因,您确实希望有一个指向指针的指针,在这种情况下,您需要更改使用指针的代码以正确使用它。这里的一个问题是,编译器无法帮助您确保类型安全,因为您将指针存储为
    void*
    并隐式它可以将其转换为其他类型

    作为旁注,您使用的
    &
    运算符似乎比我想象的要多得多。在我看来,您的函数是只读的,例如
    getValue()
    ,只需获取一个结构参数,而不是指向结构的指针,这样您就不需要太多地获取变量的地址。但这可能是一个品味问题。

    问题是:

    void setup()
    {
        struct Test t;
        initialize();
        init(&t);
        setModule(&t);
    }
    
    Test
    结构被分配为本地堆栈
    void setModule(struct Test *t)
    {
        struct Module m;
        m.categ=t->categ;
        m.ptr=&t;
        setInstance(&m);
    }