so I understand that global variables are evil, and programmers should avoid them. However, I have been doing some studying regarding potential vulnerabilities behind it, so that is the reason why I'm using it at the moment.
I have this toy example:
#define MAX_LENGTH 8
int counter = MAX_LENGTH;
int *size;
int main(int argc, char **argv)
{
int value = atoi(argv[1]);
size = &value;
int *test;
*test = value;
printf("Hello World %d %d\n", *size, *test);
}
So the core question is that why for a global variable pointer, do I have to get the address of a variable to store the value stored in that variable?
My understanding of storing a value to a pointer is how I used the int *test;
variable. I simply dereference this variable *test = value
to store the value. However, I am curious why for global variable pointer *size
, I cannot simply do *size = value
, but instead must do size = &value
.
I tried searching this question through StackOverflow, but maybe I didn't phrase the question right or such, so I could not find the relevant link for it.
I would appreciate any kind of tips, thank you in advance,
Kind regards,
Edit: Thanks everyone for the responses!
CodePudding user response:
int *test;
*test = value;
It is undefined behaviour. The pointer is not initialized and it is not referencing a valid int
object. Your code "works" only by accident - you did not overwrite anything important or the wild (initialized pointers are called this way) was not referencing the protected memory area. Never use any not initialized pointers or automatic variables.
It is a very serious C programming error. It does not matter if the pointer is defined in the file (global) or local scope. They behave exactly the same.
CodePudding user response:
Think of a pointer as a place in memory meant to store an address so that int *test
, a pointer to an int
, stores an address containing an int
. When you first declare it, int *test
, test has garbage as its value since you didn't initialize it. You were lucky that the garbage happened to be the value of "some address". When you set the value of that "garbage address" to value, *test = value;
, you stored value (the int) in the "garbage address". If the random value in test happened to be an invalid address, like 0, your program would've crashed.
You should always initialize your pointers with a valid address, in this case an int
address, before you store a value into that address. You may initialize you pointer with three kinds of addresses: nullptr
, an address of an already existing integer, or an address dynamically obtained through new
, for instance:
int *p = nullptr; // valid value for a pointer, but
// don't use it since it isn't pointing to a valid address yet
int value = 0; // an integer stored in an address in the stack
int *q = &value; // initialized with the address of value; it is said to point to value
p = &value; // p now also points to value
int *r = new int(0); // initialized with a dynamic address from the heap returned by new
// new also initialized the integer in the address with the value of 0
...
delete r; // don't forget to destroy and free the dynamic memory when you're done using it
// or you'll have a memory leak...
EDIT: As @Ben Voight commented. There is a difference in initialization between global and local pointers. Global pointers with no explicit initialization are implicitly zero-initialized, i.e. nullptr. On the other hand, local pointers that are not explicitly initialized are not, and they contain an indeterminate value.