3

Is it allowed to dynamically allocate memory for static variable like this:

#include <stdio.h>
#include <stdlib.h> 

struct person
{
   int age;
   int number;
};

static struct person* person_p = NULL;

int main()
{
    person_p = (struct person*)malloc(10 * sizeof(struct person));
}

The above code built, but is it really allowed to dynamically allocate memory for static variable?

ratzip
  • 1,571
  • 7
  • 28
  • 53
  • 1
    And more importantly why are you doing that? You know that functions accept paramters! You don't want to be confused when your program grows! – Iharob Al Asimi May 27 '15 at 12:39
  • 1
    what you mean by only the pointer is static? – ratzip May 27 '15 at 12:42
  • The pointer itself is static, but the memory which you allocated via the pointer neither knows nor cares about this. – Paul R May 27 '15 at 13:05
  • 1) in C, do not cast the returned value from malloc() (and family of functions) 2) always check (!=NULL) the returned value from malloc() (and family of functions) to assure the operation was successful. – user3629249 May 28 '15 at 02:57
  • the 'static' modifier means the pointer is only visible within the current file. This is a link time attribute. Other than that detail, it is like any other 'global' variable. – user3629249 May 28 '15 at 02:58

4 Answers4

4

Yes, it's valid and allowed. (Unless you're using the pointer as a placeholder) You can (and need to) dynamically allocate and free() memory to and from the pointer before and after using it.

Rather, please make a note, you do not cast the return value of malloc() and family in C.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • why if it is const, it is invalid? – ratzip May 27 '15 at 12:42
  • @ratzip you cannot change `const`. :-) – Sourav Ghosh May 27 '15 at 12:42
  • 1
    `const`-ness is irrelevant here, and probably is just confusing the issue. – Paul R May 27 '15 at 13:07
  • @PaulR Sir, I thought of bringing up the `const`, as OP had written _dynamic allocation for static variable_, so thought this will clear up the issue. Please correct me if i'm wrong – Sourav Ghosh May 27 '15 at 13:09
  • I don't think `const`-ness is at all relevant - you could still allocate memory dynamically - see e.g. http://codepad.org/fhn2PHg4 (although of course in this instance that would not be particularly useful!). – Paul R May 27 '15 at 13:11
  • @PaulR Sir, I think I'm doing a miserable job in explaining myself . :-( I tried to mean `struct person* const person_p = NULL;` in this case. Hope this is fine. – Sourav Ghosh May 27 '15 at 13:18
  • In that case though the use of `const` is not analogous to the use of `static` - I would suggest just deleting the following: "The variable is static, not const. So, there is no issue." and keep everything else - then your answer would make sense. – Paul R May 27 '15 at 13:20
  • @PaulR Appriciate the review sir, Updated, can you check once for the clarity now? – Sourav Ghosh May 27 '15 at 13:25
  • 1
    Thanks for taking the time to clarify the answer. – Paul R May 27 '15 at 13:44
0

I don't see why not. Even though static means there can only be one instance of the object, you still need space for that object. Keep in mind however that anything that is malloc'd needs to be free'd, so you will want to do that at the end of your main() function.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
  • this is C code. What you mean "so this structure may be better suited to be in a wrapper function or class where the destruction of it can be tracked." – ratzip May 27 '15 at 12:40
  • Ah yes - you are correct. In that case, you will want to free your structure at the end of the main() function. – Lawrence Aiello May 27 '15 at 12:40
  • Well, and `static` does not mean you can have only one instance of the object. This *is* one definition, this defines one object (or rather variable, since we're talking C). – Gauthier May 27 '15 at 15:22
0

Memory isn't "owned" by pointers to it. You can do the following things:

  • Dynamically allocate memory
  • Make a pointer point to that memory

It doesn't really make sense to say "dynamically allocate memory for a pointer".

Any pointer can point to any object (subject to alignment and aliasing restrictions), it makes no difference what the storage duration of the pointer is.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Note that it is the pointer that is static, not the memory it points to.

static means two unrelated things:

  1. give static memory allocation (memory allocated at start of program, and only released at end of program)
  2. give internal linkage (do not allow other compilation units - modules - to access the identifier. Here a variable, could be a function)

In your code, 1. static memory allocation is irrelevant, since the variable is global anyway and as such already has it.

Then, 2. internal linkage does not matter either, because what you are trying to do is inside the module anyway.

In other words, person_p is exactly as a usual global variable within your module, and you can do whatever you want to it.

It's only the pointer that is defined by this line of code, so you can dynamically allocate memory elsewhere, and assign the memory address to person_p if you wish.

Gauthier
  • 40,309
  • 11
  • 63
  • 97