Static guidelines



  • In my department, everything is in C, functionnally complex (aka spaghetti code) but technically simple enough. 

    One of the first things I discovered in the coding guidelines was that "usage of global &
    static variables should be removed if not absolutely necessary" for "performance and maintenance reasons".

    Yesterday, I was delivered the following file.c:

     

    #include "SomeStructure.h"

    static SomeStructure * pStruct;

    int init(int Size){

        pStruct->Ptr = malloc(Size);

        pStruct->CurrentSize = size;

     }

    int freeStruct(){

        free(pStruct->Ptr);

        pStruct()->CurrentSize = 0;

    }

     /* plus some service using Struc which I didn't nother to read */

      

    I'm starting to think  "performance and maintenance reasons" means half the developers we have are too clueless to properly use static allocation.


  • Discourse touched me in a no-no place

    @Reauiem said:

    In my department, everything is in C, functionnally complex (aka spaghetti code) but technically simple enough. 

    One of the first things I discovered in the coding guidelines was that "usage of global &
    static variables should be removed if not absolutely necessary" for "performance and maintenance reasons".

    Yesterday, I was delivered the following file.c:

     

    #include "SomeStructure.h"

    static SomeStructure * pStruct;

    int init(int Size){

        pStruct->Ptr = malloc(Size);

        pStruct->CurrentSize = size;

     }

    int freeStruct(){

        free(pStruct->Ptr);

        pStruct()->CurrentSize = 0;

    }

     /* plus some service using Struc which I didn't nother to read */

      

    I'm starting to think  "performance and maintenance reasons" means half the developers we have are too clueless to properly use static allocation.

    Your guideline against static variables is ambiguous. Does it mean static method variables or file-scope statics? If the latter, what's wrong with those?

    Not seeing a WTF yet.



  • I'd like to see where the SomeStructure to which pStruct points is being allocated (if ever).



  • @Zecc said:

    I'd like to see where the SomeStructure to which pStruct points is being allocated (if ever).

     

     

    So do I...

     



  •  Does this structure end with an array?



  •  The structure is the following:

     typedef struct SomeStructure {

    AnotherStruct * Ptr;

    int size;

    } SomeStructure

     

    Ptr usually points to the begining of dynamically allocated array of AnotherStruct.

     


  • Discourse touched me in a no-no place

    Oh well, at least it is probably relatively easy to convert to using a thread-local...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.