Re: char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)



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
.



Relevant Pages

  • Re: char * ptr = "amit", in which area of memory it is stored (heap,stack,code,global)
    ... you cannot change or append into this. ... Well You are right your program will crash. ... Your str1 is assigned to string "amit" when you do strcatyour ...
    (comp.lang.c)
  • Re: newbie trouble making array of instances
    ... A new string instance was created by the Removemethod, and the reference to this new instance was assigned to str1. ... Notice there is no verbiage describing special-casing around a string that happens to come back as empty; it says it *always* returns a new string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TransferText Export Problem
    ... Sub ExportDataToCSV() ... Dim DB As DAO.Database, RS As DAO.Recordset, fld As DAO.Field, str1 As ... String ... There is also a Write command ...
    (comp.databases.ms-access)
  • Re: How to enter a price value from right to left?
    ... Valworks on a null string, ... both the textual value and the SelStart property of the textbox, ... > Bonj, thanks a lot ... ... > I had a crash when I highlighted the complete text and hit the ...
    (microsoft.public.vb.general.discussion)
  • Re: access 2003 crashing
    ... before it has finished the calculation and this causes the crash. ... Dim MySQL As String, MySQL1 As String, MySQL2 As String, Dt As String, ... As String, MySQL7 As String, cp As Currency, D As Currency ... 'UPDATE SERVICES SO THAT MONETARY DATA IS BLANK IN RST ...
    (microsoft.public.access.formscoding)