我无法理解如何为双指针分配内存.我想读取一个字符串数组并存储它.
char **ptr;
fp = fopen("file.txt","r");
ptr = (char**)malloc(sizeof(char*)*50);
for(int i=0; i<20; i++)
{
ptr[i] = (char*)malloc(sizeof(char)*50);
fgets(ptr[i],50,fp);
}
Run Code Online (Sandbox Code Playgroud)
而不是这个我只是分配一个大的内存块并存储字符串
char **ptr;
ptr = (char**)malloc(sizeof(char)*50*50);
Run Code Online (Sandbox Code Playgroud)
那会错吗?如果是这样,为什么呢?
我认为以下将给我10个不稳定的整数
volatile int foo[10];
Run Code Online (Sandbox Code Playgroud)
但是,我认为以下内容不会做同样的事情.
volatile int* foo;
foo = malloc(sizeof(int)*10);
Run Code Online (Sandbox Code Playgroud)
如果我错了,请纠正我,以及如何使用malloc获得易失性的项目数组.
谢谢.
我在下面的代码中有一个分段错误,但在我将其更改为指针指针后,它很好.有人能给我任何理由吗?
void memory(int * p, int size) {
try {
p = (int *) malloc(size*sizeof(int));
} catch( exception& e) {
cout<<e.what()<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
它在主要功能中不起作用
int *p = 0;
memory(p, 10);
for(int i = 0 ; i < 10; i++)
p[i] = i;
Run Code Online (Sandbox Code Playgroud)
但是,它的工作方式是这样的.
void memory(int ** p, int size) { `//pointer to pointer`
try{
*p = (int *) malloc(size*sizeof(int));
} catch( exception& e) {
cout<<e.what()<<endl;
}
}
int main()
{
int *p = 0;
memory(&p, 10); //get the address of …Run Code Online (Sandbox Code Playgroud) 假设我struct在ANSI C中有这个:
typedef struct _point
{
float x;
float y;
} Point;
Run Code Online (Sandbox Code Playgroud)
这个函数来创建这个struct:
Point createpoint(float x, float y)
{
Point p;
p.x = x;
p.y = y;
return p;
}
Run Code Online (Sandbox Code Playgroud)
这允许我创建一个struct具有此功能,即:
int main()
{
Point pointOne = createpoint(5, 6);
Point pointTwo = createpoint(10, 4);
float distance = calculatedistancefunc(pointOne, pointTwo);
/* ...other stuff */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人告诉我这段代码无效,因为在返回之前它struct没有malloc在createpoint(float x, float y)函数中获取,并且struct将被删除.但是,当我使用我struct这样的时候,它似乎没有被删除.
所以我的问题是:我必须malloc这样做struct …
出于这样或那样的原因,我想手动滚动归零版本malloc().为了最大限度地减少算法的复杂性,我想写:
void * my_calloc(size_t size)
{
return memset(malloc(size), 0, size);
}
Run Code Online (Sandbox Code Playgroud)
这个定义明确size == 0吗?可以malloc()使用零大小调用,但这允许它返回空指针.请问后续调用memset是否正常,或者这是未定义的行为,我需要添加一个条件if (size)?
我非常想避免多余的条件检查!
假设malloc()暂时没有失败.实际上那里也会有一个手动滚动版本malloc(),它会在失败时终止.
像这样的东西:
void * my_malloc(size_t size)
{
void * const p = malloc(size);
if (p || 0 == size) return p;
terminate();
}
Run Code Online (Sandbox Code Playgroud)
我有以下结构:
typedef struct _chess {
int **array;
int size;
struct _chess *parent;
} chess;
Run Code Online (Sandbox Code Playgroud)
我有:
typedef struct _chess *Chess;
Run Code Online (Sandbox Code Playgroud)
现在,我想创建一个动态长度数组来存储指向ARCH结构的指针,所以我执行以下操作:
Chess array [] = malloc(size * sizeof(Chess));
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:初始化程序无效.
如果我放弃[]并执行此操作:
Chess array = malloc(size * sizeof(Chess));
Run Code Online (Sandbox Code Playgroud)
它编译没有错误,但当我尝试通过执行以下操作将此数组的元素设置为NULL:
array[i]=NULL;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:从类型'void*'分配类型'struct _chess'时出现不兼容的类型
知道我做错了什么吗?谢谢.
我创建了一个旨在获取用户输入的函数.它要求将内存分配给保存用户输入的变量; 但是,该函数在函数结束时返回.释放已分配内存/返回变量值的正确方法是什么?
这是代码:
char *input = malloc(MAX_SIZE*sizeof(char*));
int i = 0;
char c;
while((c = getchar()) != '\n' && c != EOF) {
input[i++] = c;
}
return input;
Run Code Online (Sandbox Code Playgroud)
我应该返回输入地址并在使用后将其释放吗?
对于释放输入变量的最合适方法感到好奇.
如果我在我的程序中使用动态分配内存malloc()但在程序运行时没有释放内存,那么在程序终止后是否会释放动态分配的内存?
或者如果它没有被释放,并且我一遍又一遍地执行相同的程序,它每次都会分配不同的内存块吗?如果是这样的话,我应该如何释放这段记忆?
注意:我能想到的一个答案是重新启动我正在执行程序的机器.但是,如果我在远程计算机上执行程序并且重启不是一个选项?
我试图说服(引用C99标准的具体部分)一位同事,以下是未定义的行为:
int *p = malloc(1);
p[0] = 0;
Run Code Online (Sandbox Code Playgroud)
但我无法在标准中找到明确确保未定义的具体部分.我正在寻找标准中从这些线到结论的逻辑步骤:未定义的行为.它是从第一行转换void *到int *第一行吗?第二行的作业?
我能找到的唯一相关部分malloc是它返回一个适当对齐的指针(7.20.3):
如果分配成功,则返回指针,以便将其分配给指向任何类型对象的指针,然后用于在分配的空间中访问此类对象或此类对象的数组(...)
我试着在标准中寻找空间,但是由于空白和其他词汇问题,噪音太大了.