← Back to context

Comment by Borg3

3 months ago

What different behaviour you mean? static in function means that this is just preallocated somewhere in data, not on stack nor heap. Thats it. Yes, its not thread safe so should never be used in libraries for any buffering.

But in program, its not bad. If I ever need multiple calls to it in same thread:

  static char buf[8][32];
  static int z;
  char *p=buf[z];
  z=(z+1)&7;

Works pretty well ;)

> What different behaviour you mean?

Static foremost means that the value is preserved from the last function invocation. This is very different behaviour, than an automatically allocated variable. So calling a function with a static variable isn't idempotent, even when all global variables are the same.

> If I ever need multiple calls to it in same thread:

What is this code supposed to do???? It hands out a different pointer, the first 8 times, than starts from the beginning again? I don't see what this is useful for!

  • If you want to return some precalculated stuff w/o using malloc() free(). So you just have 8 preallocated buffers and you rotate them between calls. Of course you need to be aware that results have short lifetime.

    • That sounds like a maintenance nightmare to me. If you insist on static, I would at least only use one buffer, to make it predictable, but personally I would just let the caller pass a pointer, where I can put the data.

      What application is that for? Embedded, GUI program, server, ...?

      1 reply →