Thread: static array?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    static array?

    hey, there, i am wondering whether a static array is similar to a static variable, which hold value between subsequent calls.
    Code:
    void readdata(){
    double workspace[3600];
    ...
    }
    and I will call the readdata() in a while loop. I am worrying that it is not efficient, since each call will allocate and deallocate the workspace[3600]. So does static array do the job?
    Code:
    void readdata(){
    static double workspace[3600];
    ...
    }
    For reason of clarity, I want to keep the workspace[100] inside readdata() since that's the only place it is used. To pull it out and place it in the caller is not what I want.
    Thanks for your reply.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you make the thing static, it will live for the whole lifetime of the program, and each time the function is called it will have the same values as it did when the previous call ended.

  3. #3
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    If you declared workspace inside the caller, then it would only be allocated once. You could just pass the address of workspace to readdata() & it would have full read & write control over the variable. This way workspace need not be made static & it doesn't matter if workspace is not used in the calling function directly.
    Both code segments would constantly declare workspace each time readdata is called.
    Having it like you want is not going to completely please you. workspace being declared inside readdata() may be more clearer, but is less efficient (esp. logically). Having workspace passed to readdata() will ensure it is only declared once (unless you have another loop).
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 04:03 PM
  5. static array of function pointers within class
    By Yarbles in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2005, 02:10 PM