我遇到了realloc函数的问题.我只使用C(因此没有向量)与LibCurl.我遇到的问题是我在write_data函数的第12次迭代中得到以下错误(realloc():无效的下一个大小)(我将函数作为回调传递给Curl,每次libcurl都调用它时一些要传回的数据(数据以块的形式传递)).
-Removed-
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
char * Data; //stores the data
size_t RunningSize;
int write_data( char *ptr, size_t size, size_t nmemb, void *stream )
{
size_t ThisSize = (size * nmemb); //Stores the size of the data to be stored
size_t DataLen = strlen( Data ); //length of the data so far
RunningSize = (RunningSize + ThisSize ); //update running size (used as new size)
Data = realloc( Data, RunningSize ); //get new …Run Code Online (Sandbox Code Playgroud) 我试过释放内存而不使用free()如下
int *ptr = malloc(20);
realloc(ptr, 0);
Run Code Online (Sandbox Code Playgroud)
它会起作用吗?
我试图在 GMP 中获得一个大整数的二进制表示。我将 1 和 0 存储在一个名为expBinary的数组中。我使用 malloc 分配大小为“int”的内存,然后在添加新位时使用realloc增加此内存。转换是没有任何问题的完美运行,但是当我尝试后while循环分配使用malloc任何更多的内存,它给我段错误,当我调用同一个代码第二次,第一次给了我不分段错误。我已经检查过并且“expBinary”没有存储任何越界的东西,我已经给出了下面的代码
int binarySize = 0;
int * expBinary = malloc(sizeof(int));
int i = 0;
// Run until exp == 0
while(mpz_cmp_ui(exp,0) != 0)
{
binarySize++;
expBinary = (int*) realloc(expBinary,(binarySize));
// Getting LSB of exp
if(mpz_even_p(exp) != 0)
expBinary[i] = 0;
else
expBinary[i] = 1;
// Incrmenting variables
i++;
// Dividing exponent by 2
mpz_tdiv_q_ui(exp,exp,2);
}
// This line is giving error
int * temp …Run Code Online (Sandbox Code Playgroud) 我有格式字符串,我解析它比用输入参数替换格式说明符.现在我考虑如何在替换参数后为这样的结果字符串分配内存.我可以像格式字符串一样分配这个字符串,但是在%s任何长的位置替换其他字符串
都需要在某些不确定的融合中重新分配这个字符串,这使得有必要在代码中进行一些不优雅的计算.所以我认为我可以分配这个从格式字符串创建的字符串,只需char by char,每次重新分配它,如:
/*** for loop traversing next chars in format string ***/
// if new char
str = realloc(str, sizeof(*str) +1);
// if %s
str = realloc(str, sizeof(*str) + strlen(in_str));
// if %d
str = realloc(str, sizeof(*str) + strlen(d_str));
Run Code Online (Sandbox Code Playgroud) 我有一个简化的C程序,它演示了这个问题.当我尝试realloc使用指针调用时,它工作正常,但如果我尝试向指针添加偏移量(即从数组中的后一个元素开始),它将失败.
# include <stdio.h>
# include <stdlib.h>
struct arbNum{
unsigned char* bytes;
long len;
};
void trim(struct arbNum* num){
printf("%d,%d,%d,%d\n",num->bytes,num->bytes[0],num->bytes[1],num->len);
unsigned char* nbytes = realloc(num->bytes,num->len);
printf("Realloc successful.\n");
num->bytes = nbytes;
}
int main(void){
struct arbNum p = {calloc(2,1),2};
trim(&p);
}
Run Code Online (Sandbox Code Playgroud)
例如,这输出:
9247152,0,0,2
Realloc successful.
Run Code Online (Sandbox Code Playgroud)
但是,从上面的代码中,更改realloc(num->bytes,num->len);为realloc(num->bytes+1,num->len-1);将输出/行为更改为:
10361264,0,0,2
Run Code Online (Sandbox Code Playgroud)
然后崩溃.可能我根本不理解指针,但我目前的理解是它们本质上是存储另一个值的地址.在这种情况下,为什么将pointer+1not point指向存储在高于指针的地址中的值,或者换句话说,将作为该指针所指向的数组的第一个元素.我要做的是num.bytes将从第一个元素开始指向的数组复制到内存中的新地址,但显然,由于某种原因,它失败了.
据我所知,C 标准库允许通过使用 realloc 函数来调整内存分配的大小,如下例所示:
char *a = malloc(10);
char *b = realloc(a, 8);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,a 和 b 可能相等,并且分配已有效地从右侧缩小了两个字节。
但是,我想知道是否有办法从左侧缩小内存分配,如下所示:
char *a = malloc(10);
char *b = /* ... */; // shrink a from the left by 2 bytes
Run Code Online (Sandbox Code Playgroud)
其中(a + 2) == b、 以及 a 开头的原始 2 个字节现在可供分配器使用。这应该发生,而无需将数据复制到内存中的新位置。只需缩小分配即可。
我知道使用 realloc 从右侧缩小内存或手动将数据复制到新的较小分配可能是一种选择,但这些方法不适合我的需求。
有没有办法使用 C 的标准库或提供此功能的任何其他库来实现此目的?
我并不是要求 100% 的保证,realloc 也可以返回指向不同位置的指针,但很可能会。
预先感谢您的见解。
我可以将所有字节向左移动并尝试缩小,但这涉及复制。
在C中,你可以在realloc里面重新分配吗?例如,当您需要对它们两者进行malloc并重新分配它们时,结构中的结构.如果是,有人可以提供一个简单的例子吗?先感谢您.
我使用时遇到了一些问题realloc(),因此我使用尽可能少的代码制作了一个示例程序来说明问题.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
unsigned int i;
unsigned long long *a;
srand(time(NULL));
a = malloc(sizeof(unsigned long long));
for (i = 0; i < 20; ++i)
{
a[i] = rand() % 32;
printf("%llu\n", a[i]);
a = realloc(a, (i + 1) * sizeof(unsigned long long));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这输出:
*检测到glibc demo:realloc():下一个大小无效:0x0000000000dc3010**
为什么会崩溃?
编辑:
我试图chaning (i + 1)来(i + 2),然后程序的工作,但我不明白为什么.我只要求将内存空间扩展一个 unsigned long long.
int *p=malloc(20);
Run Code Online (Sandbox Code Playgroud)
现在Heap将分配20个字节的内存.并将第一个字节的地址返回到指针p.(假设没有返回NULL指针).
现在我这样做,
int *q=realloc(p, 40);
Run Code Online (Sandbox Code Playgroud)
现在他们有以下可能性:
1].q = P
2].!Q = P
3].Q = NULL
忘了可能性2和3.
现在我写道:
free(p);
Run Code Online (Sandbox Code Playgroud)
现在会发生什么?
前20个字节是否会被释放,休息仍然会被分配,或者所有40个字节都将被释放还是其他东西?
嘿,我正试图解决这个学校的运动..
编写一个程序,继续读取字符串并连接它们(将它们添加到单个字符串).串联应该在一个函数中发生,如果成功则返回1,如果失败则返回0.对于内存分配,只使用realloc!
调试程序时我没有收到任何错误,但是当我尝试运行程序时,在插入字符串之后,唯一出现的是"Segmentation Fault",它会是什么?这是代码:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int cat(char **, char *);
int main(void)
{
char string[51];
char *output=NULL;
char choice;
do
{
printf("Please enter a string [<50 chars]: ");
fgets(string,50,stdin);
if(string[strlen(string)-1]=='\n') /* if newline was read as well */
string[strlen(string)-1]=0; /* discard it */
if(cat(&output,string))
printf("\n\nThe string now contains:\n%s\n",output);
else
{
printf("error: memory (re-)allocation failed!\n\n");
return 1; /* exit with error */
}
printf("Continue? (y/n) - ");
fgets(string,3,stdin); /* read input from keyboard - leave a safety buffer to …Run Code Online (Sandbox Code Playgroud)