Re: [algogeeks] Re: Process memory Layout
> > "What i am saying is >>> >>> if i write it like >>> p[5]= 'a'; >>> >>> Then the compiler will raise an error . Because the memory where the >>> string constant "Hello world" gets stored is read only. >>> >>> But it i do it like >>> >>> char s[] = "hello world",*p; >>> >>> p=s; >>> >>> p[5]='a'; >>> >>> It is valid becoz the string is now stored in stack segment of program . >>> >>> So my doubt is where the string constant gets stored exactly in the first >>> case...and why it cant be altered.." >> >> When you declare a string using pointer, the contents are stored in *read only memory*. So if u try to alter it, it will show error, but when u create an array of same string ,compiler copies the string from read only memory to stack where you can modify it. So when u alter it, it shows no error. -- Thanks and Regards *Devansh Gupta* *B.Tech Third Year* *MNNIT, Allahabad* -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Process memory Layout
its a run time error(segmentation fault). it is placed in a read only memory On Mon, Aug 22, 2011 at 3:05 PM, aditi garg wrote: > @ kumar > if we hav > char *p="hello world"; > p[5]= 'a'; > it generates only run time error and not compile time error > > > On Mon, Aug 22, 2011 at 10:47 AM, kumar raja wrote: > >> What i am saying is >> >> if i write it like >> p[5]= 'a'; >> >> Then the compiler will raise an error . Because the memory where the >> string constant "Hello world" gets stored is read only. >> >> But it i do it like >> >> char s[] = "hello world",*p; >> >> p=s; >> >> p[5]='a'; >> >> It is valid becoz the string is now stored in stack segment of program . >> >> So my doubt is where the string constant gets stored exactly in the first >> case...and why it cant be altered.. >> >> >> On 21 August 2011 22:06, Sanjay Rajpal wrote: >> >>> You can change the pointer only, not the content. >>> >>> But in case of static int, u can also change the value also. if u specify >>> const, u can't change the value then. >>> >>> >>> Sanju >>> :) >>> >>> >>> >>> On Sun, Aug 21, 2011 at 9:56 PM, Dave wrote: >>> @Kumar: You've declared a pointer, and you can change the pointer, just as in int i=10 declares an integer that you can change. Dave On Aug 21, 11:28 pm, kumar raja wrote: > char *p= "hello world"; > > When we try to modify the above string it will raise an error. I heard that > the String constant is stored in the Data segment of the process . The data > segment consists of two parts . > 1)Initialized data segment > 2)Uninitialized data segment > > If we use some other global variable in the same program say static int > i=10; > > But it could be modified at later.So why not the string constant cant be > modified??? Someone said that it is stored in Text/Code segment . I think > thats wrong . the Text segment is only a set of machine instructions to > execute the program ,but does not contain any data values. > > So, Where the above String constant is stored and why it cant be altered??? > > -- > Regards > Kumar Raja > M.Tech(SIT) > IIT Kharagpur, > 10it60...@iitkgp.ac.in > 7797137043. > 09491690115. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Regards >> Kumar Raja >> M.Tech(SIT) >> IIT Kharagpur, >> 10it60...@iitkgp.ac.in >> 7797137043. >> 09491690115. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Aditi Garg > Undergraduate Student > Electronics & Communication Divison > NETAJI SUBHAS INSTITUTE OF TECHNOLOGY > Sector 3, Dwarka > New Delhi > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Process memory Layout
string are stored in fixed memory locations(thats why they called as immutable). alteration to that memory location is not allowed. Thank you, Siddharam On Mon, Aug 22, 2011 at 3:05 PM, aditi garg wrote: > @ kumar > if we hav > char *p="hello world"; > p[5]= 'a'; > it generates only run time error and not compile time error > > On Mon, Aug 22, 2011 at 10:47 AM, kumar raja wrote: > >> What i am saying is >> >> if i write it like >> p[5]= 'a'; >> >> Then the compiler will raise an error . Because the memory where the >> string constant "Hello world" gets stored is read only. >> >> But it i do it like >> >> char s[] = "hello world",*p; >> >> p=s; >> >> p[5]='a'; >> >> It is valid becoz the string is now stored in stack segment of program . >> >> So my doubt is where the string constant gets stored exactly in the first >> case...and why it cant be altered.. >> >> >> On 21 August 2011 22:06, Sanjay Rajpal wrote: >> >>> You can change the pointer only, not the content. >>> >>> But in case of static int, u can also change the value also. if u specify >>> const, u can't change the value then. >>> >>> >>> Sanju >>> :) >>> >>> >>> >>> On Sun, Aug 21, 2011 at 9:56 PM, Dave wrote: >>> @Kumar: You've declared a pointer, and you can change the pointer, just as in int i=10 declares an integer that you can change. Dave On Aug 21, 11:28 pm, kumar raja wrote: > char *p= "hello world"; > > When we try to modify the above string it will raise an error. I heard that > the String constant is stored in the Data segment of the process . The data > segment consists of two parts . > 1)Initialized data segment > 2)Uninitialized data segment > > If we use some other global variable in the same program say static int > i=10; > > But it could be modified at later.So why not the string constant cant be > modified??? Someone said that it is stored in Text/Code segment . I think > thats wrong . the Text segment is only a set of machine instructions to > execute the program ,but does not contain any data values. > > So, Where the above String constant is stored and why it cant be altered??? > > -- > Regards > Kumar Raja > M.Tech(SIT) > IIT Kharagpur, > 10it60...@iitkgp.ac.in > 7797137043. > 09491690115. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Regards >> Kumar Raja >> M.Tech(SIT) >> IIT Kharagpur, >> 10it60...@iitkgp.ac.in >> 7797137043. >> 09491690115. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Aditi Garg > Undergraduate Student > Electronics & Communication Divison > NETAJI SUBHAS INSTITUTE OF TECHNOLOGY > Sector 3, Dwarka > New Delhi > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Process memory Layout
@ kumar if we hav char *p="hello world"; p[5]= 'a'; it generates only run time error and not compile time error On Mon, Aug 22, 2011 at 10:47 AM, kumar raja wrote: > What i am saying is > > if i write it like > p[5]= 'a'; > > Then the compiler will raise an error . Because the memory where the string > constant "Hello world" gets stored is read only. > > But it i do it like > > char s[] = "hello world",*p; > > p=s; > > p[5]='a'; > > It is valid becoz the string is now stored in stack segment of program . > > So my doubt is where the string constant gets stored exactly in the first > case...and why it cant be altered.. > > > On 21 August 2011 22:06, Sanjay Rajpal wrote: > >> You can change the pointer only, not the content. >> >> But in case of static int, u can also change the value also. if u specify >> const, u can't change the value then. >> >> >> Sanju >> :) >> >> >> >> On Sun, Aug 21, 2011 at 9:56 PM, Dave wrote: >> >>> @Kumar: You've declared a pointer, and you can change the pointer, >>> just as in int i=10 declares an integer that you can change. >>> >>> Dave >>> >>> On Aug 21, 11:28 pm, kumar raja wrote: >>> > char *p= "hello world"; >>> > >>> > When we try to modify the above string it will raise an error. I heard >>> that >>> > the String constant is stored in the Data segment of the process . The >>> data >>> > segment consists of two parts . >>> > 1)Initialized data segment >>> > 2)Uninitialized data segment >>> > >>> > If we use some other global variable in the same program say static >>> int >>> > i=10; >>> > >>> > But it could be modified at later.So why not the string constant cant >>> be >>> > modified??? Someone said that it is stored in Text/Code segment . I >>> think >>> > thats wrong . the Text segment is only a set of machine instructions to >>> > execute the program ,but does not contain any data values. >>> > >>> > So, Where the above String constant is stored and why it cant be >>> altered??? >>> > >>> > -- >>> > Regards >>> > Kumar Raja >>> > M.Tech(SIT) >>> > IIT Kharagpur, >>> > 10it60...@iitkgp.ac.in >>> > 7797137043. >>> > 09491690115. >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > > > -- > Regards > Kumar Raja > M.Tech(SIT) > IIT Kharagpur, > 10it60...@iitkgp.ac.in > 7797137043. > 09491690115. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Aditi Garg Undergraduate Student Electronics & Communication Divison NETAJI SUBHAS INSTITUTE OF TECHNOLOGY Sector 3, Dwarka New Delhi -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Process memory Layout
What i am saying is if i write it like p[5]= 'a'; Then the compiler will raise an error . Because the memory where the string constant "Hello world" gets stored is read only. But it i do it like char s[] = "hello world",*p; p=s; p[5]='a'; It is valid becoz the string is now stored in stack segment of program . So my doubt is where the string constant gets stored exactly in the first case...and why it cant be altered.. On 21 August 2011 22:06, Sanjay Rajpal wrote: > You can change the pointer only, not the content. > > But in case of static int, u can also change the value also. if u specify > const, u can't change the value then. > > > Sanju > :) > > > > On Sun, Aug 21, 2011 at 9:56 PM, Dave wrote: > >> @Kumar: You've declared a pointer, and you can change the pointer, >> just as in int i=10 declares an integer that you can change. >> >> Dave >> >> On Aug 21, 11:28 pm, kumar raja wrote: >> > char *p= "hello world"; >> > >> > When we try to modify the above string it will raise an error. I heard >> that >> > the String constant is stored in the Data segment of the process . The >> data >> > segment consists of two parts . >> > 1)Initialized data segment >> > 2)Uninitialized data segment >> > >> > If we use some other global variable in the same program say static int >> > i=10; >> > >> > But it could be modified at later.So why not the string constant cant be >> > modified??? Someone said that it is stored in Text/Code segment . I >> think >> > thats wrong . the Text segment is only a set of machine instructions to >> > execute the program ,but does not contain any data values. >> > >> > So, Where the above String constant is stored and why it cant be >> altered??? >> > >> > -- >> > Regards >> > Kumar Raja >> > M.Tech(SIT) >> > IIT Kharagpur, >> > 10it60...@iitkgp.ac.in >> > 7797137043. >> > 09491690115. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Regards Kumar Raja M.Tech(SIT) IIT Kharagpur, 10it60...@iitkgp.ac.in 7797137043. 09491690115. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Process memory Layout
You can change the pointer only, not the content. But in case of static int, u can also change the value also. if u specify const, u can't change the value then. Sanju :) On Sun, Aug 21, 2011 at 9:56 PM, Dave wrote: > @Kumar: You've declared a pointer, and you can change the pointer, > just as in int i=10 declares an integer that you can change. > > Dave > > On Aug 21, 11:28 pm, kumar raja wrote: > > char *p= "hello world"; > > > > When we try to modify the above string it will raise an error. I heard > that > > the String constant is stored in the Data segment of the process . The > data > > segment consists of two parts . > > 1)Initialized data segment > > 2)Uninitialized data segment > > > > If we use some other global variable in the same program say static int > > i=10; > > > > But it could be modified at later.So why not the string constant cant be > > modified??? Someone said that it is stored in Text/Code segment . I think > > thats wrong . the Text segment is only a set of machine instructions to > > execute the program ,but does not contain any data values. > > > > So, Where the above String constant is stored and why it cant be > altered??? > > > > -- > > Regards > > Kumar Raja > > M.Tech(SIT) > > IIT Kharagpur, > > 10it60...@iitkgp.ac.in > > 7797137043. > > 09491690115. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Process memory Layout
@Kumar: You've declared a pointer, and you can change the pointer, just as in int i=10 declares an integer that you can change. Dave On Aug 21, 11:28 pm, kumar raja wrote: > char *p= "hello world"; > > When we try to modify the above string it will raise an error. I heard that > the String constant is stored in the Data segment of the process . The data > segment consists of two parts . > 1)Initialized data segment > 2)Uninitialized data segment > > If we use some other global variable in the same program say static int > i=10; > > But it could be modified at later.So why not the string constant cant be > modified??? Someone said that it is stored in Text/Code segment . I think > thats wrong . the Text segment is only a set of machine instructions to > execute the program ,but does not contain any data values. > > So, Where the above String constant is stored and why it cant be altered??? > > -- > Regards > Kumar Raja > M.Tech(SIT) > IIT Kharagpur, > 10it60...@iitkgp.ac.in > 7797137043. > 09491690115. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.