473,587 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char string

When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?

Regards
Mangesh .

Apr 9 '06 #1
25 1907
I think stack, since xyz is a constant statically allocated.

Apr 9 '06 #2

akiross wrote:
I think stack, since xyz is a constant statically allocated.


I don't know what the standard says, but wouldn't it depend on where
the statement was made? If it were global, wouldn't the memory be
allocated before the program started running? In that case, I don't
think it could be on the stack. It might be in an initialization
section.

Apr 9 '06 #3
On 2006-04-09, akiross <ak**********@g mail.com> wrote:
I think stack, since xyz is a constant statically allocated.


He hasnt specified *where* he has done this declaration so you cant
state whether it is static/heap or not : to be more specific if he is
writing this line in a function as opposed to "global declarations"
outside of any function definitions then this is one thing, inside
functions is another. Inside functions, without the static keyword
then it is a stack based allocation and you cant access it reliably at
function return. Inside functions with "static" it is probably heap and
fine to return a pointer to the data for access by calling functions.

What does the standard say about this?

Apr 9 '06 #4
mangesh wrote:
When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?


Neither. It can be passed out to higher scopes, even after the scope
of p ends (so its not on the stack unless its in a scope as high as
main().) You cannot reliably call free on it, or any containing memory
object, meaning its not in the heap.

The memory exists statically. That is, it occupies the same sort of
memory as globals do though the scope may differ. In the real world,
this is usually put into a memory segment called the init segment which
is similar to the data segment, but may have other attributes, such as
being "read-only".

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Apr 9 '06 #5
Richard G. Riley wrote:

On 2006-04-09, akiross <ak**********@g mail.com> wrote:
I think stack, since xyz is a constant statically allocated.


He hasnt specified *where* he has done this declaration so you cant
state whether it is static/heap or not : to be more specific if he is
writing this line in a function as opposed to "global declarations"
outside of any function definitions then this is one thing, inside
functions is another. Inside functions, without the static keyword
then it is a stack based allocation and you cant access it reliably at
function return.
Inside functions with "static" it is probably heap and
fine to return a pointer to the data for access by calling functions.

What does the standard say about this?


It says that you're clueless on this matter.

"The stack" and "the heap"
are not concerns of the C programming language.
An object refered to by a string literal such as "xyz",
has static duration,
regardless of whether it's inside or outside of a function.
Objects with static duration
are initialised before main starts to execute.

--
pete
Apr 9 '06 #6
On 9 Apr 2006 09:43:40 -0700, we******@gmail. com wrote:
mangesh wrote:
When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?


Neither. It can be passed out to higher scopes, even after the scope
of p ends (so its not on the stack unless its in a scope as high as
main().) You cannot reliably call free on it, or any containing memory
object, meaning its not in the heap.

The memory exists statically. That is, it occupies the same sort of
memory as globals do though the scope may differ. In the real world,
this is usually put into a memory segment called the init segment which
is similar to the data segment, but may have other attributes, such as
being "read-only".


Which is why these things are usually declared and passed around as
"const char*". Bear that in mind, original poster.
Apr 9 '06 #7

mangesh wrote:
When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?

Regards
Mangesh .


I was wondering if the standard actually talks of stack or heap. I
guess not. But again I am playing a guessing game.

I would rather say that the memory is statically linked so you can
access the data even after you have exited the function.

Oh, btw what should my reply be to an interviewer who asks this
question ? I tried reasoning with him (and thats not in one interview
but many) in most polite terms (AFAIK) with above statements but he
would not listen. Please let me know what my response be.

Apr 10 '06 #8
"Jaspreet" <js***********@ gmail.com> writes:
mangesh wrote:
When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?

Regards
Mangesh .
I was wondering if the standard actually talks of stack or heap. I
guess not. But again I am playing a guessing game.


It doesn't.
I would rather say that the memory is statically linked so you can
access the data even after you have exited the function.
I'd say statically allocated, not statically linked.
Oh, btw what should my reply be to an interviewer who asks this
question ? I tried reasoning with him (and thats not in one interview
but many) in most polite terms (AFAIK) with above statements but he
would not listen. Please let me know what my response be.


Storage duration is described in C99 6.2.4.

Many C implementations do use a "stack" (for objects with "automatic
storage duration" and other per-call data) and a "heap" (for objects
with "allocated storage duration"). It's unlikely that such an
implementation would use either stack or heap for string literals.

This probably isn't the best place to ask about interview strategies.
Personally, if I know I'm right about something during a job
interview, I'm not going to back down because the interviewer thinks
he's smarter than I am. (And it's always possible the interviewer is
deliberately testing me.) But I take absolutely no responsibility for
the consequences of treating this observation as advice.

--
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.
Apr 10 '06 #9
Jaspreet wrote:
mangesh wrote:
When we write

char *p = "xyz" ;

Many books state that compiler stores "xyz" in memory and returns
pointer to that memory
which is assigned to p . My question is , in what type of memory ,
heap or stack ?

Regards
Mangesh .


I was wondering if the standard actually talks of stack or heap. I
guess not. But again I am playing a guessing game.

I would rather say that the memory is statically linked so you can
access the data even after you have exited the function.

Oh, btw what should my reply be to an interviewer who asks this
question ? I tried reasoning with him (and thats not in one interview
but many) in most polite terms (AFAIK) with above statements but he
would not listen. Please let me know what my response be.


If you need the job then say its on the stack because you cannot call
free() on it, or any containing object. Then if you get the job
compile the source yourrself, decompile it, then show him that it is in
the init segment. Then point him to documentation about how your OS
loads segments into memory, and that segments are not part of any
"stack".

If you don't need the job that badly, or you reject the job for the
content of this question (and possibly others) then tell him to compile
it up for himself, trace it with a debugger and he will see that it is
neither on the stack, nor in the heap (the address ranges will almost
surely not match up -- and on a standard x86 you will *know* its not on
the stack because its not using stack instructions to access it). If
the interviewer is using the latest MS Visual studio, tell him to try
to execute p[0] = 'x', and ask him which of the heap or the stack he
thinks can contain read-only memory.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Apr 11 '06 #10

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

Similar topics

7
3528
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same time? I thought of a possible solution. However, I am not quite sure if I understood char vs. char*. Any insight is greatly appreciated. Here is the question: ---------------------- ....
24
3142
by: Norm | last post by:
Could someone explain for me what is happening here? char *string; string = new char; string = "This is the string"; cout << string << std::endl; The output contains (This the string) even though I only allocated 5 characters in memory with new.
9
2197
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my attempt at it, but it seems to be lacking... I'm aware that strdup() is nonstandard (and a bad idea for C++ code) - please just bear with me: /* Assume relevant headers are included */ class char_ptr {
22
12734
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous' or even why there could be a problem. here is the code ////////
4
11555
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
2
3398
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has raised a doubt in my mind on the same issue. Either through ignorance or incompetence, I've been unable to resolve some issues. 6.4.4.4p6 states...
5
3952
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
8
10077
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) When I asked why, I was told that it facilitated calling it as:
18
4039
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
20
3529
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
7924
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
7854
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,...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
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
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2364
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
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.