- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我被指派为我们的学校辩护制定一个有效的学生选举计划(并且可能用于实际使用......)。然而,我已经被一个问题困扰了一段时间。
我的目标是创建一个灵活的结构数组,因为我不能使用任意限制,而且数组对我来说有 99 项的限制(*请参阅帖子末尾)。我已经使用了 realloc() 但它为无效的旧大小提供了 abort() 。但是,我尝试在另一个程序中测试动态结构数组,它工作得完美无缺。我无法弄清楚是什么导致另一个崩溃。
我的选举计划(即崩溃的计划):
注意:entr_cmd函数只是将光标移动到屏幕底部并打印文本,而STREQL只是查看两个字符串是否匹配,只是strcmp的快捷宏
struct candidate {
long lrn;
char *name;
int grade;
char *section;
char *party;
char *position;
}
**candidates,
// :: Temporary Array for storing all the candidates in the position to be voted in
**candidates_cur;
int can_c = 0;
[...]
int main() {
[...]
candidates = malloc(2 * sizeof(struct candidate *));
[...]
if(STREQL(command, "c")) {
struct candidate *c;
if(can_c > 1) {
struct candidate **tmp;
tmp = (struct candidate**) realloc(candidates, (1 + can_c) * sizeof(struct candidate *));
if(tmp != NULL) candidates = tmp;
}
candidates[can_c - 1] = malloc(sizeof(struct candidate *));
c = candidates[can_c - 1];
entr_cmd("Candidate's Name: ");
// :: This recieves the input but replaced for testing
c->name = malloc(4 * sizeof(char));
strcpy(c->name, "XXX");
can_c++;
}
[...]
完美运行的测试程序:
这会为测试结构的成员生成一个随机数字字符串
struct test {
char *name;
char *another;
int test;
} **arr;
int main() {
int r1;
arr = malloc(2 * sizeof(struct test *));
r1 = rand() % 45;
for(int i = 0; i < r1; i++) {
int r2 = rand() % 22;
if(i > 2) {
struct test **data;
data = (struct test**) realloc(arr, (2 + i) * sizeof(struct test*));
if(data != NULL) {
arr = data;
}
}
arr[i] = malloc(sizeof(struct test *));
struct test *t = arr[i];
t->name = malloc(r2 * sizeof(char));
t->another = malloc(r2 * sizeof(char));
t->test = r2;
for(int ii = 0; ii < r2; ii++) {
t->name[ii] = (char) (rand() % 9) + '0';
t->another[ii] = (char) (rand() % 9) + '0';
}
printf("====[%u]====\n%s\n%s\n%u\n", i, arr[i] -> name, arr[i] -> another, arr[i] -> test);
}
for(int i = 0; i < r1; i++) {
free(arr[i]->name);
free(arr[i]->another);
free(arr[i]);
}
free(arr);
getch();
}
任何帮助将不胜感激,因为我们的老师对我寄予厚望,并告诉我这会很容易容易,但事实证明恰恰相反。
感谢您的阅读,祝您有美好的一天!!!
额外的废话:我在一门类(class)中,我们经常使用 TurboC++ 进行编程,但是在那里做到这一点非常困难,所以我转而使用 C99 和我的 neovim 设置,这样我就可以快速导航和C99 中的大部分功能也可以在 TurboC++ 中运行。换句话说,我无法真正使用(最新标准)C++,如果是这样,我想我可能会更容易地做到这一点
最佳答案
问题与内存分配根本无关!出现问题是因为我不断分配候选者[-1],我已将候选者[can_c - 1] 放入候选者[can_c] 内存中。
感谢所有提供帮助的人! (@someprogrammerdude 指出了这一点)
关于c - realloc() 结构数组的旧大小无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098545/
如果 realloc 失败并返回 NULL 是前一个缓冲区被释放还是保持不变?我没有在手册页中找到那条特定的信息,我不确定该怎么做。如果内存被释放,那么双重释放可能会有风险。如果没有,就会发生泄漏。
OS: Linux CC: GCC 4.8.2 目标:改变 char* 的大小 -> 变小 问题:更改后的大小相同... 行是带有数据的字符串... 代码片段: char * tmp = NUL
在一个函数中我使用了 malloc : void name1(struct stos* s) { s = malloc (4 * sizeof (int)); } 一切正常。但是后来我用了rea
我知道有一个 realloc允许我调整内存块大小的函数(它与一个免费函数配对)。但是,我正在尝试对一些成员指针使用 new 而不是 realloc 分配内存的 c++ 类执行相同的操作。在 C++ 中
我正在尝试在 C 中创建一个动态整数数组,它应该在填满后自动将其大小加倍。 要扩展数组的大小,我想使用 realloc 函数。不幸的是,指向我的 DynamicArray 和 GCC 崩溃的数据的指针
这是我被教导使用的方式 realloc() : int *a = malloc(10); a = realloc(a, 100); // Why do we do "a = .... ?" if(a
我尝试在每个循环中使用 realloc(),因此我只为 C 中的 int 数组使用必要的内存,但输出值已更改。尽管如此,在我的代码中使用 Valgrind 时,我得到了正确的值。 我在做 Advent
平台:Linux 3.2.0 x86 (Debian Wheezy) 编译器:GCC 4.7.2 (Debian 4.7.2-5) 我想知道如果我尝试 realloc() 一个已递增的指针会发生什么。
我知道可以在内核中使用 malloc 在 GPU 的全局内存上分配内存。是否也可以使用realloc? 最佳答案 您可以为您的数据类型编写自己的 realloc 设备函数。 只需为新数组分配新空间,将
我在对数组使用 malloc/realloc 命令时遇到了一些问题。我创建了一个包含一些整数的小数组,并尝试通过使用 realloc 扩展大小并添加值来为其添加一个值,但是当我这样做时,0 索引的值不
背景: 我使用 calloc() 创建了一个数组,一切都运行良好。然后我使用 realloc() 使数组更大。它似乎只是创建一个没有任何内容的新指针,并在我尝试访问数组中的元素时调用运行时错误。 我的
假设我已经使用 malloc() 分配了内存,如果我在我的代码中这样做: char *newline = realloc ( oldline , newsize ); // Assuming oldl
我正在尝试在下面的程序中使用 realloc 重新分配内存,并在我使用 malloc(i = (int*)malloc(5 * sizeof(int))) 使用react的 realloc 初始内存之
为什么下面的代码输出两次 4,而不是 8 和 20?谢谢 int size = 0; int *pointer; pointer = malloc(2 * sizeof(int)); size = s
我正在尝试将一堆 WCHAR 添加到缓冲区。这个函数就是将它添加到我的缓冲区中的原因.. DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHA
可能我的大脑现在不能正常工作......我想知道为什么我在我的代码中收到提到的错误: int ** zm; zm = (int**)calloc(1, sizeof(int*)); *zm = (in
我正在尝试用 C 语言编写代码,但遇到了 realloc 的问题。该代码在某个时间点工作正常,但在另一时间重新分配期间因堆损坏错误而崩溃。我已将填充数据的结构和函数粘贴到其中。谁能告诉我我是否在这里做
realloc 会改变它的第一个参数吗? 改变第一个参数是否取决于实现? 有什么理由不应该是const吗?作为反例,memcpy 将其 src 参数设为 const。 ISO C 标准,第 7.20.
我在 realloc 中遇到此错误,该错误仅发生在我学校的实验室计算机上,而不发生在我的计算机上。 在此程序中,我将行号存储在 File_Node 结构中。 File_Node 是一个链表的一部分,每
来自 man realloc:realloc() 函数返回一个指向新分配的内存的指针,该指针适合任何类型的变量,可能与 ptr 不同,如果请求失败,则返回 NULL . 因此在此代码片段中: ptr
我是一名优秀的程序员,十分优秀!