Question

i'm quite familiar with Java, not so in C.

In Java, if i have a method that does something and returns a String, it would look like:

private String doSomething(...) {
    String s;
    // do something;
    return s;
}

The syntactic equivalent in C would not work and is plain wrong:

char* doSomething(...) {
    char s[100];
    // do something;
    return s;
}

of course i can do:

char* doSomething(...) {
    char *s;
    s = malloc(100 * sizeof(char));
    // do something;
    return s;
}

which would work (i think!) but i seldom see codes doing this way (is it because it's unnecessarily filling the heap?)

most commonly, i see:

bool doSomething(char *s) {
    // do something that copies chars to s
    return true;
}

And the calling statements would be:

char s[100];
doSomething(s);

What if I do not know the size of the char array until inside the function itself? i.e. I would not be able to declare the char array outside the function and then pass it in.

What would be the correct way to deal with such a scenario?

Was it helpful?

Solution

An example of static allocation case (you are obliged to think about the maximum buffer size at compile time)

buf[MAX_NO];
do(buf, MAX_NO);

_Bool do(char *s, size_t len) { 
// use len as boundary in your operations
}

or dynamic allocation case (using malloc as you said and using pointers to save the buffer location and size)

char *s = NULL;
size_t final_len;
do(&s, &final_len);

_Bool do(char** s, size_t* final_len) {
    size_t len = discoverSize();
    char* buf = (char*) malloc(sizeof(char) * len);

    *s = buf; //save memory location
    *final_len = len; //save buffer size
    // use len as boundary in your operations

}

// do whatever
free(s); //remember to free the buffer for politeness

OTHER TIPS

Let the calling code be responsible for allocating the memory. Pass in the buffer and the length of the buffer in example 2:

bool doSomething(char *s, size_t buflen)
{ 
    // do something that copies chars to s up to max of buflen
    return true; 
} 

This tends to reduce leaks, since the calling code is in control of the memory management.

For when you cannot know the right size of the returned string, use the malloc solution. Of course you have to free the string, after you are done with it.

You cannot return an array in C.

You can return a pointer and you should learn (by reading a good book on programming in the C language) the difference between arrays and pointers.

A common idiom is to return a dynamically allocated pointer. Then you should have a convention telling who will free the pointer.

You could also return a structure containing an array.

addenda

If you want to have a garbage collector in C, try Boehm's garbage collector

The safest way is to leave the responsibility of allocating to the caller, as said by others.

But, if you don't need reentrant code and simplicity of Java, the syntactic working equivalent in C is:

char* doSomething(...) {
    static char s[100];
    // do something;
    return s;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top