16

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?

5 Answers 5

19

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.

4
  • 1
    The best thing about passing the array to the function is that it avoids coupling allocation and you inner logic (this is important since C has manual memory management and multiple ways to allocate an array)
    – hugomg
    Commented Nov 29, 2011 at 13:08
  • 1
    i understand your point, and also can see this idiom in codes. but the problem in my scenario is that, before calling the function, i would not know in advance the amount of memory to allocate and the value of buflen to pass in.
    – Pie Faced
    Commented Nov 29, 2011 at 13:10
  • In this answer, the function is bound to a maximum length to operate on. You cannot circumvent this problem unless you use dynamic allocation. Commented Nov 29, 2011 at 13:13
  • the calling code can dynamically allocate and pass in with length. Commented Nov 29, 2011 at 13:16
5

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
0
2

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.

1

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

0

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;
}
2
  • 2
    That is not in any sense the syntactic working equivalent of the Java function. The String in the Java function is not a static object and the Java function can be called multiple times without stomping on the value.
    – JeremyP
    Commented Nov 29, 2011 at 13:16
  • @JeremyP - I partially agree: This is not semantically equivalent.
    – mouviciel
    Commented Nov 29, 2011 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.