Thread: Will 'static' optimise my code?

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    Will 'static' optimise my code?

    Code:
    void samRing::getParameterDisplay (long index, char *text)
    {
    	static char waveName [4] [4] = { "Sin", "Tri", "Squ", "Saw" };
    
    	switch (index)
    	{
    		case pFreq:
    			sprintf (text, "%d", (int) (freq * MAX_FREQ));
    		break;
    
    		case pWaveType:
    			strcpy (text, waveName [waveType]);
    		break;
    
    		case pDry:
    			sprintf (text, "%d", (int) (dry * 100));
    		break;
    	}
    }
    I'm presuming that because the array is static, the assignments will only occur once. In all subsequent calls, this will be skipped over. Am I right?

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Yes, after waveName is initialized the first time the function is called, subsequent calls won't reassign it. But any speed gain will probably be negligible.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Make it a constant (const char WaveName[] ...) and the compiler *might* optimize it even further.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I wasn't hoping to make great gains in speed, I just didn't like the thought of that array being re-assigned every call - it's just sloppy. But thanks for the const idea, I'll do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a static library from C++ and C code
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 07:28 PM
  2. tools for static code analysis
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2009, 12:41 PM
  3. error with Static Library code
    By ghostshadow189 in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2007, 03:12 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM