如何在cpp中使用realloc
How to use realloc in cpp
我有以下cpp代码
#include <stdio.h> /*utiliser printf*/
#include <fcntl.h>
#include <math.h> /*utiliser pour les formules de math*/
#include <malloc.h>
#include <iostream.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Le type nombre complexe */
typedef struct {
double Preel;
double Pimaginaire;
} COMPLEXE;
#define ALLOC_ERROR 1
void indienne(double *MatE, int tE, int nE, double *SortieExp, double *Tempbis)
{
double *TempE=NULL, *SortieE=NULL;
int *dec=NULL;
int i, tampon, kE;
kE=(int)(log(nE)/log(2));
if(nE==8)
kE=3;
/* ALLOCATION DES MATRICES*/
if (!(TempE = (double *)calloc(tE * tE, sizeof(double))))
exit(ALLOC_ERROR);
printf("check1 te=%d, nE=%d",tE,nE);
if (!(dec = (int *)realloc(kE , sizeof(int))))
exit(ALLOC_ERROR);
if (!(SortieE = (double *)calloc(tE * tE, sizeof(double))))
exit(ALLOC_ERROR);
printf("check2 te=%d",tE);
memcpy(TempE,MatE,tE * tE * sizeof(double));
for (i=0; i<tE; i++)
*(Tempbis+(tE * i) + i) = 1.0;
if (nE==1)
{
memcpy(SortieExp, MatE, tE*tE*sizeof(double));
}
else
{
printf("kE=%d, nE=%dn", kE, nE);
if (nE%2==0)
decompose(kE, nE,dec);
else
decompose(kE, nE-1, dec);
for (i=0; i<kE; i++)
{
carre(TempE, tE, SortieE);
memcpy(TempE, SortieE, tE*tE*sizeof(double));
tampon=*(dec+i);
if (tampon==1)
{
mult(Tempbis, tE, tE, SortieE, tE, tE, SortieExp);
memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
}
}
if (nE%2 !=0)
{
memcpy(Tempbis, SortieExp, tE*tE*sizeof(double));
mult(Tempbis, tE, tE, MatE, tE, tE, SortieExp);
}
}
free(TempE);
free(SortieE);
free(dec);
}
当我编译这段代码时,出现以下错误
从'int'转换为'void*'无效[-fpermissive]|
这是关于下面的代码行
if (!(dec = (int *)realloc(kE , sizeof(int))))
如何删除此错误?
您将int kE
作为这里的第一个参数:
realloc(kE , sizeof(int))
但是,realloc
是这样声明的:
void *realloc(void *ptr, size_t size);
换句话说,它需要一个指针!请阅读我链接到上面的手册页以获得更多细节。简而言之,您可能想要这样处理错误行:
if (!(dec = (int *)realloc(dec , sizeof(int))))
请注意,这有点糟糕,因为如果realloc
失败,您将失去dec
的原始值,从而导致内存泄漏。如果在出错时退出并不重要,但除此之外,您应该保留dec
的原始值,这样您可以更优雅地处理错误,而不仅仅是退出。
vector
这样的c++容器类,而不是在C的内存分配函数上瞎胡闹。
你的代码中可能还有其他问题,但这个答案并不是代码审查,而只是解释为什么你会得到你得到的错误。
试着不要这样做:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *pa = malloc(10 * sizeof *pa); // allocate an array of 10 int
if(pa) {
printf("%zu bytes allocated. Storing ints: ", 10*sizeof(int));
for(int n = 0; n < 10; ++n)
printf("%d ", pa[n] = n);
}
int *pb = realloc(pa, 1000000 * sizeof *pb); // reallocate array to a larger size
if(pb) {
printf("n%zu bytes allocated, first 10 ints are: ", 1000000*sizeof(int));
for(int n = 0; n < 10; ++n)
printf("%d ", pb[n]); // show the array
free(pb);
} else { // if realloc failed, the original pointer needs to be freed
free(pa);
}
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 无法在UE4中包含BP类到CPP类
- turbojpeg-cpp中的realloc图像缓冲区
- 如何在cpp中使用realloc