Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> 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, ...?


it is very predicatable.. Every call it returns buffer, after 8 calls it wraps. I use such stuff in many places.. GUI programs.. daemons.. Most stuff are single threaded. If threads are used, they are really decupled from each other.

Yes, you should never ever use it in Library.. But small utility functions should be okish :)

This is example from my Ruby graph library. GetMouseEvent can be called alot, but I need at most 2 results. Its Ruby, so I can either dynamicaly allocate objects and let GC pickup them later, or just use static stuff here, no GC overhead. it can be called 100s of times per second, so its worth it.

  static GrMouseEvent evs[8];
  static int z=0;
  GrMouseEvent *ev=&evs[z];




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: