[C question] How should I release the dynamic allocation in this situation?

Asked 2 years ago, Updated 2 years ago, 142 views

#include <stdio.h>
#include <stdlib.h>

char *Mystrrev(char *string) {
    int nLength = 0;
    char *Mystr = NULL;

    for(int i = 0; string[i] != '\0'; i++) nLength++;

    Mystr = (char*)calloc(nLength + 1, sizeof(char));
    if(Mystr != NULL) {
        for (int i = 0; i < nLength; i++)
            Mystr[i] = string[nLength - 1 - i];

        Mystr[nLength] = '\0';
    }

    return Mystr;
}

int main(void) {
    char* pastr[3] = { "Hello", "World", "String"};
    char* *ppastr = pastr;

    puts(ppastr[2]);
    puts(Mystrrev(ppastr[2]));

    return 0;
}

Hello! I made this short chords while studying.

I learned that if I make a dynamic allocation, I must release it, but in this situation, where (at which timing) should I release it?

If you do it before returning in Mystrrev(), there will be a problem with the return value, but is it too far to do it in the main function... Hmm...

It's going to be easy Please tell me even if it's short TT!!

c calloc dynamic-allocation

2022-09-20 20:07

1 Answers

For your information, as far as I know, the strrev function is not a standard function. It's a function that was used a lot in the Dos period, and now you can just think of it as a name. In Windows, you have to use the _strrev function, and if it's gcc, it's probably deprecated.

According to the code now, it's right to do it with the main function.

If you want to do it in a different format, you need to replace the prototype of the function with a different format.

Alternatively, if the memory space indicated by the parameter char *string is an array or dynamically assigned space rather than a string constant, you can change the value there. In this case, dynamic assignment and release are not required inside the function, but the original changes. The _strev function in Windows is applicable in this case. So instead of using the _strrev function immediately, we replicate the original string to another space, and then we reverse the replicated string.

Refer to the code below to make it work like a _strrev function.

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

char* _Mystrrev(char* string) {
    int len = strlen(string);

    char* temp = (char*)malloc(sizeof(char) * (len + 1));
    if (temp != NULL)
    {
        for (int i = 0; i < len; i++)
            *(temp + i) = *(string + len - 1 - i);

        *(temp + len) = '\0';

        for (int i = 0; i < len; i++)
        {
            *(string + i) = *(temp + i);
        }

        free(temp);
    }

    return string;
}

int main(void) {
    char str_hello[] = "Hello";
    char str_hi[] = "hi";

    puts(str_hello);
    puts(_Mystrrev(str_hello));
    puts(str_hello);

    char* p = (char*)malloc(sizeof(str_hi));
    if (p != NULL)
        memcpy(p, str_hi, sizeof(str_hi));

    puts(str_hi);
    puts(_Mystrrev(p));
    puts(str_hi);

    free(p);
    p = NULL;

    return 0;
}


2022-09-20 20:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.