簡體   English   中英

函數執行后結構數組中的值丟失

[英]Loss of values in array in struct after function execution

我正在研究一個AC代碼,該代碼包含一個結構,該結構包含一些我稱為range的值。

我的目的是動態使用這個所謂的范圍(每次執行時保存不同數量的數據)。 我現在暫時使用#define comp代替。 通過使用s1結構(和內存分配),每次我調用update_range時,這個所謂的range都會更新。

我發現很奇怪,當我引入“ show_range”函數在更新函數內部/外部輸出實際值時,我意識到我松開了前兩個值。 這是代碼。 有什么建議嗎? 提前致謝!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <complex.h>
#define comp 1024

// struct holding a complex-valued range
struct range {
    int dimensions;         /* number of dimensions */
    int* size;              /* array holding number of points per dimension */
    complex double* values; /* array holding complex valued */
    int components; /* number of components that will change on any execution*/
};

// parameters to use in function
struct s1 {
    int tag;
    struct range* range;
};

int update_range(struct s1* arg);
int show_range(struct range* argrange, char* message);
int copy_range(struct range* in, struct range* out);

int main(void) {
    int ret = 0;
    struct s1 s1;
    s1.tag = 0;
    s1.range = malloc(sizeof(struct range));
    update_range(&s1);
    show_range(s1.range, "s1.range inside main function");

    return ret;
}

////////////////////////////////////////////
int update_range(struct s1* arg) {
    int ret = 0;
    int i;
    struct range range;
    range.dimensions = 1;
    range.size = malloc(range.dimensions * sizeof(int));
    range.components = comp;
    range.size[0] = range.components; // unidimensional case
    range.values = malloc(range.components * sizeof(complex double));
    for (i = 0; i < range.components; i++) {
        range.values[i] = (i + 1) + I * (i + 1);
    }
    show_range(&range, "range inside update_range function");

    arg->range->size =
        malloc(range.dimensions * sizeof(int)); // size was unknown before
    arg->range->values =
        malloc(comp * sizeof(complex double)); // amount of values was unknown
    copy_range(&range, arg->range);
    show_range(arg->range, "arg->range inside update_range function");

    if (range.size)
        free(range.size);
    range.size = NULL;
    if (range.values)
        free(range.values);
    range.values = NULL;
    return ret;
}

////////////////////////////////////////////
// Show parameters (10 first values)
int show_range(struct range* argrange, char* message) {
    int ret = 0;
    vint i;
    printf("   ******************************\n");
    printf("   range in %s \n", message);
    printf("   arg.dimensions=%d \n", argrange->dimensions);
    printf("   arg.size[0]=%d \n", argrange->size[0]);
    printf("   argrange.components=%d \n", argrange->components);
    printf("      first 10 {Re} values: \n");
    for (i = 0; i < 10; i++) {
        printf("   argrange.values[%d]=%f\n", i, creal(argrange->values[i]));
    }
    printf("\n");
    return ret;
}

////////////////////////////////////////////
// copy range
int copy_range(struct range* in, struct range* out) {
    int ret = 0;

    if (in == NULL) {
        fprintf(stderr, "error: in points to NULL (%s:%d)\n", __FILE__,
                __LINE__);
        ret = -1;
        goto cleanup;
    }
    if (out == NULL) {
        fprintf(stderr, "error: out points to NULL (%s:%d)\n", __FILE__,
                __LINE__);
        ret = -1;
        goto cleanup;
    }

    out->dimensions = in->dimensions;
    out->size = in->size;
    out->values = in->values;
    out->components = in->components;

cleanup:
    return ret;
}

您的copy_range函數已損壞,因為它僅復制指向大小和值的指針,而不復制內存。 調用free(range.size); free(range.values); 您也正在從原始對象刪除內存,但沒有將其指針設置回NULL。

調用update_range之后,s1.range的大小和值均具有非NULL指針,但它們指向已刪除的內存。

由於訪問釋放的內存,您遇到未定義的行為(UB)。 您的copy_range()函數僅對兩個指針字段進行淺表復制,因此當您運行free(range->size) ,會使arg->range->size無效。

您應該通過分配和復制指針內容來使copy_range()成為深層副本,例如:

out->size = malloc(in->dimensions * sizeof(int));
memcpy(out->size, in->size, in->dimensions * sizeof(int));

out->values = malloc(in->components * sizeof(complex double));
memcpy(out->values , in->values, in->components * sizeof(complex double));
There are not 10 items to print, so the lines:

printf("      first 10 {Re} values: \n");
for (i = 0; i < 10; i++) {
    printf("   argrange.values[%d]=%f\n", i, creal(argrange->values[i]));
}

Will be printing from random memory.
a much better method would be:

    printf("      first %d {Re} values: \n", min(argrange.components,10));
for (i = 0; i < argrange.components; i++) {
    printf("   argrange.values[%d]=%f\n", i, creal(argrange->values[i]));
}

The above is just one of many problems with the code.  
I would suggest executing the code using a debugger to get the full story.  
as it is, the code has some massive memory leaks due mostly 
to overlaying malloc'd memory pointers.  
for instance as in the following:

arg->range->size =
    malloc(range.dimensions * sizeof(int)); // size was unknown before
arg->range->values =
    malloc(comp * sizeof(complex double)); // amount of values was unknown

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM