The stack a pointer reside [duplicate]
This question already has an answer here:
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?
17 answers
I read a make_tempory_file demonstration in APUE, and confused about:
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
void make_temp(char *template);
int main()
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
printf("trying to create first temp file...n");
make_temp(good_template);
printf("trying to create second temp file...n");
make_temp(bad_template);
exit(0);
void make_temp(char *template)
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0)
err_sys("can’t create temp file");
printf("temp name = %sn", template);
close(fd);
if (stat(template, &sbuf) < 0)
if (errno == ENOENT)
printf("file doesn’t existn");
else
err_sys("stat failed");
else
printf("file existsn");
unlink(template);
The instruction explain it:
The difference in behavior comes from the way the two template strings are declared. For the first template, the name is allocated on the stack, because we use an array variable. For the second name, however, we use a pointer. In this case, only the memory for the pointer itself resides on the stack; the compiler arranges for the string to be stored in the read-only segment of the executable. When the mkstemp function tries to modify the string, a segmentation fault occurs.
I tried to understand the statement but stuck with stack
Does it refer to the stack where the arrow point to?
c
marked as duplicate by Antti Haapala
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 14 '18 at 8:19
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:
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?
17 answers
I read a make_tempory_file demonstration in APUE, and confused about:
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
void make_temp(char *template);
int main()
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
printf("trying to create first temp file...n");
make_temp(good_template);
printf("trying to create second temp file...n");
make_temp(bad_template);
exit(0);
void make_temp(char *template)
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0)
err_sys("can’t create temp file");
printf("temp name = %sn", template);
close(fd);
if (stat(template, &sbuf) < 0)
if (errno == ENOENT)
printf("file doesn’t existn");
else
err_sys("stat failed");
else
printf("file existsn");
unlink(template);
The instruction explain it:
The difference in behavior comes from the way the two template strings are declared. For the first template, the name is allocated on the stack, because we use an array variable. For the second name, however, we use a pointer. In this case, only the memory for the pointer itself resides on the stack; the compiler arranges for the string to be stored in the read-only segment of the executable. When the mkstemp function tries to modify the string, a segmentation fault occurs.
I tried to understand the statement but stuck with stack
Does it refer to the stack where the arrow point to?
c
marked as duplicate by Antti Haapala
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 14 '18 at 8:19
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:
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?
17 answers
I read a make_tempory_file demonstration in APUE, and confused about:
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
void make_temp(char *template);
int main()
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
printf("trying to create first temp file...n");
make_temp(good_template);
printf("trying to create second temp file...n");
make_temp(bad_template);
exit(0);
void make_temp(char *template)
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0)
err_sys("can’t create temp file");
printf("temp name = %sn", template);
close(fd);
if (stat(template, &sbuf) < 0)
if (errno == ENOENT)
printf("file doesn’t existn");
else
err_sys("stat failed");
else
printf("file existsn");
unlink(template);
The instruction explain it:
The difference in behavior comes from the way the two template strings are declared. For the first template, the name is allocated on the stack, because we use an array variable. For the second name, however, we use a pointer. In this case, only the memory for the pointer itself resides on the stack; the compiler arranges for the string to be stored in the read-only segment of the executable. When the mkstemp function tries to modify the string, a segmentation fault occurs.
I tried to understand the statement but stuck with stack
Does it refer to the stack where the arrow point to?
c
This question already has an answer here:
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?
17 answers
I read a make_tempory_file demonstration in APUE, and confused about:
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
void make_temp(char *template);
int main()
char good_template = "/tmp/dirXXXXXX"; /* right way */
char *bad_template = "/tmp/dirXXXXXX"; /* wrong way*/
printf("trying to create first temp file...n");
make_temp(good_template);
printf("trying to create second temp file...n");
make_temp(bad_template);
exit(0);
void make_temp(char *template)
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0)
err_sys("can’t create temp file");
printf("temp name = %sn", template);
close(fd);
if (stat(template, &sbuf) < 0)
if (errno == ENOENT)
printf("file doesn’t existn");
else
err_sys("stat failed");
else
printf("file existsn");
unlink(template);
The instruction explain it:
The difference in behavior comes from the way the two template strings are declared. For the first template, the name is allocated on the stack, because we use an array variable. For the second name, however, we use a pointer. In this case, only the memory for the pointer itself resides on the stack; the compiler arranges for the string to be stored in the read-only segment of the executable. When the mkstemp function tries to modify the string, a segmentation fault occurs.
I tried to understand the statement but stuck with stack
Does it refer to the stack where the arrow point to?
This question already has an answer here:
Why do I get a segmentation fault when writing to a string initialized with “char *s” but not “char s”?
17 answers
c
c
asked Nov 14 '18 at 4:28
JawSawJawSaw
4,59811837
4,59811837
marked as duplicate by Antti Haapala
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 14 '18 at 8:19
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 Antti Haapala
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 14 '18 at 8:19
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 |
add a comment |
1 Answer
1
active
oldest
votes
When the mkstemp function tries to modify the string, a segmentation fault occurs.
The reason is because the string declared as char *bad_template = "/tmp/dirXXXXXX";
the bad_template
is assigned to a character string literal.
In C
, modifying a string literal is undefined behaviour
Reference - Section 6.4.5-6 of C99 Standard
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz". A wide string literal is the same, except prefixed by the letter L.
If the program attempts to modify such an array, the behavior is undefined.
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-const
, such as the OP'sgood_template
, may be modified.
– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When the mkstemp function tries to modify the string, a segmentation fault occurs.
The reason is because the string declared as char *bad_template = "/tmp/dirXXXXXX";
the bad_template
is assigned to a character string literal.
In C
, modifying a string literal is undefined behaviour
Reference - Section 6.4.5-6 of C99 Standard
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz". A wide string literal is the same, except prefixed by the letter L.
If the program attempts to modify such an array, the behavior is undefined.
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-const
, such as the OP'sgood_template
, may be modified.
– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
add a comment |
When the mkstemp function tries to modify the string, a segmentation fault occurs.
The reason is because the string declared as char *bad_template = "/tmp/dirXXXXXX";
the bad_template
is assigned to a character string literal.
In C
, modifying a string literal is undefined behaviour
Reference - Section 6.4.5-6 of C99 Standard
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz". A wide string literal is the same, except prefixed by the letter L.
If the program attempts to modify such an array, the behavior is undefined.
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-const
, such as the OP'sgood_template
, may be modified.
– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
add a comment |
When the mkstemp function tries to modify the string, a segmentation fault occurs.
The reason is because the string declared as char *bad_template = "/tmp/dirXXXXXX";
the bad_template
is assigned to a character string literal.
In C
, modifying a string literal is undefined behaviour
Reference - Section 6.4.5-6 of C99 Standard
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz". A wide string literal is the same, except prefixed by the letter L.
If the program attempts to modify such an array, the behavior is undefined.
When the mkstemp function tries to modify the string, a segmentation fault occurs.
The reason is because the string declared as char *bad_template = "/tmp/dirXXXXXX";
the bad_template
is assigned to a character string literal.
In C
, modifying a string literal is undefined behaviour
Reference - Section 6.4.5-6 of C99 Standard
A character string literal is a sequence of zero or more multibyte characters enclosed in
double-quotes, as in "xyz". A wide string literal is the same, except prefixed by the letter L.
If the program attempts to modify such an array, the behavior is undefined.
edited Nov 14 '18 at 4:40
answered Nov 14 '18 at 4:35
Rishikesh RajeRishikesh Raje
5,6071826
5,6071826
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-const
, such as the OP'sgood_template
, may be modified.
– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
add a comment |
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-const
, such as the OP'sgood_template
, may be modified.
– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-
const
, such as the OP's good_template
, may be modified.– John Bollinger
Nov 14 '18 at 4:48
+1 for a correct answer that avoids discussing the stack at all. "Stack" is not a C language concept. Rather, it is a (very common) implementation detail. The important thing is that regardless of the nature or location of any object's storage, string literals must not be modified. Of course, any other array whose elements are non-
const
, such as the OP's good_template
, may be modified.– John Bollinger
Nov 14 '18 at 4:48
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
so I should forget stack, is it right to use stack here? @JohnBollinger
– JawSaw
Nov 14 '18 at 7:47
2
2
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate - The reason you are getting a segmentation fault has nothing to do with the stack. You are modifying a string literal, which should not be modified.
– Rishikesh Raje
Nov 14 '18 at 8:27
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
@A_Pivate, I don't mean to say that there isn't a stack. I'm just saying that that's not a good way to think about the problem, nor really about any problem involving C language semantics.
– John Bollinger
Nov 14 '18 at 14:00
add a comment |