473,804 Members | 3,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..

Nov 15 '05 #1
10 1943
Amit wrote:

char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..


You're close.
The rules of C say that
an attempt to write to an object identified by a string literal,
causes undefined behavior.
str1 points to an object identified by a string literal.
Also, you need to make sure you have enough room for strcat.

char *str1 = "amit";
char str2[sizeof "amit" - 1 + sizeof "mehta"] = "mehta";

strcat(str2, str1);

--
pete
Nov 15 '05 #2

Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..

I thing it is wrong because
str1 has the address of the value amit
u have to strcat(*str1,st r2)

Nov 15 '05 #3
venkatesh wrote:

Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..


I thing it is wrong because
str1 has the address of the value amit
u have to strcat(*str1,st r2)


Get yourself to a compiler and try your idea.
You're very wrong.

*str1 is an expression of type char with a value of 'a',
refering to a byte in a nonwriteable object.
The first argument to strcat must be a pointer to a string
in a writable object that has enough space for the resulting
concatenated string.

--
pete
Nov 15 '05 #4
Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..


Errors abound, crash or not.

char *str1 = "amit";

This defines "amit" a string constant and assigns its address to str1.
The string constant is perhaps not writeable.

char str2[] = "mehta";

This defines "mehta" a string constant and assigns the string itself to
the array. The type of str2 is now 'array 6 of char'. str2 belongs to
you and is writeable.

strcat(str1, str2);

This might 'work' but cannot be correct. The memory at the end of str1
is not yours.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #5
"Amit" <am************ @gmail.com> writes:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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..


The language doesn't define where str1 is stored; it could be carved
into stone tablets.

str1 is a pointer to the array created by the string literal "amit1".
Attempting to write to that array, or attempting to read or write
beyond the end of the array, invokes undefined behavior.

The strcat() call will crash *if you're lucky*. If you're unlucky, it
will seem to work. That's the nature of undefined behavior.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6
Das

Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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.
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.

This is generally not noticed for small strings. but if your second
string is large enough then you will get SIGSEGV.

Nov 15 '05 #7
On 16 Oct 2005 02:04:32 -0700, "Das" <as*********@gm ail.com> wrote in
comp.lang.c:

Amit wrote:
char *str1="amit";
char str2[]="mehta"
strcat(str1,str 2);

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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #8
CoL
In this case , according to ANSI C standard the string literal becomes
the part of read only memory, most obvious the datasection. So the
nature of the char * str actually becomes const char *str1... so you
cant write into it.

Nov 15 '05 #9
"CoL" <ap***********@ gmail.com> writes:
In this case , according to ANSI C standard the string literal becomes
the part of read only memory, most obvious the datasection. So the
nature of the char * str actually becomes const char *str1... so you
cant write into it.


One more time. Please provide context when you post a followup.

If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Please complain to Google about their broken interface.

String literals in C are not of type const char*. A string literal is
an array of char (not const); in most contexts, it's implicitly
converted to char* (again, not const). Modifying a string literal is
undefined behavior because the standard specifically says it's
undefined behavior.

It might have made more sense for string literals to be const
(<OT>they are in C++</OT>), but they aren't.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4402
by: Dmitri | last post by:
Hi there, Does anybody know what is DB2 UDB admin API equivalent to "db2 terminate" command? Some background: I'm developing monitoring application(http://chuzhoi_files.tripod.com). I want to be able to specify a node number during attachment, so I'm using sqlesetc (API for "set client attach_dbpartitionum" command). If user specifies an incorrect node number, DB2 will fail with something like:
10
3521
by: Amit | last post by:
Which is more efficient and why? p++ or ++p. Thanks.
8
14777
by: John | last post by:
Hi all: Is there a C function to make a procedure sleep or delay for a few seconds/minutes on Linux and Sun OS platform? Thanks
7
3002
by: kaul | last post by:
i want to create a 2-d array containg r rows and c columns by dynamic memory allocation in a single statement so that i will be able to access the ith and jth index as say arr how is that possible?
2
1740
by: Ashish | last post by:
Iam trying the out of state session management for the first time, trying to convert a big project to be adaptable to both type of session management .. what i see that it is trying to serialize a class which is not touched by session object, do i have to serialize all the classes in the project or only the ones touches, also the abstract classes ? TIA -ashish
3
4571
by: Carpe Diem | last post by:
Hello I have an aspx page that loses Session("user") value after a few minutes even after I set <sessionState mode="InProc" cookieless="false" timeout="300"> in web.config and wrote function Session_Start() { Session.Timeout = 3000; } in global.asax
13
2823
by: andro | last post by:
Hi everybody! I have several tables from which I want to exract the SAME value (along with other referenced data). All the values are in the same column within the tables. How can I achieve this? TIA. Andro
13
2092
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Pair { std::string name;
6
1803
by: Amit_Basnak | last post by:
Dear Friends I have two structures as below typedef struct { long_int length; char data; } CI_STRUCT_DATA; typedef CI_STRUCT_DATA *ptr_CiStructData;
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.