简体   繁体   English

realloc 在第二次调用时失败

[英]realloc failing on second call

I am trying to add a bunch of WCHARs to a buffer.我正在尝试将一堆 WCHAR 添加到缓冲区中。 This function is what adds it to my buffer..这个 function 就是将它添加到我的缓冲区中的原因。

DWORD add_to_buffer(BYTE *databuffer, WCHAR *path, WCHAR *value_name, DWORD type, BYTE *data, DWORD data_size, DWORD already_added) {
    DWORD path_size = wcslen(path) * 2;
    DWORD value_name_size = wcslen(value_name) * 2;

    WCHAR *type_name = reg_type_to_wchar(type);
    DWORD type_size = wcslen(type_name) * 2;


    DWORD total_length = already_added + path_size + value_name_size + type_size + data_size;

    *databuffer = realloc(databuffer, total_length);
    
    CopyMemory(databuffer, path, path_size);
    CopyMemory(databuffer + path_size, value_name, value_name_size);
    CopyMemory(databuffer + path_size + value_name_size, type_name, type_size);
    CopyMemory(databuffer + path_size + value_name_size + type_size, data, data_size);
        
    return total_length;
}

On the second call to the add_to_buffer() the realloc() fails.在第二次调用add_to_buffer()时 realloc() 失败。 I am basically calling this function over and over, while adding information to it and making it bigger as needed.我基本上一遍又一遍地调用这个 function,同时向它添加信息并根据需要使其更大。 I am not sure how to troubleshoot this issue as everything in VS looks correct when going into the function.我不确定如何解决此问题,因为进入 function 时 VS 中的所有内容看起来都是正确的。 databuffer is not getting freed anywhere outside of this function.在此 function 之外的任何地方都不会释放数据缓冲区。

You are mixing up levels of pointer indirection.您正在混淆指针间接的级别。 For example, in your line, *databuffer = realloc(databuffer, total_length);例如,在您的行中, *databuffer = realloc(databuffer, total_length); your are (on the left-hand side) dereferencing the databuffer variable but, inside the call, you are not.您(在左侧)取消引用databuffer变量,但在调用内部,您不是。

If you want your function to modify the pointer (which it does), then you need to pass a pointer to that pointer, so that the modified value (new address) is available to the calling module.如果您希望 function修改指针(它会这样做),那么您需要将指针传递给该指针,以便调用模块可以使用修改后的值(新地址)。 Like this:像这样:

DWORD add_to_buffer(BYTE **databuffer, WCHAR *path, WCHAR *value_name, DWORD type, BYTE
 *data, DWORD data_size, DWORD already_added) { // Pass a "databuffer" as a DOUBLE pointer
    DWORD path_size = wcslen(path) * 2;
    DWORD value_name_size = wcslen(value_name) * 2;

    WCHAR *type_name = reg_type_to_wchar(type);
    DWORD type_size = wcslen(type_name) * 2;

    DWORD total_length = already_added + path_size + value_name_size + type_size + data_size;

    // It is also bad practice to overwrite the argument in "realloc" calls; save to a
    // temp, so that you can check for failure ...
    BYTE *temp = realloc(*databuffer, total_length); // Not the "*" before databuffer!
    if (temp == NULL) { // Allocation failure ...
        // Here, place code to handle/signal the error
        // But note that we STILL HAVE THE ORIGINAL POINTER!
        return 0; // and return a value that indicates failure
    }
    *databuffer = temp; // Succeeded: we can now safely reassign the passed pointer.
    
    // We now need to also dereference the double pointer in the following calls ...
    CopyMemory(*databuffer, path, path_size);
    CopyMemory(*databuffer + path_size, value_name, value_name_size);
    CopyMemory(*databuffer + path_size + value_name_size, type_name, type_size);
    CopyMemory(*databuffer + path_size + value_name_size + type_size, data, data_size);
        
    return total_length;
}
*databuffer = realloc(databuffer, total_length);

Here you assign value returned by realloc to first byte of databuffer.在这里,您将 realloc 返回的值分配给数据缓冲区的第一个字节。 You shouldn't dereference databuffer.您不应该取消引用数据缓冲区。

realloc return value: "This function returns a pointer to the newly allocated memory, or NULL if the request fails." realloc 返回值: “如果请求失败,此 function 返回指向新分配的 memory 或 NULL 的指针。” On the 2nd call, there should be no more available space to realloc... i think the easiest way is to change it as: *databuffer = realloc(databuffer, total_length);在第二次调用中,应该没有更多可用空间来重新分配...我认为最简单的方法是将其更改为: *databuffer = realloc(databuffer, total_length); if(databuffer == NULL) { *databuffer = malloc(total_length); if(databuffer == NULL) { *databuffer = malloc(total_length); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM