1

I have a function that works, but I would like to know why static char out[0]; does not produce a warning when it needs to allocate statically memory in scope? What is the correct value for the size of out in this example?:

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

char *hex(char *s)
{
    int i, l = (int)strlen(s);
    static char out[0]; // should it be 7 ?
    for(i = 0; i < l; i++) {
        s[i] -= 5;
        sprintf(&out[i*6], "0x%02x, ", (unsigned char)s[i]);
    }
    return out;
}

int main(void)
{
    char s[] = "hello";
    printf("%s", hex(s)); // 0xa8, 0xa5, 0xac, 0xac, 0xaf, 
    return 0;
}

3 Answers 3

3

No C compiler I know of gives warnings for out-of-bounds array accesses. You're on your own for maxing sure the array index is in bounds.

Sign up to request clarification or add additional context in comments.

Comments

3

I think you have to use dynamic allocation with malloc :

char *out = malloc(6 * strlen(s) + 1); // 6 = strlen("0xXX, ")

And don't forget to free "out" after you used it

2 Comments

Just to be clear, you'll have to add 1 to the size you're allocating because C strings have to be null-terminated: en.wikipedia.org/wiki/Null-terminated_string
Using malloc + 1 for the \0 is another option i could use. i would like to try another way with statically memory to see what happends. It's a good tip, thank you.
1

why static char out[0]; does not throws warning

This is undefined behavior to specify a zero sized array and the compiler is not obligated to produce a diagnostic in such cases. If we look at the C99 draft standard section 6.7.5.2 Array declarators paragraph 1 says(emphasis mine):

[..]If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero.[...]

Although gcc will warn you in this case if you use the -pedantic flag, I receive the following warning:

warning: ISO C forbids zero-size array ‘out’ [-pedantic]

If it also undefined to access an array out of bounds and the same applies here about warnings.

if we look at the definition of undefined behavior in section 3.4.3 in paragraph 2 says(emphasis mine):

NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

Using a static variable for the output is a problematic design, it means that every caller to that method will share the same output. A better choice would be to use malloc to dynamically allocate memory which means you do have to remember to free the memory after you are done.

5 Comments

Zero sized array is not why this does not throw warning. ideone.com/ejmIdU
Ok malloc is god, i have an answer for the first question, thank you. To the 2nd question i should not use zero, so what is the correct value would it be? I intend to use static char in this case for my understanding.
@HansZ There are multiple issues, at this point I think I have addressed all of them. If you have more specific issues let me know.
@pipe3r You would have to determine the maximum space needed, which does not seem possible in this case unless you put a maximum constraint on your function.
Fun fact, I fired up my old copy of visual studio 2008 and char out[0]; IS a compiler error in vs. Ideone uses gcc, and I just verified with gcc on my own machine. I'm gonna go home and try it out with armcc and see what we get. (also, yes, just always always use malloc or new, if you're doing something so complicated that you can't use either, you're doing something wrong).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.