C 使用 printf 打印非常奇怪的值
C printing extremely weird values with printf
我正在重写这个 post 因为我设法找出了问题所在。我的输出极度损坏的问题是由于动态内存分配不当造成的。
基本上我需要为指向结构的指针数组分配内存,但数组本身嵌套在另一个结构中,嵌套让我有点困惑,最后我把它复杂化了。
所以我有一个名为 Catalog 的结构,我的数组在其中并且该数组指向另一个名为 Books 的结构。
当我最初为它分配内存时,我只为一个数组分配了内存,而不是一个指针数组:
catalog->arrayP = malloc(INITIAL_CAPACITY * sizeof( Books );
// But I should have done this:
catalog->arrayP = (Books *) malloc(INITIAL_CAPACITY * sizeof( Books );
// That first (Books *) was extremely important
我遇到的第二个问题是,当我尝试更新内存以容纳更多书籍时,我实际上正在减少它:
catalog->arrayP = realloc(catalog->arrayP, 2 * sizeof( catalog->arrayP));
// I did this thinking it would just increase the memory to twice that of what it currently was, but it didn't
cataloc->capacity = catalog->capacity * 2;
catalog->arrayP = realloc(catalog->arrayP, catalog->capacity * sizeof( catalog->arrayP));
因此,每当我需要增加我的指针数组时,我最终只分配了足够的内存供 2 本书使用,而不是当前的两倍。
Frankenstein; Or, The Modern Prometh..Shelley, Mary Woll.
您的打印结果有点泄露了答案。您忘记了字符串上的空终止符并且 printf 侵入了下一个字段直到到达空终止符。
在以下字段中它找不到并入侵了更多东西。
这是一个最小的例子
#include <stdio.h>
#include <string.h>
struct test{
char test[37]; // space for 36 chars + null
char test2[16]; // space for 15 chars + null
};
int main(void) {
struct test Test;
strcpy(Test.test, "randomrandomrandomrandomrandomrandom"); // Copy the 37 bytes
strcpy(Test.test2, "notnotnotnotnot"); // Copy the 16 bytes
//Replace null terminator with trash for demonstration purposes
Test.test[36] = '1'; // replaces 37th byte containing the terminator ([=11=]) with trash
printf("%38s", Test.test); // should print randomrandomrandomrandomrandomrandom1notnotnotnotnot
return 0;
}
我正在重写这个 post 因为我设法找出了问题所在。我的输出极度损坏的问题是由于动态内存分配不当造成的。
基本上我需要为指向结构的指针数组分配内存,但数组本身嵌套在另一个结构中,嵌套让我有点困惑,最后我把它复杂化了。
所以我有一个名为 Catalog 的结构,我的数组在其中并且该数组指向另一个名为 Books 的结构。
当我最初为它分配内存时,我只为一个数组分配了内存,而不是一个指针数组:
catalog->arrayP = malloc(INITIAL_CAPACITY * sizeof( Books );
// But I should have done this:
catalog->arrayP = (Books *) malloc(INITIAL_CAPACITY * sizeof( Books );
// That first (Books *) was extremely important
我遇到的第二个问题是,当我尝试更新内存以容纳更多书籍时,我实际上正在减少它:
catalog->arrayP = realloc(catalog->arrayP, 2 * sizeof( catalog->arrayP));
// I did this thinking it would just increase the memory to twice that of what it currently was, but it didn't
cataloc->capacity = catalog->capacity * 2;
catalog->arrayP = realloc(catalog->arrayP, catalog->capacity * sizeof( catalog->arrayP));
因此,每当我需要增加我的指针数组时,我最终只分配了足够的内存供 2 本书使用,而不是当前的两倍。
Frankenstein; Or, The Modern Prometh..Shelley, Mary Woll.
您的打印结果有点泄露了答案。您忘记了字符串上的空终止符并且 printf 侵入了下一个字段直到到达空终止符。
在以下字段中它找不到并入侵了更多东西。
这是一个最小的例子
#include <stdio.h>
#include <string.h>
struct test{
char test[37]; // space for 36 chars + null
char test2[16]; // space for 15 chars + null
};
int main(void) {
struct test Test;
strcpy(Test.test, "randomrandomrandomrandomrandomrandom"); // Copy the 37 bytes
strcpy(Test.test2, "notnotnotnotnot"); // Copy the 16 bytes
//Replace null terminator with trash for demonstration purposes
Test.test[36] = '1'; // replaces 37th byte containing the terminator ([=11=]) with trash
printf("%38s", Test.test); // should print randomrandomrandomrandomrandomrandom1notnotnotnotnot
return 0;
}