结构成员在通过后损坏,但在再次通过后没有损坏

时间:2011-08-05 09:15:27

标签: c data-structures ansi corruption

我的ANSI C程序中有一些非常奇怪的错误。 我正在使用调试器,我发现'size'变量在函数'doSthing'中被破坏了。在'doSthing'之外''尺寸'得到了一个合适的价值,但在'doSthing'里面,我得到的价值与它应该是什么相似,可能是一些随机数据。这不是一个神秘的但是......

在'doAnotherThing'中,从'doSthing'调用'我再次获得正确的值。我想如果它通过了正确的值,它就不会被破坏,我错了吗?但那为什么它有不同的价值?

struct中的指针在函数内部不会改变。 内存分配给oTVoTV->oT

我真的不知道这里发生了什么......

typedef struct{
ownType     *oT[]   /* array of pointers */
int     size;
} ownTypeVector;

void doSthing(ownTypeVector* oTV);
void doAnotherThing(ownTypeVector* oTV);


void doSthing(ownTypeVector* oTV)
{

...
    doAnotherThing(oTV);
...

}

感谢您的评论,我收集了包含控制逻辑和数据结构的所有代码,以便进行编译。它在嵌入式系统中运行,可以从多个源接收字符,通过给定的规则从中构建字符串,在字符串准备好之后,调用需要该字符串的函数。这也可以是一系列功能。这就是我有函数指针的原因 - 我可以通过选择'activityFromCharacters'函数之外的函数来为一堆事物使用相同的逻辑。 在这里,我通过向AVector添加A-s,B-s和C-s来构建数据结构。  当然,这些独立来源中的每一个都有自己的静态字符串,因此它们不会互相打扰。

更详细的代码版本中的问题:

'aV-> size'在任何地方都有适当的值,除了'handleCaGivenWay'。在获得calles之前,“aV-> size”是可以的,在'addA''aV-> size'也可以。离开'handleCaGivenWay'之后再好了。

#define                     NUMBER_OF_AS            1
#define                     NUMBER_OF_BS            5
#define                     NUMBER_OF_CS            10

typedef struct{
char    name[81];
} C;

typedef struct{
C               *c[NUMBER_OF_CS];   /* array of pointers */
int                     size;
int                     index;
} B;

typedef struct{
B *b[NUMBER_OF_BS];     /* array of pointers */
char    name[81];
int     size;
} A;

typedef struct{
A *a[NUMBER_OF_AS]; /* array of pointers */
int     size;
} AVector;

typedef struct {
char *string1;
char *string2;
} stringBundle;

typedef struct{
void (*getCharacter)(char *buffer);
void (*doSthingwithC)(stringBundle* strings,AVector* aV);
AVector*    aV;

} functionBundle;

void getCharFromaGivenPort(char *buffer)
{
//...
}

void addA(AVector * aV, stringBundle* strings)
{
            aV->a[aV->size]->size = 0;
            ++aV->size;

                int i = 0;

            if(strlen(strings->string2) < 81)
            {
                for(i;i<81;++i)
                {
                    aV->a[aV->size-1]->name[i] = strings->string2[i];
                }
            }
            else {report("Too long name for A:");
                    report(strings->string2);}
}


void handleCaGivenWay(stringBundle* strings,AVector* aV)
{
    A* a;
    a = NULL;
    if(aV->size) { a = aV->a[aV->size-1]; }

switch(1)
{
    case 1:     addA(aV,strings); break;
    case 2:     //addB()...

    default:                if (a && aV->size)
                                    {   //addC(a->thr[a->size-1],c);
                                    }

                            else report("A or B or C invalid");
                            break;
}
//handleCaGivenWay
}

void activityFromCharacters(stringBundle* strings,functionBundle* funcbundle)
{
        /* some logic making strings from characters by */
        /* looking at certain tokens */

        (* funcbundle->doSthingwithC)(strings,funcbundle->aV);
}
//activityFromCharacters

AVector* initializeAVector(void)
{
AVector* aV;

if (NULL == (aV = calloc(1,sizeof(AVector))))
    { report("Cannot allocate memory for aVector."); }

int i = 0;
int j = 0;
int k = 0;

for(i; i < NUMBER_OF_AS; ++i)
{
    if (NULL == (aV->a[i] = calloc(1,sizeof(A))))
            { report("Cannot allocate memory for As."); }

    aV->a[i]->size = 0;
    aV->a[i]->name[0] = 0;

    for(j; j < NUMBER_OF_BS; ++j)
        {
        if (NULL == (aV->a[i]->b[j] = calloc(1,sizeof(B))))
                        { report("Cannot allocate memory for Bs."); }

        aV->a[i]->b[j]->size = 0;

        for(k; k < NUMBER_OF_CS; ++k)
            {
            if (NULL == (aV->a[i]->b[j]->c[k] = calloc(1,sizeof(C))))
                            { report("Cannot allocate memory for Cs."); }
            }
        }
}

aV->size = 0;

return aV;
//initializeProgramVector
}

int main (void)
{
AVector* aV;
   aV = initializeAVector();


while(1)
{
    static stringBundle string;
        static char str1[81];
        static char str2[81];
        string.string1 = str1;
        string.string2 = str2;

        functionBundle funcbundle;
        funcbundle.getCharacter = &getCharFromaGivenPort;
        funcbundle.doSthingwithC = &handleCaGivenWay;
        funcbundle.aV = aV;

        activityFromCharacters(&string,&funcbundle);
}

//main
}

3 个答案:

答案 0 :(得分:0)

您的代码显示它没有任何错误... 但我认为你在doSthing函数中获取大小值是错误的。 你在那里打印它的地址。所以专注于一些指针的东西..

答案 1 :(得分:0)

尝试在调用之前打印oTV->size,并作为doSthing函数中的第一个语句。如果在两个打印中都得到正确的值,那么问题在于函数doSthing。如果您显示调用doSthing的代码,则可以更好地理解问题。

答案 2 :(得分:0)

搜索了很长时间才找到这个。我发现了2个问题,但不知道你到底想要完成什么,所以我无法确定我建议的修正是你想要的。

typedef struct{
    A *a[NUMBER_OF_AS]; /* array of pointers */
    int     size;
} AVector;

// and in addA():
aV->a[aV->size]->size = 0;

首先:您正在内联结构中的指针数组。我认为你想要和需要的是一个指向指针数组的指针,以便它可以增长,这是我想要的addA()。 addA()aV->a[aV->size]->size = 0;中的行不能很好地表达您的意图,但看起来您正试图将值更改为数组中的最后一个条目,因为它在内联中结构将导致单独的字段size在某些对齐上纯粹重合;这是一种非常脆弱的编程方式。所以我建议的是这个。更改结构以包含A** a; // pointer to pointer-array,最初使用malloc并在需要时将其重新malloc(并复制)(在addA()中)。

相关问题