gpt4 book ai didi

c - 在第一个函数中传递指向多个函数和内存分配的指针

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:03 25 4
gpt4 key购买 nike

我的问题是在 C 编程而不是 C++ 的上下文中!我试图在多个函数之间传递一个指针。但是,内存分配不应该由调用者完成。我尝试了一个小例子来模拟这个。可以看出,当指针指向 main 函数中定义的结构变量时,它按预期“工作”。那就是当传递地址的值时,我的函数可以操纵该内存地址中的值。但是,当函数调用返回并且控制权传递给 main 时,为什么指针会“重新初始化”?指针能否以某种方式反射(reflect)它指向的地址?

如何做到这一点?

这是我的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//example to pass a struct to void pointer and a void pointer to struct
//testing if memory allocation is done by callee and not caller

typedef struct mystructure{
int a;
char b;
unsigned char c[10];
}mystruct;


void func(void *var){

mystruct *s = var;
s->a = 100;
s->b = 'I';
strncpy(s->c,"test",5);
printf("In func\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}

void voidOut(void *var){

mystruct *s = var;
printf("In voidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);

}

//here is void pointer is both and 'in' and 'out' parameter
void memfunc(void *var){

mystruct *s = var;
s = (mystruct *)malloc(sizeof(mystruct));
s->a = 100;
s->b = 'I';
printf("In memfunc\n");
strncpy(s->c,"test",5);
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}

//here is void pointer is an 'in' parameter
void memvoidOut(void *var){

mystruct *s = var;
printf("In memvoidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}


int main(int argc, char *argv[]){
mystruct val;
func(&val);
voidOut(&val);

mystruct *ptr = NULL;
memfunc(ptr);
memvoidOut(ptr);

return 0;
}

更新:在回答和评论之后,这是我所拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//example to pass a struct to void pointer and a void pointer to struct
//testing if allocation is done by callee and not caller

typedef struct mystructure{
int a;
char b;
unsigned char c[10];
}mystruct;


void func(void *var){

mystruct *s = var;
s->a = 100;
s->b = 'I';
strncpy(s->c,"test",5);
printf("In func\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}

void voidOut(void *var){

mystruct *s = var;
printf("In voidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);

}


//here is void pointer is both and 'in' and 'out' parameter
void memfunc(void **var){

mystruct *s = var;
s = (mystruct *)malloc(sizeof(mystruct));
s->a = 100;
s->b = 'I';
printf("In memfunc\n");
strncpy(s->c,"test",5);
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
//memcpy(var,s, sizeof(s));
}

//here is void pointer is an 'in' parameter
void memvoidOut(void **var){

mystruct *s = var;
printf("In memvoidOut\n");
printf("s->a = %d\n",s->a);
printf("s->b = %c\n",s->b);
printf("s->c = %s\n",s->c);
}


int main(int argc, char *argv[]){
mystruct val;
func(&val);
voidOut(&val);

mystruct *ptr = NULL;
memfunc(&ptr);
memvoidOut(&ptr);

return 0;
}

但是我的输出是:

In func
s->a = 100
s->b = I
s->c = test
In voidOut
s->a = 100
s->b = I
s->c = test
In memfunc
s->a = 100
s->b = I
s->c = test
In memvoidOut
s->a = 0
s->b = d
s->c =

我错过了什么?我应该为 memvoidOut 中的结构定义内存吗?

最佳答案

您应该更改分配新内存的函数的签名:

void memfunc(void *var)

void memfunc(void **var)

从main调用

memfunc(&ptr)

解释:

你的 memfunc 函数,复制传递的指针 ptr。最初,它将 this 指针分配给分配的空间(通过 malloc),但随后,在函数返回后,该指针仍然是传递给 memfunc 的原始指针(即 NULL),因为该 ptr 指针的副本已被回收。

现在如果你按照建议改变它,指针的地址将被传递,因此你不会面临“复制指针”的问题

关于c - 在第一个函数中传递指向多个函数和内存分配的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34920143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com