Thread: Static array is allocated in undefined area

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    30

    Static array is allocated in undefined area

    Hi all.

    In the following code the array 'splitbuf' is allocated outside any of the output sections defined by my linker command file.

    Code:
    #define QMFB_SPLITBUFSIZE_BATCH (8192/JPC_BATCH_SIZE) .
    
    typedef struct {
    	jpc_fix_t vec[JPC_BATCH_SIZE]; 
    } jpc_batch_t;
    
    static void jpc_qmfb1d_split_batch(jpc_fix_t *startptr, int startind, int endind,
      register int step, jpc_fix_t *lstartptr, int lstartind, int lendind,
      jpc_fix_t *hstartptr, int hstartind, int hendind)
    {
    	jpc_batch_t splitbuf[QMFB_SPLITBUFSIZE_BATCH];
                    .....
                    .....
    I should mean that I have set the stack size sufficiently large, so how come it is not allocated there?

    Regards,
    Esben.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How come it's not allocated where? It is a local variable. Provided your array isn't some stupidly huge size, it's allocated on the stack when the function is called. It's destroyed when the function ends. The static keyword as you use it, is only making a static function. Static functions are just like non-static functions, except their scope of visibility is limited to the file they're declared in. It doesn't mean that all of the variables in the function are static.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    30
    >How come it's not allocated where?
    The stack...

    Btw, the array size is rather large, but I have allocated around 20 KB of stack, so I guess it should fit...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so how come it is not allocated there?
    Because local variables are allocated on the stack at run-time, not link time.

    At best, all the linker knows is that there is a block of memory called .stack or something, and that is where the startup routine will set the initial stack pointer to point to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by esben
    >How come it's not allocated where?
    The stack...
    Quote Originally Posted by quzah
    It is a local variable. [snip]...[/snip] it's allocated on the stack when the function is called. It's destroyed when the function ends.
    See what happens when you actually read?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment operator for static array inside a class
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 07-27-2008, 11:11 AM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM