简体   繁体   中英

After creating an array through dynamic allocation, a problem occurs when changing the memory size through realloc in C

I am practicing C language.

I wanted to use dynamic allocation to use only the size of the string I input as memory and check whether the input string was properly saved.

So, I wrote the following code using malloc and realloc functions.

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


void str_copy(char* str_array_f) {

    void* tmp;
    char buf;
    unsigned char arr_size = 1; 
    unsigned char arr_cur = 0;

    while ((buf = getchar())) {
        if (buf == '\n') {
            break;
        }
        str_array_f[arr_cur++] = (char)buf;

        tmp = realloc(str_array_f, ((arr_size++) * sizeof(char)) + sizeof(char));
        if (tmp != 0) {
            str_array_f = tmp;
        }
        else {
            printf("memory leak error occur! \n");
            break;
        }
    }
    str_array_f[arr_size - 1] = 0x00; 
    
}


void main() {

    int contiune = 1;

    while (contiune) {
        char* str_array = malloc(sizeof(char) + sizeof(char));

        printf("Please type something : ");
        str_copy(str_array);

        printf("'str_array' have this : %s \n", str_array);
        printf("-------------------------------------------------\n");
        if (str_array[0] == '1') {
            contiune = 0;
        }
        free(str_array);
    }

}

And, as a result of the performance, The following problems have occurred.

  1. Strange values sometimes appear from the 5th character of the intermittently printed value (To reproduce this issue, it is recommended to remove the while loop and try repeatedly) 在此处输入图像描述

  2. In the case of repeatedly receiving a value by using the while loop, an error occurs after 4 repetitions. 在此处输入图像描述

  3. If the allocated memory of tmp, which is a void type pointer, is released after line 22(eg, 'free(tmp);'), when executed, no output and an error occurs immediately. 在此处输入图像描述

For the above 3 problems, I am not sure what is the cause and how to fix it.

Please let me know if there is a solution.

And, if there is a bad coding method in my code in terms of efficiency or various aspects, I would appreciate it if you let me know.

* Programming execution environment: Visual studio 2019

to explain what you're doing wrong I'm going to use a minimal example here

void change_x(int x) {
    x = 2;
}

int main() {
    int x = 1;
    change_x(x);
    printf("%i\n", x); // it'll print 1 not 2
    return 0;
}

here the integer x is copied when the function is called and changing it won't really change the x in main. similarly you are doing in your code that str_array_f = tmp; it really won't change the str_array but the copied value. and you're trying to free a pointer that was reallocated before.

the fix for the example above is not to pass the value x instead pass the address of x (which is equivalent to pass by reference in other languages)

void change_x(int* x) {
    *x = 2;
}

int main() {
    int x = 1;
    change_x(&x);
    printf("%i\n", x); // it'll print 1 not 2
    return 0;
}

and for your code

void str_copy(char** str_array_f) {...} // change the parameter
*str_array_f = tmp; // de reference and use it.

str_copy(&str_array); // call with it's address

And one more thing, don't reallocate more often it's not efficient. instead just just allocate your "array" type with a minimum size and when it's filled reallocate it with the size of 2 times of it (or 1.5 if you like)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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