Click here to Skip to main content
16,021,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have a nested structure which contains others structure within it, currently when i move from one page to another my previously initialized structure loses its values. by making it static i am not sure whether all the variables of my structures (even those nested structure variables) maintain there state or not, the non-structure variable of my structure do maintain their value when coming back to the same page(same .c file) where they were initialized.

eg:
C#
struct first {
    struct nestedfirst *nf;
    unsigned count;
    };


CSS
struct nestedfirst  {
    unsigned num;
    };


const struct first *f;


if i declare it as

static struct first *f;

will the nestedfirst structure value will remain available between different file(.c) calls, say i initialized it at file1.c and move to file2.c then comeback to file1.cs will i find its value unchanged if not what should i do to make it happen.
Posted
Updated 13-May-15 5:05am
v5
Comments
Sergey Alexandrovich Kryukov 12-May-15 9:29am    
Not clear. What "page"? what looses what value. How about a code sample demonstrating it?
—SA
Andreas Gieriet 12-May-15 9:32am    
What do you mean by "page" - there is no such term in C/C++.
Is it C or C++? They may behave differently depending on the context.
Cheers
Andi
PS: What you show in your question is not a nested struct. You show us a struct with two members: one is an int, the other is a pointer to a pointer to some struct.
Rajesh R. Sharma 12-May-15 10:13am    
Can u provide a code sample ??
[no name] 12-May-15 11:24am    
Only static data is guaranteed to be maintained throughout the application. For example if any pointers in a static structure refer to 'local' variables then those variables cannot be guaranteed if you move from the current function (either up or down). The fact that sometimes the data remains, does not guarantee it.
CPallini 12-May-15 16:05pm    
Usually, when coding, you have to know in advance what your are doing.
What do you mean with 'page'?

You must work with pointers to the top structure. If not the top struct is copyied by value, but all values which are pointers get bogous.

Try to avoid such nesting it isnt good style. Get to a finner and better modularity.
 
Share this answer
 
Comments
RajneeshSaysHello 13-May-15 1:08am    
you mean static pointers or pointers in general, since i have lots of variables inside my structure with many other nested structure.
If the meaning of page for you is file then maybe I understood what your problem is.
You want to access to same structure from different modules. If so you need to define your structure in only one module, than use a reference to 'external' data in all other modules where you want access same structure. Don't use 'static' qualifier or your struct will not be accessible at all from other modules.
See this example:
1. Create an header file where you will define the protos of your structures:
C++
// header file "header.h" where you define structures

typedef struct _nestedfirst    //define inner structure
{
    unsigned num;
} stNESTEDFIRST;

typedef struct _first
{
    struct _nestedfirst *nf;
    unsigned count;
} stFIRST;

extern stFIRST MyStFirstStructure;


2. Include this header in all files where you need access to the structure, but in only one instanciate the structure:
C++
// file1.c
 #include <header.h>

stFIRST MyStFirstStructure;    //in file1.c we create the struct

int foo(void)
{
    MyStFirstStructure.nf->num = 0;
    ....
}


C++
// file2.c
 #include <header.h>

int bar(void)
{
    MyStFirstStructure.nf->num += 2;
    ....
}


Please take into account that while the storage attribute applies to the whole structure, meaning that all variables inside the structure get such attribute, in your case in your structure you have declared a pointer to a struct, not a struct!
So the pointer still is permanent and holds indefinitely the value you have set into it, but the nested structure referred by the pointer have its own storage class depending on what you put in its declaration.
 
Share this answer
 
v5
Comments
[no name] 16-May-15 10:21am    
nf not assigned to new struct. This will blow up ---MyStFirstStructure.nf->num = 0;
Frankie-C 16-May-15 10:48am    
Of course it's not assigned (and a lot of many many other initialization are missing), this is an just an *example* to explain how to make objects visible between modules, it is not a complete program! :-)
The user is not complaining on crashes or memory exceptions on null pointers, so we *must* assume that he knows how to deal with that.
Please concentrate on the question: how to make available same data aggregate between different modules. ;-)
[no name] 16-May-15 19:07pm    
Are we sure that's the question? That's not how the last sentence reads. It seems to be about lifetime and not linkage which is what you are talking about.
I stick to my point about your example. What nf is pointed to is crucial here and to show operations on an unassigned nf could be misleading.
Frankie-C 17-May-15 3:17am    
I think this is really a waste of time. The last sentence is:
"will the nestedfirst structure value will remain available between different file(.c) calls, say i initialized it at file1.c and move to file2.c then comeback to file1.cs will i find its value unchanged if not what should i do to make it happen."
Have you seen "say i initialized it at file1.c ..." we have to suppose that he has initialized everything :-).
What nf is pointed to is something that he has managed, if he put a pointer to a struct he has to update that pointer, if he doesn't do that he will have a memory problem, not different data between modules.
In my opinion he has simply declared the same variable in different modules, that leads to different instances, one for each module, each one with its own data.
We are criticizing our answers while no clarification is coming from user. His poor english (even worst than mine :-)) lets open any interpretation.
Even in your solution the use of 'static' on the pointer declaration can be opinablle if that pointer have to be shared between modules it will be not, or will trigger a compiler error if declared 'extern' also... ;-)
[no name] 17-May-15 3:37am    
There are 2 or 3 possible interpretations - variable lifetime, linkage or both. You answered one I another. I was going to give yours a 5 when I noticed the matter in your answer I pointed out originally. Thats all. It is a correct answer to one interpretation. I did not pick any issue with you over the OP's question. Its not clear. Yes we know.
Can I suggest that before asking this kind of question you should consult documentation and then you should do a test yourself using your debugger. If you don't know how to use the debugger now is the time to learn.

Your use of the word page is confusing as you actually mean file or translation unit http://en.wikipedia.org/wiki/Translation_unit_%28programming%29[^].

Now if we look here:https://msdn.microsoft.com/en-us/library/s1sb61xd.aspx[^]
we learn:
•When you declare a variable or function at file scope (global and/or namespace scope), the static keyword specifies that the variable or function has internal linkage. When you declare a variable, the variable has static duration and the compiler initializes it to 0 unless you specify another value.

Internal linkage means only visible in the file you declare it and static duration means lives for the life of the program.

So yes if you declare it static it will retain its value throughout the life of the program.

You will need something like:

C++
static struct first *f = new first();
first->count = 12;
first->nf = new nestedfirst();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900