我在使用一个旨在成为String缓冲区的程序时遇到了一些麻烦,特别是这个函数用于使用字符串cstr重置缓冲区.如果cstr为null,则需要将内容重置为空char'\ 0'.它总是挂在第二组realloc,它正在调整大小buf->内容我不知道为什么会这样.任何帮助都是极好的.
结构:
typedef struct strbuf {
char *contents;
size_t length;
} StringBuffer;
Run Code Online (Sandbox Code Playgroud)
它被称为
strbuf_reset(sb, NULL)
Run Code Online (Sandbox Code Playgroud)
这是有问题的strbuf_reset函数.
StringBuffer *strbuf_reset(StringBuffer *buf, const char *cstr)
{
if(buf == NULL)
return NULL;
StringBuffer *tempBuf = NULL ;
if(cstr == NULL)
tempBuf = (StringBuffer*)realloc(buf,sizeof(StringBuffer) + sizeof(char));
else
tempBuf = (StringBuffer*)realloc(buf,sizeof(buf) + strlen(cstr)*sizeof(char));
if(tempBuf == NULL)
return NULL;
if(cstr == NULL)
tempBuf->contents = (char*)realloc(buf->contents,sizeof(char));
else
tempBuf->contents = (char*)realloc(buf->contents,(sizeof(buf->contents) + strlen(cstr)*sizeof(char) + 1));
if(tempBuf->contents == NULL){
free(tempBuf);
return NULL;
}
buf = tempBuf;
if(cstr == NULL) …Run Code Online (Sandbox Code Playgroud) 这个问题有点长,因为源代码,我试图尽可能简化.请耐心等待,并感谢您的阅读.
我有一个循环的应用程序可能运行数百万次.而不是在该循环中的数千到数百万malloc/次free呼叫,我想先做一个malloc,然后做几千到几百万次realloc呼叫.
但是当我使用时,我遇到了一个问题,即我的应用程序消耗了几GB的内存并自行杀死realloc.如果我使用malloc,我的内存使用情况很好.
如果我在较小的测试数据集运行valgrind的内存测试,它报告没有内存泄漏有两种malloc或realloc.
我已经验证我将每个malloc-ed(然后是realloc-ed)对象与相应的匹配free.
所以,从理论上讲,我并没有泄漏内存,只是使用realloc似乎消耗了我所有可用的RAM,我想知道为什么以及我能做些什么来解决这个问题.
我最初的东西是这样的,使用malloc和正常工作:
Malloc代码
void A () {
do {
B();
} while (someConditionThatIsTrueForMillionInstances);
}
void B () {
char *firstString = NULL;
char *secondString = NULL;
char *someOtherString;
/* populate someOtherString with data from stream, for example */
C((const char *)someOtherString, &firstString, &secondString);
fprintf(stderr, "first: [%s] | …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用fscanf()来提供一个数组,同时循环遍历包含整数列表的文件,n个整数长整数.似乎我需要使用malloc和/或可能realloc.我听说malloc命令占用了相当多的执行时间,并且最好过度分配.有人会介意帮助我理解实现这一目标的基石吗?
免责声明:我是C的新手
我尝试以下方法重新分配一个float大小从2X2到3X3 的2D 数组.代码抛出一段segfault时间试图realloc记忆weights[2].
num_vertices = 2;
float **weights = malloc(num_vertices*sizeof(float *)); // weight array matrix
for(i = 0; i < num_vertices; i++){
weights[i] = malloc(num_vertices*sizeof(float));
}
num_vertices = 3;
weights = realloc(weights, num_vertices*sizeof(float *)); // weight array matrix
for(i = 0; i < num_vertices; i++){
weights[i] = realloc(weights[i], num_vertices*sizeof(float));
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以再次free使用2D阵列malloc,但我正在寻找更优雅的解决方案.有任何想法吗?
我正在编写一个程序,其中输入将从stdin中获取.第一个输入是一个整数,表示从stdin读取的字符串数.我只是逐个字符地读取字符串到动态分配的内存中,并在字符串结束后显示它.
但是当字符串大于分配的大小时,我使用realloc重新分配内存.但即使我使用memcpy,程序仍然有效.是不是使用memcpy的未定义行为?但是在C中使用Realloc的示例不使用memcpy.那么哪一个是正确的方法呢?我的程序如下所示是正确的吗?
/* ss.c
* Gets number of input strings to be read from the stdin and displays them.
* Realloc dynamically allocated memory to get strings from stdin depending on
* the string length.
*/
#include <stdio.h>
#include <stdlib.h>
int display_mem_alloc_error();
enum {
CHUNK_SIZE = 31,
};
int display_mem_alloc_error() {
fprintf(stderr, "\nError allocating memory");
exit(1);
}
int main(int argc, char **argv) {
int numStr; //number of input strings
int curSize = CHUNK_SIZE; //currently …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个数组来保存一个int,然后当要添加另一个int时,增加它的大小以容纳另一个int ..依此类推..
我知道这不是realloc的有效使用,但它更能证明概念.只是让它工作将允许我优化它并能够将它应用于有用的东西.一个工作的例子.当我调用print函数时,问题就来了,它只是段错误.任何帮助,将不胜感激.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef char String[100];
void begin(int *);
void add(int *, int);
void print(int *);
int tempcount=0;
int main(void)
{
int *n=NULL;
String menu;
begin(n);
while(true)
{
scanf("%9s", menu);
if(!strcmp("a", menu)) //add
{
int i=0;
scanf("%d", &i);
add(n, i);
}
else if(!strcmp("p", menu)) //print
{
print(n);
}
else if(!strcmp("q", menu)) //quit
{
free(n);
break;
}
}
return 0;
}
void begin(int *n)
{
n=malloc(sizeof(int));
if(n==NULL)
{
printf("Error in malloc!");
return;
} …Run Code Online (Sandbox Code Playgroud) 我需要实现一个指向typedef指针的简单动态指针数组.
每次用户请求时使用realloc,数组大小将增加sizeof(指针).
所以我拥有的是:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef void* node;
void printmem(node* a, int c) {
int i;
for (i = 0; i < c; ++i)
{
printf("%d\t\t%p\n", i, a[i]);
}
}
int main(void) {
node* nodes = NULL;
int i = 0, count = 20;
for (i = 0; i < count; ++i)
{
nodes = realloc(nodes, sizeof(node));
}
printf("Done reallocating\n");
printmem(nodes, i);
// free(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
Done reallocating
0 (nil)
1 (nil)
2 (nil) …Run Code Online (Sandbox Code Playgroud) 我的问题是'realloc'.以下代码正常工作(没有警告):
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int num=10;
int *vet;
int i;
for (i=0; i<num; i++)
{
/* allocate memory of vet to contains (i+1) int */
vet = (int*) realloc ( vet, (i+1) * sizeof(int) );
/* write numbers in the allocated memory */
vet[i] = 321 + i;
}
/* print test, if all works I must see:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | …Run Code Online (Sandbox Code Playgroud) 我正在做一个K和RC编程书的乐趣练习.该程序用于从用户输入的一组行中查找最长行,然后打印它.
This is a test
This is another long test
this is another long testthis is another long test
Run Code Online (Sandbox Code Playgroud)
它对前两个输入运行正常,但对于较大的字符串(第三个输入)则失败
Error in `./longest': realloc(): invalid next size: 0x000000000246e010 ***
Error in `./longest': malloc(): memory corruption (fast): 0x000000000246e030 ***
Run Code Online (Sandbox Code Playgroud)
我一直试图调试这个2天(橡皮鸭调试),但逻辑似乎很好.GDB指向_getline函数中的realloc调用,并在顶部显示glibc.so内存分配调用的大量回溯.
这是我写的(部分,部分内容直接取自本书): -
#include <stdio.h>
#include <stdlib.h>
int MAXLINE = 10;
int INCREMENT = 10;
char* line = NULL, *longest = NULL;
void _memcleanup(){
free(line);
free(longest);
}
void copy(char longest[], char line[]){
int i=0;
char* temp = realloc(longest,(MAXLINE)*sizeof(char)); …Run Code Online (Sandbox Code Playgroud) 我已经做了一个最小的工作示例,说明如何使用realloc向数组添加元素.这将在具有更多元素的未来计划中进行扩展.
#include <stdio.h>//printf
#include <stdlib.h>//malloc, realloc
int main() {
//BEGIN REALLOCATE-ABLE ARRAY
unsigned int *array, loop_variable;
const unsigned int ORIGINAL_ARRAY_SIZE = 4, REALLOC_INDICES = 99;
array = malloc(ORIGINAL_ARRAY_SIZE*sizeof(unsigned int));
for (loop_variable = 0; loop_variable < ORIGINAL_ARRAY_SIZE; loop_variable++) {
array[loop_variable] = loop_variable;
}
//BEGIN REALLOCATION
for (loop_variable = 1; loop_variable < REALLOC_INDICES; loop_variable++) {
array = realloc(array,sizeof(unsigned int)*(ORIGINAL_ARRAY_SIZE+loop_variable));
array[ORIGINAL_ARRAY_SIZE+loop_variable-1] = 2*(ORIGINAL_ARRAY_SIZE+loop_variable-1);
printf("reallocate array[%d] = %d\n",ORIGINAL_ARRAY_SIZE+loop_variable-1,array[ORIGINAL_ARRAY_SIZE+loop_variable-1]);
}
//BEGIN PRINTING ARRAY VALUES
for (loop_variable = 0; loop_variable < ORIGINAL_ARRAY_SIZE+REALLOC_INDICES-1; loop_variable++) {
printf("array[%d] = …Run Code Online (Sandbox Code Playgroud)