Do previous declarations in C change how pointers are initialized? [duplicate]
This question already has an answer here:
pointer default value .?
4 answers
How are pointers in C initialized? It seems like previous declarations change how they are initialized.
Consider the following example:
int *a;
printf("a: %pn", (void*)a);
This code snippet results in
a: (nil)
So one could think that variables at function start are initialized with null, but if I execute this code:
int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}
This is the result:
a: 0x7ffff3bb2e40
b: (nil)
How can I determine how the variables are initialized?
c pointers memory initialization
marked as duplicate by Pedro Silva, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
pointer default value .?
4 answers
How are pointers in C initialized? It seems like previous declarations change how they are initialized.
Consider the following example:
int *a;
printf("a: %pn", (void*)a);
This code snippet results in
a: (nil)
So one could think that variables at function start are initialized with null, but if I execute this code:
int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}
This is the result:
a: 0x7ffff3bb2e40
b: (nil)
How can I determine how the variables are initialized?
c pointers memory initialization
marked as duplicate by Pedro Silva, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42
add a comment |
This question already has an answer here:
pointer default value .?
4 answers
How are pointers in C initialized? It seems like previous declarations change how they are initialized.
Consider the following example:
int *a;
printf("a: %pn", (void*)a);
This code snippet results in
a: (nil)
So one could think that variables at function start are initialized with null, but if I execute this code:
int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}
This is the result:
a: 0x7ffff3bb2e40
b: (nil)
How can I determine how the variables are initialized?
c pointers memory initialization
This question already has an answer here:
pointer default value .?
4 answers
How are pointers in C initialized? It seems like previous declarations change how they are initialized.
Consider the following example:
int *a;
printf("a: %pn", (void*)a);
This code snippet results in
a: (nil)
So one could think that variables at function start are initialized with null, but if I execute this code:
int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}
This is the result:
a: 0x7ffff3bb2e40
b: (nil)
How can I determine how the variables are initialized?
This question already has an answer here:
pointer default value .?
4 answers
c pointers memory initialization
c pointers memory initialization
edited Nov 22 '18 at 21:51
M.M
106k11120244
106k11120244
asked Nov 22 '18 at 21:08
NightRain23NightRain23
183
183
marked as duplicate by Pedro Silva, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Pedro Silva, gsamaras
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 8:56
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
5
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42
add a comment |
5
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42
5
5
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42
add a comment |
3 Answers
3
active
oldest
votes
If a pointer is defined at file scope, it is initialized to NULL.
If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
add a comment |
Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities
- Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.
- Initialization during startup. It is quite common to initialize to zero some segments of memory such us the
bss
segment during the startup (beforemain()
). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.
Edited for accuracy due to M.M 's comment.
add a comment |
How are pointers in C initialized?
In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.
The only pointers which are implicitly initialized are those in global or static
variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL
pointer.
In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g
). Then you'll get warnings. Improve your code to have none.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If a pointer is defined at file scope, it is initialized to NULL.
If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
add a comment |
If a pointer is defined at file scope, it is initialized to NULL.
If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
add a comment |
If a pointer is defined at file scope, it is initialized to NULL.
If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.
If a pointer is defined at file scope, it is initialized to NULL.
If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.
answered Nov 22 '18 at 21:28
dbushdbush
103k13108145
103k13108145
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
add a comment |
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
5
5
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.
– M.M
Nov 22 '18 at 21:38
add a comment |
Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities
- Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.
- Initialization during startup. It is quite common to initialize to zero some segments of memory such us the
bss
segment during the startup (beforemain()
). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.
Edited for accuracy due to M.M 's comment.
add a comment |
Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities
- Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.
- Initialization during startup. It is quite common to initialize to zero some segments of memory such us the
bss
segment during the startup (beforemain()
). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.
Edited for accuracy due to M.M 's comment.
add a comment |
Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities
- Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.
- Initialization during startup. It is quite common to initialize to zero some segments of memory such us the
bss
segment during the startup (beforemain()
). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.
Edited for accuracy due to M.M 's comment.
Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities
- Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.
- Initialization during startup. It is quite common to initialize to zero some segments of memory such us the
bss
segment during the startup (beforemain()
). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.
Edited for accuracy due to M.M 's comment.
edited Nov 23 '18 at 8:36
answered Nov 22 '18 at 21:19
JoseJose
1,291516
1,291516
add a comment |
add a comment |
How are pointers in C initialized?
In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.
The only pointers which are implicitly initialized are those in global or static
variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL
pointer.
In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g
). Then you'll get warnings. Improve your code to have none.
add a comment |
How are pointers in C initialized?
In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.
The only pointers which are implicitly initialized are those in global or static
variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL
pointer.
In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g
). Then you'll get warnings. Improve your code to have none.
add a comment |
How are pointers in C initialized?
In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.
The only pointers which are implicitly initialized are those in global or static
variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL
pointer.
In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g
). Then you'll get warnings. Improve your code to have none.
How are pointers in C initialized?
In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.
The only pointers which are implicitly initialized are those in global or static
variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL
pointer.
In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g
). Then you'll get warnings. Improve your code to have none.
edited Nov 23 '18 at 8:47
answered Nov 23 '18 at 8:40
Basile StarynkevitchBasile Starynkevitch
179k13173374
179k13173374
add a comment |
add a comment |
5
Undefined behavior is undefined.
– EOF
Nov 22 '18 at 21:12
The pointer either has some random value or is zeroed, depending on how it is declared.
– CoffeeTableEspresso
Nov 22 '18 at 21:13
You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)
– M.M
Nov 22 '18 at 21:42