3

Can I access the static variable using the global pointer?

//In main.c file
static int c = 20; #Static vairble 
int *ptr = &c; #Global pointer variable
int main(){
  fun();
  printf("%d\n", c);
}

//In sub.c file
extern int *ptr;
void fun(){
  ++*ptr;
}

When I tried to access the *ptr global variable using the extern keyword in another file I'm able to access the static variable c and even the changes made on the *ptr is reflecting on c in main.c file

  1. Why does "ptr" not loses its global property even after its assigned by a static variable address?

  2. Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

  3. Can global variables store non-constant values?

1
  • What does ++*ptr actually do? What indirection is occuring? Commented Jan 12, 2019 at 22:01

4 Answers 4

4

static is not about laying the object into a portion of memory that cannot be altered /addressed from other translation units. It is just about defining that the object has internal linkage and therefore cannot be grabbed by other translation units through this (internal) name.

But if you somehow get the memory address of an object declared static in another translation unit, you may legally access the object.

So your construct is valid.

One might just obey the "static initialization order fiasco" when other translation units draw copies of your ptr (probably before this got pointed to c). Consider, for example, what might happen when another translation unit contains at file scope...

extern int *ptr;
int *myPtr = ptr;
...
int main() {
   int x = *myPtr;
}
Sign up to request clarification or add additional context in comments.

Comments

4

You're confusing the linkage of a pointer variable and the linkage of what it points to.

The linkage of a pointer does not change, regardless of what it points to.

Here the linkage of ptr is global (aka external) and the linkage of c is static (aka internal). Neither one changes the linkage of the other.

static means that c will not be visible (i.e. available for direct linkage) in another (e.g.) .c file. That is, another .c could not do:

int *ptr2 = &c;

But, it could do:

int **ptr3 = &ptr;

You could have the reverse:

int c = 20;
static int ptr = &c;

This is a static/internal linkage ptr pointing to a global/external linkage c.

A program has several memory areas: .text for code. .data for initialized variables (such as c) and .bss for unitialized areas.

Since you initialize both ptr and c, they both end up in .data

If you had done:

#include <stdio.h>

static int c = 20; // Static vairble
int *ptr;

int main(){
    ptr = &c;
    return 0;
}

Then, c is initialized and goes in .data but ptr is not initialized but is assigned and goes in the .bss segment

All variables [global or not] can have non-constant values. Either by initialization or assignment.

All above variables have global/file scope.

Here is another type of scope:

#include <stdio.h>

static int c = 20; // Static vairble

int main(){
    int *ptr = &c;
    return 0;
}

Here, ptr has function scope. It is placed on the stack frame of main. It has no linkage to speak of (i.e. it will never show up in the symbol table of the executable).

1 Comment

Scope is not the issue. Both c and ptr have file scope (which is the term used by the C standard, not global or static). The issue is linkage, which is the process by which the same identifier in different scopes can be made to refer to the same object. c has internal linkage. ptr has external linkage.
1

Why does "ptr" not loses its global property even after its assigned by a static variable address?

A variable cannot "loose" its scope or storage class, the static linkage class specifier determines the visibility of the symbol, not where or how it is stored. It prevents the data being referenced via that symbol, it does not prevent pointer references to the same location. C is a systems level language - as such it does not prevent you from doing pretty much anything that can be done at architecture level.

Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

No. The static keyword is used in this case to specify static or or internal linkage, it is also used (for function-local variables) as a storage class specifier, but all variables outside a function have static storage class regardless of internal or external linkage.

That said, initialised static storage data is placed in a different linker section than zero or uninitialised data. This is to facilitate runtime start-up initialisation.

Can global variables store non-constant values?

Perhaps that deserves a separate question to explain why you might think otherwise - it seems unrelated to your other questions and a but left-field. In your own example ptr is storing &c, but that could be modified at run-time to point elsewhere.

Comments

1

Why does "ptr" not loses its global property even after its assigned by a static variable address?

ptr remains global regardless where it points since it is declared global.

Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

In general they reside in the same data segment.

https://www.geeksforgeeks.org/memory-layout-of-c-program/

Can global variables store non-constant values?

yes, just like any other variable, thus the name "variable"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.