首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将指针移交给C中的另一个函数

将指针移交给C中的另一个函数
EN

Stack Overflow用户
提问于 2012-11-23 23:28:03
回答 3查看 102关注 0票数 0

我在使用这个代码时遇到了问题。当我执行"PrepareEncryption“(在示例中)时,代码返回一个有效的指针,但当我将其传递给"EncryptBig”时,它不再有效(指向随机数)。我最好的选择是删除原始结构。那么,如果这是问题所在,我该如何保护它呢?我知道这里有一个内存泄漏。

代码语言:javascript
复制
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);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-23 23:32:12

这是因为你返回了一个指向局部变量的指针。

局部变量存储在堆栈上,当一个函数返回时,堆栈的该区域被其他函数重用,您将得到一个指针,该指针现在指向未使用的内存或当前被其他函数占用的内存。这是未定义的行为,有时可能会起作用,有时可能会给你“垃圾”数据,有时可能会崩溃。

票数 4
EN

Stack Overflow用户

发布于 2012-11-23 23:32:46

在PrepareEncryption中,您将返回指向堆栈(本地对象)上分配的struct filecrypt origineel的指针。这就是问题所在。就在函数返回(结束执行)之后,origineel占用的内存变得无效。您需要通过调用malloc在堆上分配它。

票数 0
EN

Stack Overflow用户

发布于 2012-11-23 23:34:00

您需要:

代码语言:javascript
复制
mirror = &origineel;
return mirror;

origineel是一个局部变量。上面的代码相当于:

代码语言:javascript
复制
return &origineel;

...返回一个指向局部变量的指针,该指针在函数结束时超出了作用域。它有时似乎会返回一个有效的指针,这只是偶然的。

使用malloc,或者更好的方法是,将指针地址作为参数传递到目标位置,并且不要返回它:

代码语言:javascript
复制
int *PrepareEncryption(struct filecrypt *origineel, char* locatie);

struct filecrypt myStruct;
PrepareEncryption(&myStruct, "abc");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13531933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档