简体   繁体   中英

Char* created using malloc in function, compiler says address is on the stack and cannot be returned

I am writing a function that is supposed to take a string as input and add quotes around the word and then return the pointer to the new modified string.

//add quotes around a single word
char** _add_quotes(char* word){
        int char_count = 0;
        while(word[char_count] != '\0'){
                char_count++;
        }
        char* word_quotes = malloc((char_count+3) * sizeof(*word_quotes));
        word_quotes[0] = '\"';
        for(int i =0; i < char_count; i++){
                word_quotes[i+1] = word[i];
        }
        word_quotes[char_count+1] = '\"';
        word_quotes[char_count + 2] = '\0';
        return (&word_quotes);
}

This is where it returns

char** new_word_w_qs = _add_quotes(new_word); //add quotes
//copy new word with quotes to the final string
for (int m = 0; m < word_len; m++){
new_string[string_index] = *new_word_w_qs[m];
string_index++;
}

I expected it to return the address of the string on the heap, instead I got an error. warning: address of stack memory associated with local variable 'word_quotes' returned [-Wreturn-stack-address] return (&word_quotes); ^~~~~~~~~~~

char f() {
    char a = 'a';
    return &a;
}

The variable a stops existing after the function returns. So after the function returns, the variable a does not exists, the address of the variable &a is invalid after the function returns, there is no memory there after the function returns.

char **f2() {
   char *b = "abc";
   return &b;
}

This is the same. The b variable does not exists after the function, so the address of the b variable is invalid after the function returns. No matter if it's a pointer. The address stored inside b variable is still valid, but the address of variable b is invalid after the function returns.

Just return the pointer by value, not a pointer to the pointer.

//add quotes around a single word
char* _add_quotes(char* word){
        ...
        char* word_quotes = malloc((char_count+3) * sizeof(*word_quotes));
        ...
        // this is the value as returned by malloc()
        // the pointer value returned by malloc still valid after the function returns 
        return word_quotes;
}

And your function could be rewritten to use standard library functions:

char* _add_quotes(char* word){
        char* word_quotes = calloc((strlen(word) + 3), sizeof(*word_quotes));
        if (word_quotes == NULL) return NULL;
        strcat(word_quotes, "\"");
        strcat(word_quotes, word);
        strcat(word_quotes, "\"");
        return word_quotes;
}

or even:

char* _add_quotes(char* word){
        char* word_quotes = calloc((strlen(word) + 3), sizeof(*word_quotes));
        if (word_quotes == NULL) return NULL;
        sprintf(word_quotes, "\"%s\"", word);
        return word_quotes;
}

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