1

My question is about Memory and how it is being accessed across a program with multiple C files (Multiple Modules).

file1.h Version 1

    #ifndef file1_h
    #define file1_h

    typedef struct
    {
      UINT8  var1;
      UINT16 var2;
      UINT16 var3;
    } TestAccess;

    static TestAccess* pLongRangeAccess;

    #endif

file1.h Version 2

    #ifndef file1_h
    #define file1_h

    typedef struct
    {
      UINT8  var1;
      UINT16 var2;
      UINT16 var3;
    } TestAccess_t;

    TestAccess_t* pLongRangeAccess;

    #endif

Main.c

#include "file1.h"

void main(void)
{
  pLongRangeAccess->var1 = 4;
  pLongRangeAccess->var2 = 8;
  pLongRangeAccess->var3 = 16;
}

Module1.c //Needs read/write access to the struct variables

#include "file1.h"

void module1(void)
{
  pLongRangeAccess->var1 = 5;
  pLongRangeAccess->var2 = 10;
  pLongRangeAccess->var3 = 20;
}

Question 1) See "Version1" Does static in keyword make it such that there will be only one copy in heap memory (uninitialized) and therefore only one address of the pointer variable OR would it create 2 static variables with different memory since the header is used in every module?

Question 2) see "Version 2" If static was not declared for the pointer variable, then it would still be in heap memory and there would be one UNIQUE address that could be used to accessed to read and write operations to the members?

Question 3) In version 2 of file1.h if the pointer was declared in Main.c, and the memory location was fixed i.e it was declared as a constant, would it be available for access from any module by de-referencing the memory address location?

6
  • 2
    I see 3 questions, this is broad. Commented Jun 8, 2017 at 6:40
  • 1
    You don't declare static variables in header files. And there is no heap memory anywhere in these pieces of code. Commented Jun 8, 2017 at 6:40
  • 2
    Overall, never declare any variables in header files. Very poor program design and tends to lead to all manner of linking problems. Commented Jun 8, 2017 at 6:41
  • 1
    None of these are on the "heap". And static means static storage duration and linkage, read it as a global that's private to the compilation unit. What you're probably looking for is extern Commented Jun 8, 2017 at 6:41
  • 1
    "therefore only one address of the pointer variable OR would it create 2 static variables with different memory", this look like a XY problem Commented Jun 8, 2017 at 6:45

1 Answer 1

5

I think you are mixing up "memory allocation" with variable declaration and variable definition. I think what you want is to share a variable pLongRangeAccess among different translation units, such that it exists only once in your program. To do that, declare the variable in the header file, and define it once in a single c-file:

#ifndef file1_h
#define file1_h

typedef struct
{
  UINT8  var1;
  UINT16 var2;
  UINT16 var3;
} TestAccess;

extern TestAccess* pLongRangeAccess;

#endif

And then the .c-file:

// file1.c:
#include "file1.h"
TestAccess* pLongRangeAccess;

Note that keyword static, when applied to variables outside any function, means that this variable is private to the respective translation unit; this is mainly for information hiding and for avoiding unintended name clashes with other translation units (from different vendors, probably).

So, if you write

// file1.c
static TestAccess* pLongRangeAccess;

// file2.c
static TestAccess* pLongRangeAccess;

, then translation unit file1 and file2 both have their own private variable pLingRangeAccess, which is not visible to other translation units (regardless if any head file declares the variable as extern). The linker will not complain any "duplicate variable definition" in this case, as - as mentioned - the variables are private to their TU.

If you write, however, the following

// file1.c
TestAccess* pLongRangeAccess;

// file2.c
TestAccess* pLongRangeAccess;

, then both variables become visible to the other translation units, and the linker will complain that variable pLongRangeAccess is defined twice in your program, which is not allowed.

Sign up to request clarification or add additional context in comments.

1 Comment

Its becoming clear now. I see you define it as a static in the file1.c file. Static would mean that this pointer variable will only be accessible by any functions inside file1.c only. will a similar syntax be required for any other file that needs to access pLongRangeAccess?

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.