Return'ing char*

This was always a bit tricky for me...
Mostly I want to return dynamically sized c strings. Just initializing char* as a normal c string hast no guarantee for the size of the string, thus may cause an access violation.
Then new'ing strings or malloc'ing them would result in having to free or delete[] them, but since I'm returning them, I actually cannot do so.
Now I've read yesterday that using const as the return value of the function, the program is told that this part within memory cannot be modified by the programmer hence it may be overridden. But is this really true?

Which is the best way to return c strings?

Thank you for any answers.

Sincerely, DarkDragon1993
I'll attempt to give an answer to your original question if you can give a convincing argument as to why you're not using std::string.

hence it may be overridden.

Meaning what?
I prefer doing it this way:

1
2
3
void myFunc( string& result ) {
  // do work here, returning in result
}

no direct return means no additional copy construction
no statics means the function is reentrant (good for multithreading)
no worries about memory since the caller owns the variable
Thought about that once but I've forgotten it again... Thanks for this.

Why not use std::string? I didn't say I'm not using it, but I still would like to know what would be the best way to return c strings, just because I like to know how people before C++ arrived did it. Doesn't mean that I stick to the old fashioned... If that isn't reason enough for you, I can't help it.

What I ment with "hence it may be overridden" is, that I've read that const would define the return value as unchangable - of course - so the system may write different data over this part of memory without causing trouble.
Now I've read yesterday that using const as the return value of the function, the program is told that this part within memory cannot be modified by the programmer hence it may be overridden. But is this really true?
If any variable is declared as const, it's an indication that its value cannot be changed, and the compiler will stop you if you mistakenly attempt to change the value. It's a safety feature designed to catch programmer errors.

A function can return nothing or it can return something. Consider this example from the C standard library:
 
const char* strcpy(char* s1, const char* s2);
That means the function takes a string that can be modified, a string that will not be modified and returns non-modifyable string. That means if your write code like:
1
2
3
4
5
6
7
8
#include <string.h>
int main()
{
    char name[32];
    const char *s = strcpy(name, "Lua");
    strcpy(s, "Ruby"); // can't do this: s is const
    return 0;
}



I like to know how people before C++ arrived did it
You'll have to remember two things.
1. The memory buffer that the string is in is just large enough for the string (includin the null terminator).
2. The user of the string must free the memory.

A standard function that works in this way is strdup().
http://pubs.opengroup.org/onlinepubs/007908799/xsh/strdup.html

An example of a function that concatenates two strings is:
1
2
3
4
5
6
7
8
9
10
11
12
char* mystrcat(const char *s1, const char *s2)
{
    size_t len = strlen(s1) + strlen(s2) + 1;
    char* s = malloc(len);

    if (s)
    {
        strcpy(s, s1);
        strcat(s, s2);
    }
    return s;
}


The user has to "know" that the string must be deleted when done with it.

And it's this "knowing" that makes its use error-prone.
Topic archived. No new replies allowed.