Re: static declarations in structs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 
Compiler checks that if you are defining an eleemnt of a structure, taht can not be static as, then you will be able to bring a situation in which the few of the elements are static and the whole structure is non-static. As Static variables are defined on Data Segment rather than stack segment and local (non-static) variable are declared on stack segment,  so you can not have a situation where few of the elements of structure are on datasegment (as they have been defined as Static) and the other elements on stact segment (becos they are non-static).
 
Compiler checks that no element of structure should be defined as static.
 
For your second thing, as I have already explained, static and global variable are declared on data segment and local variable are stored on stack segment.
You can access the static variable defined in other function, by pointers, because they are alive once the control comes out of the function they are defined in. So thethings are there, but compiler puts a restriction not to access them directly by c syntax, you can definitely access them thru pointers.
 
Regards,
Gaurav.


Anandraj <anandrajm@xxxxxxxxxxx> wrote:
Hi Jim,
Thanks for the rply
I do know that it does not make any sense ,
but i jus want to know the technical explaination Why and how the
compiler throws an Error!

> > 2.How does the complier diffrenciate between a Global Variable and
> > Static Variable as both of them will be in the same stack?
>
> Neither are on a stack. The difference is scope. The statics are local
> to either a file or a function (depending on where that are defined).

IIRC , i guess both the global and static variables have their memory
allocated from Stack ??!!!
Plz correct me if im Wrong !
TIA
Anand




On Wed, 18 Aug 2004 08:54:57 -0400, "Jim Bauer" said:
> On Wednesday 18 August 2004 04:05, Anandraj wrote:
> > Hi guys,
> > A little off topic from kernels,
> > I have a few doubts in C.
> > 1. Why static declartions are not allowed inside structs?
> > eg
> > struct a {
> > static int i;
> > };
> >
> > Throws an error ..
>
> Because it doesn't make any sense. What would you expect it to do?
>
> > 2.How does the complier diffrenciate between a Global Variable and
> > Static Variable as both of them will be in the same stack?
>
> Neither are on a stack. The difference is scope. The statics are local
> to either a file or a function (depending on where that are defined).
>
> > Note: Plz put a CC as i am not able 2 receive mail from the list!
>

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/


Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux