LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2004, 03:53 AM   #1
swifty99uk
LQ Newbie
 
Registered: Oct 2003
Posts: 4

Rep: Reputation: 0
Static Variables in C function


I'm using a static variable in a function in C, so that data will be available when a function is called. The idea is that when this function is called, it will either use data already in an array or add data to it. the declaration I use is:

static SDataFile* struLoadedFiles[INTMaxFiles]

where SDataFile is a predefined struct.

My question is, when I declare this array, what is static (i.e remains in memory for subsequent function calls), is just the pointer or the data that it points to? I ask because in my code, the data seems to dissapear when I later use the function.

Thanks for any help
 
Old 04-14-2004, 04:36 AM   #2
illuminatedwax
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
When you make a static pointer like:
static int *g
it will only retain the pointer, and not necessarily the information that is being pointed to.
It could very easily happen that on a subsequent call to the function, g points to garbage.
It just depends on how your program is set up.

If that solves your problem, great, otherwise post some more of the code you are using.

-iw
 
Old 04-14-2004, 04:49 AM   #3
maiorino
LQ Newbie
 
Registered: Apr 2004
Location: Spain
Distribution: Mandrake
Posts: 11

Rep: Reputation: 0
Exclamation Well, it happens!

In your declaration

static SDataFile* struLoadedFiles[INTMaxFiles];

you're defining a static array of pointers to structures (? where's the struct declaration, in an elsewhere written typedef?) SDataFile, i.e. you're saving the memory location values of the pointers array, and not the values contained in the array.
To be more clear: your function is always accessing the same memory addresses through the SDataFile* pointers, because these (the pointers) values are the ones defined as static, but what is contained in those memory addresses is your business; that is not static stuff.

Depending on your needs, it would be better to declare static an array of structures of type SDataFile or, if you plan to use this data somewhere else in your code, you could even define this array as an extern one (watch it! It depends on your code!)
I know there's so many books telling you what to do and what not to do, what's good style and what's not, but in the end much depends on your own problem.
I believe I should also suggest you to give a look at C++, where this kind of problems can be better handled, but it is mainly at the level of the language way of thinking, and that takes most of the time in the learning of a language.

Marino
 
Old 04-14-2004, 05:41 AM   #4
swifty99uk
LQ Newbie
 
Registered: Oct 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for your help.

That all makes sense, I should use an array instead of a pointer.

I have implemented my code using this static pointer and it works ok, but I guess the data could get corrupted and I've just been Lucky.
 
Old 04-14-2004, 10:50 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by swifty99uk
That all makes sense, I should use an array instead of a pointer.
Or:
Use a pointer but allocate memory to it by calling malloc() or calloc(). This 'kind' of memory will remain until freed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
where does static and global variables stored raviksp General 0 04-28-2005 12:23 AM
accessing variables inside a function bahadur Programming 1 03-27-2005 04:41 AM
what does it mean when a function is static? blackzone Programming 1 09-28-2004 09:53 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
C++, when to go for static or dynamic variables? linuxlah Programming 21 02-05-2003 08:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration