Re: char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Sun, 16 Oct 2005 13:26:22 -0500
On 16 Oct 2005 02:04:32 -0700, "Das" <ashok.s.das@xxxxxxxxx> wrote in
comp.lang.c:
>
> Amit wrote:
> > char *str1="amit";
> > char str2[]="mehta"
> > strcat(str1,str2);
> >
> > It will crash, I feel str1 will be stored in code section. Once memory
> > is allocated, you cannot change or append into this.
> >
> > Please correct me If I am wrong..
>
> Well You are right your program will crash.
No, he and you are both wrong. Maybe the program will crash, maybe it
wall cause "The Star Spangled Banner" to play from the speakers on
your computer, even if your computer does not have speakers.
> Your str1 is assigned to string "amit" when you do strcat() your
> progam may not find enough space to append the str2[]. so a SIGSEGV (I
> hope) will occour.
Accuracy is highly prized in this group, please do not post answers
when you do not know what you are talking about.
The object 'str1' in the OP's sample code is pointing to a string
literal. Attempting to modify a string literal, such as by passing it
as the destination argument to strcat(), produces undefined behavior
because the C standard specifically says that it does. It has nothing
at all to do with how much space there is.
Once you generate undefined behavior, you no longer have a C program.
The language does not know and does not car what happens next.
> This is generally not noticed for small strings. but if your second
> string is large enough then you will get SIGSEGV.
"generally not noticed" by whom? It is undefined behavior, pure and
simple, and what happens afterwards is not a C language issue and is
not really topical here.
What your particular compiler or platform does is not what defines the
C language.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- References:
- Prev by Date: Re: Is % slow?
- Next by Date: Re: JR PROGRAMMER-ANALYST/ C C++/ IN
- Previous by thread: Re: char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)
- Next by thread: Re: char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)
- Index(es):
Relevant Pages
|