English 中文(简体)
除非在守则中作打印说明,否则编码便失事。
原标题:Code crashes unless I put a printf statement in it
  • 时间:2011-04-15 05:59:32
  •  标签:
  • c
  • debugging

This is a snippet of code from an array library I m using. This runs fine on windows, but when I compile with gcc on linux if crashes in this function. when trying to narrow down the problem, I added a printf statement to it, and the code stopped crashing.

void _arrayCreateSize( void ***array, int capacity )
{
    (*array) = malloc( (capacity * sizeof(int)) + sizeof(ArrayHeader) );
    ((ArrayHeader*)(*array))->size = 0;
    ((ArrayHeader*)(*array))->capacity = capacity;
    // printf("Test!
");
    *(char**)array += sizeof(ArrayHeader);
}

这份印本一旦拿到,就开始坠毁我。 我完全对发生这种情况的原因感到困惑。

最佳回答

The last line in the function is not doing what was intended. The code is obscure to the point of impenetrability.

看来,目标是在第一次记忆分配中分配一系列的<代码>int,因为<代码>sizeof (int)。 至少,如果你打算分配一系列结构点,你需要使用<条码>(SomeType *),某些点的类型(<条码>(避免*)。 作为书面材料,这将在64轨道环境中 fail然失败。

该阵列由结构负责人(ArrayHeader)分配,然后是阵列。 返回的价值假定是阵列的开始;ArrayHeader大概会从点子中找到。 这显然是罪 sin祸首,无法维持。 它可以工作,但需要极为谨慎,并且(正如Brian Kernighan所说)“如果你在撰写守则时尽可能避免,你会如何去掉”。

页: 1

void _arrayCreateSize( void ***array, int capacity )
{
    (*array) = malloc( (capacity * sizeof(int)) + sizeof(ArrayHeader) );
    ((ArrayHeader*)(*array))->size = 0;
    ((ArrayHeader*)(*array))->capacity = capacity;
    // printf("Test!
");
    *(char**)array += sizeof(ArrayHeader);
}

It adds sizeof(ArrayHeader) * sizeof(char *) to the address, instead of the intended sizeof(ArrayHeader) * sizeof(char). The last line should read, therefore:

*(char *)array += sizeof(ArrayHeader);

或者,正如评论和替代答复所指出的:

*(ArrayHeader *)array += 1;
*(ArrayHeader *)array++;

我顺便指出,职能名称不应真正从强调开始。 外部名称从“强调”开始,留待执行(C汇编者和图书馆)。


问题询问“f(> statement fixances”。 答案是因为它将问题推向了。 你会收到一份Heisenbug,因为所分配的记忆被滥用,而<代码>f()的存在能够稍微改变该守则的行为。

Recommendation

  1. Run the program under valgrind. If you don t have it, get it.
  2. Revise the code so that the function checks the return value from malloc(), and so it returns a pointer to a structure for the allocated array.
  3. Use the clearer code outlined in Michael Burr s answer.
问题回答

在添加似乎无关的<条码>印本(<><>条码>>声明时,任意随机坠毁,常常是腐败跃入的迹象。 汇编者有时直接储存关于分配记忆的信息。 夸大元数据会导致出现意外的操作时间行为。

A few suggestions:

  • are you sure that you need void ***?
  • try replacing your argument to malloc() with 10000. Does it work now?


Moreover, if you just want arrays that store some metadata, your current code is a bad approach. A clean solution would probably use a structure like the following:

struct Array {
    size_t nmemb;    // size of an array element
    size_t size;     // current size of array
    size_t capacity; // maximum size of array
    void *data;      // the array itself
};

现在,你可以通过一个类型的标的<代码>Array,用于了解<代码>/Array类型和Array->数据投向其他一切事物的适当类别。 记忆布局甚至可能与你目前的做法相同,但获得元数据的机会非常容易,尤其明显。

你们的主要听众是,从现在起5年必须维持你的法典的 poor劣。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签