将指针交给C中的另一个函数
我的代码有问题。当我执行“PrepareEncryption”(在示例中)时,代码返回一个有效指针,但当我将其传递到“EncryptBig”时,它不再有效(指向随机数)。我的最佳选择是删除原始结构。如果这是问题所在,我该如何保存它呢?顺便说一句,我知道有内存泄漏将指针交给C中的另一个函数,c,function,pointers,struct,C,Function,Pointers,Struct,我的代码有问题。当我执行“PrepareEncryption”(在示例中)时,代码返回一个有效指针,但当我将其传递到“EncryptBig”时,它不再有效(指向随机数)。我的最佳选择是删除原始结构。如果这是问题所在,我该如何保存它呢?顺便说一句,我知道有内存泄漏 struct filecrypt { FILE* bestand; FILE* nieuwbstnd; unsigned int positie; unsigned int size; unsig
struct filecrypt
{
FILE* bestand;
FILE* nieuwbstnd;
unsigned int positie;
unsigned int size;
unsigned int huidig;
float procentum;
};
struct filecrypt *PrepareEncryption(char* locatie)
{
struct stat file_status;
struct filecrypt origineel, *mirror;
int error;
char* nieuw;
if (stat(locatie, &file_status) != 0)
return NULL;
error = fopen_s(&origineel.bestand, locatie, "rb");
if (error != 0)
return NULL;
error = strlen(locatie)+5;
nieuw = (char*)malloc(error);
if (nieuw == NULL)
return NULL;
strcpy_s(nieuw, error-3, locatie);
strcat_s(nieuw, error, ".cpt");
error = fopen_s(&origineel.nieuwbstnd, nieuw, "wb+");
if (error != 0)
return NULL;
origineel.huidig = 0;
origineel.positie = 0;
origineel.procentum = 0.0f;
origineel.size = file_status.st_size;
mirror = &origineel;
return mirror;
}
float EncryptBig(struct filecrypt *handle)
{
int i, index = 0;
float calc;
char buf, *bytes = (char*)malloc(10485760); // 10 MB
if (bytes == NULL)
{
handle = NULL;
fcloseall();
return -1.0f;
}
for (i = handle->huidig; i < (handle->huidig+10485760); i++)
{
if (i > handle->size)
break;
fseek(handle->bestand, i, SEEK_SET);
fread_s(&buf, 1, 1, 1, handle->bestand);
__asm
{
mov eax, dword ptr [bytes]
add eax, dword ptr [index]
mov cl, byte ptr [buf]
xor cl, 18
xor cl, 75
not cl
mov byte ptr [eax], cl
mov eax, dword ptr [index]
add eax, 1
mov dword ptr [index], eax
}
}
fwrite(bytes, 1, i, handle->nieuwbstnd);
fseek(handle->nieuwbstnd, i, SEEK_SET);
handle->huidig += i;
calc = (float)handle->huidig;
calc /= (float)handle->size;
calc *= 100.0f;
if (calc == 100.0)
{
// GEHEUGEN LEK!
// MOET NOG BIJGEWERKT WORDEN!
fcloseall();
handle = NULL;
}
return calc;
}
void example(char* path)
{
float progress;
struct filecrypt* handle;
handle = PrepareEncryption(path);
do
{
progress = EncryptBig(handle);
printf_s("%f", progress);
}
while (handle != NULL);
}
struct filecrypt
{
文件*bestand;
文件*nieuwbstnd;
无符号整数正数;
无符号整数大小;
无符号整数;
漂浮原肠;
};
结构文件加密*PrepareEncryption(char*locatie)
{
结构统计文件的状态;
struct filecrypt origineel,*镜像;
整数误差;
查尔*尼乌;
如果(状态(位置和文件状态)!=0)
返回NULL;
错误=fopen_s(&origineel.bestand,位置,“rb”);
如果(错误!=0)
返回NULL;
错误=strlen(locatie)+5;
nieuw=(char*)malloc(错误);
if(nieuw==NULL)
返回NULL;
strcpy_s(nieuw,错误3,位置);
strcat_s(nieuw,错误,.cpt);
错误=fopen_s(&origineel.nieuwbstnd,nieuw,“wb+”);
如果(错误!=0)
返回NULL;
origineel.huidig=0;
origineel.positie=0;
原肠上皮=0.0f;
origineel.size=文件\状态.st\大小;
镜像=&originel;
返回镜;
}
浮点加密大(结构文件加密*句柄)
{
int i,指数=0;
浮钙;
char buf,*字节=(char*)malloc(10485760);//10 MB
如果(字节==NULL)
{
handle=NULL;
fcloseall();
返回-1.0f;
}
对于(i=handle->huidig;i<(handle->huidig+10485760);i++)
{
如果(i>句柄->大小)
打破
fseek(handle->bestand,i,SEEK\u SET);
fread_s(&buf,1,1,1,handle->bestand);
__asm
{
mov eax,dword ptr[字节]
添加eax、dword ptr[索引]
mov cl,字节ptr[buf]
xor cl,18
xor cl,75
不是cl
mov字节ptr[eax],cl
mov eax,dword ptr[索引]
添加eax,1
mov dword ptr[索引],eax
}
}
fwrite(字节,1,i,句柄->nieuwbstnd);
fseek(handle->nieuwbstnd,i,SEEK\u SET);
handle->huidig+=i;
计算=(浮动)句柄->回挖;
计算/=(浮动)句柄->大小;
计算*=100.0f;
如果(计算=100.0)
{
//革黑根列!
//莫特·诺格·比杰维克特·沃登!
fcloseall();
handle=NULL;
}
返回计算;
}
无效示例(字符*路径)
{
浮动进度;
struct filecrypt*句柄;
handle=PrepareEncryption(路径);
做
{
进程=大(句柄);
printf_s(“%f”,进度);
}
while(handle!=NULL);
}
这是因为您返回了指向局部变量的指针
局部变量存储在堆栈上,当一个函数返回时,堆栈的该区域将被其他函数重用,并且留下一个指针,该指针现在指向未使用的内存或其他对象占用的内存。这是未定义的行为,有时可能有效,有时可能会给您“垃圾”数据,有时可能会崩溃。这是因为您返回了指向局部变量的指针
局部变量存储在堆栈上,当一个函数返回时,堆栈的该区域将被其他函数重用,并且留下一个指针,该指针现在指向未使用的内存或其他对象占用的内存。这是未定义的行为,有时可能有效,有时可能会给您“垃圾”数据,有时可能会崩溃。在PrepareEncryption中,您将返回指向堆栈(本地对象)上分配的结构filecrypt Origenel的指针。这就是问题所在。函数返回(结束执行)后,
origineel
占用的内存立即失效。您需要通过调用malloc在堆上分配它。在PrepareEncryption中,您将返回指向struct filecrypt origineel的指针,该指针是在堆栈(本地对象)上分配的。这就是问题所在。函数返回(结束执行)后,origineel
占用的内存立即失效。您需要通过调用malloc在堆上分配它。您可以:
mirror = &origineel;
return mirror;
origineel
是一个局部变量。上述内容相当于:
return &origineel;
…返回一个指向局部变量的指针,该变量在函数末尾超出范围。它有时会返回一个有效的指针,这只是偶然的
使用malloc
,或者最好将指向目标位置的指针地址作为参数传递给函数,但不要返回:
int *PrepareEncryption(struct filecrypt *origineel, char* locatie);
struct filecrypt myStruct;
PrepareEncryption(&myStruct, "abc");
你可以:
mirror = &origineel;
return mirror;
origineel
是一个局部变量。上述内容相当于:
return &origineel;
…返回一个指向局部变量的指针,该变量在函数末尾超出范围。它有时会返回一个有效的指针,这只是偶然的
使用malloc
,或者最好将指向目标位置的指针地址作为参数传递给函数,但不要返回:
int *PrepareEncryption(struct filecrypt *origineel, char* locatie);
struct filecrypt myStruct;
PrepareEncryption(&myStruct, "abc");