背景:
我使用 calloc() 创建了一个数组,一切都很好。然后我使用 realloc() 使数组更大。它似乎只是创建了一个没有任何内容的新指针,并在我尝试访问数组中的元素时调用了运行时错误。
我的代码:
#include <stdio.h>
int main() {
int *arr;
for (int i = 0; i < 5; i++){
if (i == 0) {
if ((arr = (int *) calloc(1, sizeof(int))) == NULL) {
printf("NO MORE SPACE TERMINATING PROGRAM");
return NULL;
}
} else {
int *tmp;
if ((tmp = (int *)realloc(arr, 4*sizeof(int)) == NULL)){
printf("NO MORE SPACE TERMINATING PROGRAM");
}
arr = tmp;
}
arr[i] = i;
}
}
Run Code Online (Sandbox Code Playgroud)
arr[i]=i;当 i = 1 时,它在线上抛出运行时错误; …
我正在尝试创建一个函数来从文本文件中读取一行,fgets()并将其存储在动态分配的 char* 中,malloc()但我不确定如何使用realloc(),因为我不知道这一行的长度文本,并且不想只是猜测该行可能的最大大小的幻数。
#include "stdio.h"
#include "stdlib.h"
#define INIT_SIZE 50
void get_line (char* filename)
char* text;
FILE* file = fopen(filename,"r");
text = malloc(sizeof(char) * INIT_SIZE);
fgets(text, INIT_SIZE, file);
//How do I realloc memory here if the text array is full but fgets
//has not reach an EOF or \n yet.
printf(The text was %s\n", text);
free(text);
int main(int argc, char *argv[]) {
get_line(argv[1]);
}
Run Code Online (Sandbox Code Playgroud)
我计划用文本行做其他事情,但为了保持简单,我刚刚打印了它,然后释放了内存。
另外: main 函数是通过使用文件名作为第一个命令行参数来启动的。
如何realloc()重新分配首先分配的内存malloc()?
我知道malloc()在重新分配内存之前您需要使用它,但我不明白这应该如何工作。如果动态内存对象的大小减少了realloc()怎么办?调用后是否刚刚删除了该对象的各个部分realloc()?
我的问题是:
realloc()函数如何重新分配由创建的动态内存对象malloc()?
注意:我做了这个问答是因为许多初学者似乎仍然对重新分配内存使用的问题感到困惑,realloc()尽管这里已经存在关于该主题的问题。对于不熟悉该主题但仍不能代表realloc(). 因此,由于问题,恕我直言,仍然不太适合我想要给出的答案,我进行了自己的问答。
以下是考试的引述(1% 的顶尖大学)。
我失败了,因为我的答案与“批准”的答案不同。
我有一种预感,他(教授,著名的 C 专家)的答案是不正确的。
以下是问题后跟“批准”的答案。
以下功能存在潜在错误。它是什么,我将如何修复它?
提示:这与 realloc() 函数的使用有关。请确定您将更改的行号以及您将用什么来替换它们。
Run Code Online (Sandbox Code Playgroud)BOOLEAN lengthen_string(char* string, const char newcontents[]) { int newlen = strlen(string) + strlen(newcontents) + 1; string = realloc(string, newlen); if (!string) { perror("malloc"); return FALSE; } strcat(string, newcontents); return TRUE; }
教授提供的“正确”答案是:
第 4 行: realloc 在分配失败时返回 NULL 指针。这意味着一旦失败,原始数据就会丢失。
要解决此问题,请将 realloc 的结果分配给临时变量并首先对其进行测试。
即:第4行:
Run Code Online (Sandbox Code Playgroud)char * temp=realloc(string, newlen); if(!temp) ... (all remains the same)在旧的第 9 行之后,
string = temp;
有什么想法吗?
顺便说一句,我的回答是 @string 是一个局部变量,函数的原型应该是 char **string,调用者传递一个指向其字符串指针的指针,然后被调用者会将任何 realloc() 返回值分配给 …
这里是相对较新的 C 程序员。我正在审查以下代码,作为我正在练习 C 的业余项目的教程。该abuf结构的要点是创建一个可以附加的字符串。这是代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct abuf {
char* str;
unsigned int size;
} abuf;
void abAppend(abuf *ab, const char *s, int len) {
char *new = realloc(ab->str, ab->size + len);
if (new == NULL) return;
memcpy(&new[ab->size], s, len);
ab->str = new;
ab->size += len;
}
int main(void) {
abuf ab = {
NULL,
0
};
char *s = "Hello";
abAppend(&ab, s, 5);
abAppend(&ab, ", world", 7);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一切都编译完毕,我的测试(为简单起见进行了编辑)显示字符串“Hello”存储在 …
int getmin(int a, int b)
{
return a<b?a:b;
}
void *reallocation(void *ptr, size_t size) //size_t in bytes
{
void *newptr;
int msize;
msize = getsize(ptr);
msize = getmin(msize, size);
printf("msize = %d", msize);
newptr = malloc(size);
newptr = memcpy(newptr, ptr, msize);
free(ptr);
return newptr;
}
Run Code Online (Sandbox Code Playgroud)
我已经实现了自己的realloc,并且为了使用malloc获得分配的内存的大小(但是我知道在c中没有任何方法).
我的重新分配功能在我的系统上正常工作我们如何获得malloc()分配的内存大小.
如果先前分配的内存大小大于新的所需内存,我们也可以进行内部重新分配吗?
在一个函数中我使用了malloc:
void name1(struct stos* s)
{
s = malloc (4 * sizeof (int));
}
Run Code Online (Sandbox Code Playgroud)
一切都很好.但后来我使用了realloc
void name2(struct stos* s)
{
s->size = 2*(s->size);
s = realloc (s, (s->size + 1) * sizeof (int));
}
Run Code Online (Sandbox Code Playgroud)
我在valgrind中得到无效的free/delete/realloc,realloc返回NULL.
结构声明和其余程序是:
struct stos
{
int top;
int size;
int stk[];
};
void name1(struct stos* s);
void name2(struct stos* s);
int main()
{
struct stos stosik;
struct stos* s;
s = &stosik;
name1(s);
//some operations on the array and int top here
name2(s);
}
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我找了很长时间可能出错的地方,阅读了很多关于指针,malloc/realloc等的文章,但没有结果.如果有人能帮助我,我将非常感激.
以下面的代码为例:
Struct* allocSomething(void) {
int n;
Struct *something = malloc(n*sizeof(Struct));
return something;
}
Struct* reallocSomething(Struct **s) {
int n;
Struct *something = realloc(*s, (n*sizeof(int)) - 1 * sizeof(Struct));
return something;
}
int main() {
Struct *point = allocSomething();
//code does something...
point = reallocSomething();
free(point);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,拨打电话后reallocSomething,point返回的地址是否仍然相同allocSomething?例如,如果point具有address 0x01,则当该指针由重新分配时reallocSomething,该地址是否仍然0x01?
在C89中,以下代码可以正常工作:
typedef struct{
int size;
int **ptr;
}foo;
int main()
{
foo *var;
var = malloc(sizeof(foo) + 2 * sizeof(int*));
int a = 3, b = 6;
var->ptr[0] = &a;
var->ptr[1] = &b;
printf("%d %d\n", (*var->ptr[0]), (*var->ptr[1]));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行时显示:3 6
但如果我更换
var = malloc(sizeof(foo) + 2 * sizeof(int*));
Run Code Online (Sandbox Code Playgroud)
同
var = malloc(sizeof(foo));
var = realloc(var, sizeof(foo) + 2 * sizeof(int*));
Run Code Online (Sandbox Code Playgroud)
它编译时没有任何警告,但我在以下行得到了分段错误:
var->ptr[0] = &a;
Run Code Online (Sandbox Code Playgroud)
我真的不知道我做错了什么,所以我很感激任何输入.我是初学者,所以如果我的错误显而易见,我道歉.
谢谢.