copx said:
Q1:
If an array is declared static in file A is it still valid to access
it from file B?
"Access" is a rather broad term. In general - yes.
I mean if a function form file A which returns a pointer to a
position inside of the array is called from file B can functions
in file B read/write to the array using this pointer?
Yes, of course.
I know that this works and doesn't produce a warning with
every compiler I've used but is this behaviour correct?
Yes.
The question could be stated differently: does "static" really
make the memory area allocated for the array "static"?
Yes, it does make it 'static'. But the only effect of 'static' that is
relevant to an object ("memory area", as you say) is that the storage
duration of this object becomes static, i.e. it exists as long as the
program runs. It has absolutely no effect on the "accessibility" of the
object. If you can "locate" that object form other point in your
program, you can access it without any concerns, regardless of where you
access it from. Returning a pointer to such an object (just like you
described above) is one way to locate it.
Accessibilty-related effects of 'static' do not apply to objects. They
apply to _identifiers_ of objects. An identifier declared as 'static' at
file scope gets internal linkage. This only means that this identifier
is not related in any way to identical identifiers in other translation
units. Note, that strictly speaking, this only changes the properties of
the identifier of the object and has ho effect on the properties of the
object itself. In other words, declaring an object as 'static' prevents
you from locating the corresponding object form other translation units
by using the "traditional" method: providing a declaration of the
identifier and expecting the compiler (and linker) to do the rest.
Q2:
Can I save/restore an array to/from disk like this:
int my_array[100];
fwrite(my_array, sizeof my_array, 1, out_file);
fread(my_array, sizeof my_array, 1, in_file);
Again, it works mighty fine for me but is it
valid/portable?
Well, it is valid, assuming that the level of portability/file format
consistency provided by this method is sufficient for your application.
The answer depends on the lifetime of the file you are trying to write/read.
This file definitely will not be cross-platform portable. I.e. if you
want to be able to save data by using this method on one platform and
then read it on the other (even into the same program compiled for
different platform), then you'll have to use different method of saving
and reading data. This one will not do.
If you don't really care about cross-platform portability, i.e. you are
limited to a single platform, still remember that this method will not
immediately provide any file format consistency between different
versions of your program. Tomorrow you decide to change 'int' to 'long'
and the data is no longer readable.
This method does provide a reasonable level of data consistency between
different sessions of the same program. I.e. if you are planning to save
this file and then expect to read it later into the very same
executable, it should work fine.
And finally, this method has absolutely no problems if the lifetime of
the file is limited to one session of the program (temporary swap file
is one example of such file). Actually, this is how it should be done in
this case.
What are your requirements?