LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-28-2013, 07:20 PM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
C module variable conflict?


Hi all,

I just experienced an odd issue that I didn't think could happen. In my program I have several .c files. There are 0 external global variables defined. However I just created two modules that shared a variable name for internal use.

This caused some serious problems though with my logic. I was under the assumption that the variables are isolated from each other.

Is this just not the case?

Both the modules were using a pointer called "head". They had different structure types.

Thank you.
 
Old 09-29-2013, 12:35 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
use modifier static:

Code:
static mytype *head;
 
1 members found this post helpful.
Old 09-29-2013, 08:28 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
NevemTeve gave you the solution. That may be all you need, especially for just this time.

I will try to give you the answer, so you won't be confused over this next time.

Quote:
Originally Posted by yaplej View Post
There are 0 external global variables defined.
That is apparently incorrect. I expect you mean you didn't use the keyword extern. But a global variable is defined every time you define a variable at module scope without the keyword static.

Quote:
However I just created two modules that shared a variable name for internal use.
Your intent may have been internal use, but you defined each of those variables for global use.


Quote:
This caused some serious problems though with my logic. I was under the assumption that the variables are isolated from each other.
Variables of the same name defined at function scope are isolated by scope. The lack of "static" at function scope does not make a variable global. (It makes a variable "automatic" meaning it acts as if it were created on entry to its scope and destroyed on exit, so its value is likely lost when exiting the scope and reentering it later.)

Variables defined at module scope without "static" are global. Module "scope" effectively acts like a scope only when the keyword static is used.
 
1 members found this post helpful.
Old 09-29-2013, 09:15 PM   #4
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
Thank you for that explanation. Your correct in that I did not explicitly define them as external. My understanding of external vs static vs auto is limited. I was under the assumption that to access a variable from another module it had to be defined as external. I also did not know that for the variable to be persistent that it required being defined with the static keyword.
 
Old 09-30-2013, 12:18 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by yaplej View Post
I also did not know that for the variable to be persistent that it required being defined with the static keyword.
I hope that doesn't mean I confused you, so let me try again to summarize:

In a function scope, you have either auto (not persistent) or static (persistent).

In a module scope, you have either global (persistent) or static (also persistent).

Also, in case this detail was misleading: While you are in any kind of inner scope an auto variable may not be accessible, but its value persists. The value is only non persistent when you go outside its scope, not when you nest into some other scope that just makes it temporarily inaccessible.

Last edited by johnsfine; 09-30-2013 at 12:23 PM.
 
  


Reply



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
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 12:26 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Module conflict causing network services to fail rollo Linux - Newbie 3 11-01-2005 12:19 PM
Allocating memory for user program variable from kernel module appas Programming 2 08-12-2004 10:52 PM
compiling a kernel: module-init-tools conflict, depmod problem, modules sterrenkijker Debian 3 03-08-2004 10:07 PM

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

All times are GMT -5. The time now is 12:04 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