question on const declaration 
Author Message
 question on const declaration

           I had a question on using the const qualifier.  I've always assumed
   that if you did (something like) the following:

[...]
   SomeStruct *fun (void)
   {
     const SomeStruct s={1,2,3};
     return &s;
   }

           That the memory space reserved for s in the function fun() would
   be in the global memory region, not on the stack.

No, the keyword to have s put in a global area is `static'.  (You
should know, BTW, that attempting to use the returned pointer results
in undefined behavior.)

                                                      Under HP-UX using gcc, I
   have discovered that s is placed on the stack.  Should I report this as a
   bug or is this conforming to the ansi C standard?

No, if you want that behavior declare it as `static const'.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Sun, 06 May 2001 03:00:00 GMT  
 question on const declaration
[snip -- because of Netscape]

Quote:
>         That the memory space reserved for s in the function fun() would
> be in the global memory region, not on the stack.  Under HP-UX using gcc, I
> have discovered that s is placed on the stack.  Should I report this as a
> bug or is this conforming to the ansi C standard?

>         If it is proper that memory for s is placed on the stack, what about
> performance?  Now every time f() is called, the const s must be initialized,
> even though it is a constant and cannot be changed.  Why is this done?  I
> understand that I can get around it by using "static" but I was curious.

The placement of variables and usage of a stack is up to the compiler writers /
manufacturers.  There is nothing in the standard that requires a stack to be
used.

The "const" qualifier only indicates that something should not be written to.
It does not say where to locate the item.

Some compilers, such as those for embedded systems, have an area that is
read-only.  The compilers put constants in this area.  Some compilers place
constants in the execution (code) space.

Again, this is compiler specific stuff which may vary from vendor to vendor.

--
Thomas Matthews



Sun, 06 May 2001 03:00:00 GMT  
 question on const declaration

Quote:

>        I had a question on using the const qualifier.  I've always assumed
>that if you did (something like) the following:

>typedef struct SomeStruct
>{ int i;
>  int j;
>  int k;
>} SomeStruct;

>SomeStruct *fun (void)
>{
>  const SomeStruct s={1,2,3};
>  return &s;
>}

>        That the memory space reserved for s in the function fun() would
>be in the global memory region, not on the stack.  Under HP-UX using gcc, I
>have discovered that s is placed on the stack.  Should I report this as a
>bug or is this conforming to the ansi C standard?

The C language doesn't define concepts like global memory region or stack.
What it does define is variables with static or automatic storage duration.
Here s is defined at block scope and without thew static storage class
specifier so it has automatic storage duration. That means that s exists
only for the lifetime of the function call, so accessing the return value
of fun() is an error.

Quote:
>        If it is proper that memory for s is placed on the stack, what about
>performance?  Now every time f() is called, the const s must be initialized,
>even though it is a constant and cannot be changed.  Why is this done?  I
>understand that I can get around it by using "static" but I was curious.

To do otherwise would make const affect the storage class which doesn't
make much sense (it isn't a storage calass specifier). You;d just make
the language more complex for no real benefit - as you say if you
want static you can define it as static.  An optimising compiler could
place s in "static" storage if it determined that fun() is never called
recursively (if it is called recursively each instance of the function
defines a separate s object which must all be at different addresses).
However that is a quality of implementation issue and I doubt if many
compilers would do that since the amount of code it would help is minimal.

--
-----------------------------------------


-----------------------------------------



Sun, 06 May 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. How tightly does const bind within declaration?

2. const declarations in ANSI C

3. const convention in func prototypes/declarations

4. CONST after a function declaration

5. extern const declaration

6. const declarations and arrays

7. const declarations and arrays

8. Const on a data declaration

9. const declaration

10. Declaration of static const types

11. const after function declaration?

12. Help: inline char const* const& max(char const* const &a, char const* const &b)

 

 
Powered by phpBB® Forum Software