我在for循环中遇到了一个疯狂的错误
matr=realloc(matr, newmax*sizeof(matr*));
for (i=0; i<newmax; i++){
matr[i]=realloc(matr[i], newmax*sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
matr是一个多维数组:int**matr.我需要调整列和行的大小.第一行调整列大小,for循环调整每一行的大小.它在c中运行良好.现在我正在为lua工作,它崩溃了.compilin的工作也很好.但是从lua打来的电话崩溃了
lua: malloc.c:3552: mremap_chunk: Assertion `((size + offset) & (mp_.pagesize-1)) == 0' failed.
Run Code Online (Sandbox Code Playgroud)
我没有该死的想法,因为它在c中使用它很好.
作为更大程序的一部分,我有一种计算多项式导数系数的方法.但那不是问题:).在下面的代码中,如果我跳过realloc()调用,我会得到我期望的结果(*coef).使用realloc将其中一个元素设置为0.这是我的错,还是realloc()行为实际上是意外的?
void derive (double **coef, int size)
{
int i;
if (size<2)
free_vector (*coef);
else
{
for (i=0; i<size-1; ++i)
{
(*coef)[i] = (*coef)[i+1]*(i+1);
}
(*coef) = realloc ((*coef), size-1);
}
}Run Code Online (Sandbox Code Playgroud)
如果运行它,我还会附上测试程序的完整源代码...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *new_vector (int size)
{
double *vec = calloc (size, sizeof (double));
return vec;
}
void free_vector (double *vec)
{
if (vec)
{
free (vec);
vec = NULL;
}
}
void write_vector (FILE *f, double *vec, int size)
{
int i; …Run Code Online (Sandbox Code Playgroud) 我正在尝试动态增加int数组的内存,但是我遇到了让它运行的问题.它没有扩展并向阵列添加更多元素,我不确定我做错了什么.请帮忙!
int* fibs = NULL;
void genFibs(){
int i = 1,curSize = 0,curNum = 0;
int flag = 1;
while(flag){
if(curSize != 0 &&curSize != 1){
curNum = fibs[curSize-2]+fibs[curSize-1];
}else if(curSize-1 == 1){
curNum = fibs[curSize-1]+fibs[curSize-1];
}else{
curNum = 1;
}
if(curNum<=10){
curSize++;
fibs = (int*)realloc(fibs,curSize*sizeof(int));
fibs[curSize-1] = curSize;
}else{
flag = 0;
}
}
}
}
void printFibs(){
int size = sizeof(fibs)/sizeof(int);
int i = 0;
for(i = 0;i<size;i++){
printf("%d is: %d\n",i,fibs[i]);
}
}
Run Code Online (Sandbox Code Playgroud) 我需要为进程分配所有可用内存,以便实现系统服务的测试.测试(以及其他)需要耗尽所有可用资源,尝试呼叫以及检查特定结果.
为了做到这一点,我写了一个循环,重新分配一块内存,直到,realloc返回null,然后使用最后一个好的分配,然后削减上一个成功数量和最后一个不成功的数量之间的差异,直到不成功的数量为1个字节大于上一个成功数量,保证消耗所有可用内存.
我写的代码如下(调试打印也包括在内)
#include <stdio.h>
#include <malloc.h>
int main(void)
{
char* X;
char* lastgood = NULL;
char* toalloc = NULL;
unsigned int top = 1;
unsigned int bottom = 1;
unsigned int middle;
do
{
bottom = top;
lastgood = toalloc;
top = bottom*2;
printf("lastgood = %p\ntoalloc = %p\n", lastgood, toalloc);
if (lastgood != NULL)
printf("*lastgood = %i\n", *lastgood);
toalloc = realloc(toalloc, top);
printf("lastgood = %p\ntoalloc = %p\n", lastgood, toalloc);
if (toalloc == NULL && lastgood != NULL)
printf("*lastgood = …Run Code Online (Sandbox Code Playgroud) 如果我有一个变量,str我想在堆上分配内存,我会使用malloc():
char* str = (char*)malloc(sizeof("Hello"));
Run Code Online (Sandbox Code Playgroud)
malloc()返回一个void*指针,这是我的记忆所在的内存位置.所以现在,我可以给它一些数据
str = "Hello";
Run Code Online (Sandbox Code Playgroud)
所以,内存位置现已满,有6个字节.现在,我想增加它的大小,以包含字符串"Hello World".所以,我用realloc().根据man, void* realloc(void *ptr, size_t size)将:
The realloc() function changes the size of the memory block pointed to by ptr to size bytes.
The contents will be unchanged in the range from the start of the region up to the minimum of
the old and new sizes. If the newsize is larger than the old size, …Run Code Online (Sandbox Code Playgroud) 我似乎在使用realloc扩展数组时遇到了麻烦...请帮我解决这里的代码:
main()
{
int resultarray[] = {1}, i = 0 ,ch ;
int *number = malloc(sizeof(int));
printf("Enter number : ");
while ( ch = getchar() != '\n' ) {
number[i++] = ch-48 ;
number = realloc(number,sizeof(int));
}
printf("%d",i);
}
Run Code Online (Sandbox Code Playgroud)
*"./a.out"出错:realloc():下一个大小无效:0x0000000002083010*
我无法减少动态创建的数组的大小.这是我的main函数的样子:
int main(void) {
// Intialize big array
int * a = (int *)malloc(10*sizeof(int));
assert(a);
// Fill it with squares
for (int i = 0; i < 10; ++i)
a[i] = i*i;
// Expected to print 64
printf("%d\n", a[8]);
// Shrink the big array
int * b = (int *)realloc(a, 5*sizeof(int));
assert(b);
// Expected to cause SEGFAULT
printf("%d\n", b[8]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除了printf("%d\n", b[8]);行,因为它打印64,但没有像我预期的那样导致SEGFAULT错误.为什么?
我想我错过了一些简单的东西,因为我已经看到很多与缩小记忆有关的问题realloc,但他们都认为这是可能的.
我正在使用带有GCC 4.8.2的Ubuntu 14.04并使用-std=c99选项进行编译.
void replace(char *str) {
unsigned int len = 0;
unsigned int no_of_spaces = 0;
while (*str) {
if ((char)*str == SPACE)
no_of_spaces++;
str++;
len++;
}
unsigned int new_len = len + 2 * no_of_spaces;
str = (char*) realloc(str, new_len * sizeof(char));
str[new_len] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
我用的功能就像replace("random string");.
在这里,我试图增加字符串的大小,以便可以用另一个字符串替换空格.为此,我需要计算空格的数量,并获得原始字符串的长度.我已经能够做到这一点.
为了调整大小我正在使用realloc,但当我运行它,它给出了Aborted (core dumped)?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *func(char * str){
int len;
len=strlen(str)+3;
str = (char *)realloc(str,len);
return str;
}
void main(){
printf("str:%s",func("hello"));
}
Run Code Online (Sandbox Code Playgroud)
最后的ans打印(null),而不是打印字符串:"hello".任何人都可以解释为什么会如此?我无法识别任何错误. 任何人都可以纠正错误,并帮助我使用正常工作的代码.请!
我想用这个方法在字符串前添加零:
void addFrontzeros(char *str,int zeros)
{
if(zeros==0)
{
return;
}
char *temp=malloc((strlen(str)+1+zeros)*sizeof(char));
int i=0;
for(;i<zeros;i++)
{
temp[i]='0';
}
int j=0;
for(;j<strlen(str);j++)
{
temp[j+zeros]=str[j];
}
temp[strlen(str)+zeros]=0;
str=realloc(str,(strlen(temp)+1)*sizeof(char));
strcpy(str,temp);
free(temp);
}
Run Code Online (Sandbox Code Playgroud)
但是当我从另一个方法调用它时,调用后字符串为空.调用者字符串在另一种方法中分配如下:
char *mul = malloc(sizeof(char)*2);
mul[0]='0';
mul[1]=0;
Run Code Online (Sandbox Code Playgroud)
使用valgrind我收到此错误:地址0x5201b00是一个大小为2的块内的0字节
我认为问题在于realloc,但我不知道为什么它不会在这里工作