473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

tricky pointer and string storage question

I have a function called
void f(){
char * tmp = "abc";
static * tmp1 = "abcd";
}

Can anyone tell me
1)whether pointer tmp is stored in stack or heap?
2)whether string "abc" of length 4 bytes is stored in stack or help?
3)whether pointer tmp1 is stored in stack or heap?
4)whether string "abcd" of length 5 bytes is stored in stack or help?

Thanks,

David

Jul 23 '05 #1
8 2712
david wolf wrote:
I have a function called
void f(){
char * tmp = "abc";
static * tmp1 = "abcd";
}

Can anyone tell me
1)whether pointer tmp is stored in stack or heap?
2)whether string "abc" of length 4 bytes is stored in stack or help?
3)whether pointer tmp1 is stored in stack or heap?
4)whether string "abcd" of length 5 bytes is stored in stack or help?


Looks like homework, and the declaration of tmp1 is invalid.
Jul 23 '05 #2
david wolf wrote:
I have a function called
void f(){
char * tmp = "abc";
While this is syntactically correct and semantically allowed, it is
a bad idea to initialise a pointer to non-const char with a literal.
static * tmp1 = "abcd";
The type is missing here. Syntax error.
}

Can anyone tell me
1)whether pointer tmp is stored in stack or heap?
That is unspecified. 'tmp' is _automatic_ and has _automatic_ storage
duration.
2)whether string "abc" of length 4 bytes is stored in stack or help?
Again, this is unspecified.
3)whether pointer tmp1 is stored in stack or heap?
The code doesn't compile, however, corrected to

static char const * tmp1 = "abcd";

it gives 'tmp1' _static_ storage duration. Where it is allocated is,
again, unspecified.
4)whether string "abcd" of length 5 bytes is stored in stack or help?


That's unspecified.

Things like "stack" or "heap" do not really exist in the language. They
are _implementation_details_ and should be talked about in a newsgroup
dedicated to your implementation.

V
Jul 23 '05 #3
* Victor Bazarov:

Things like "stack" or "heap" do not really exist in the language. They
are _implementation_details_ and should be talked about in a newsgroup
dedicated to your implementation.


Do you know of a C++ implementation that where 'auto' is not equivalent
to stack-based, or where ordinary 'new' doesn't allocate from a heap?

Literally you're probably correct, I don't bother checking.

As a matter of practice it is, however, a meaningless distinction.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
Alf P. Steinbach wrote:
* Victor Bazarov:
Things like "stack" or "heap" do not really exist in the language. They
are _implementation_details_ and should be talked about in a newsgroup
dedicated to your implementation.

Do you know of a C++ implementation that where 'auto' is not equivalent
to stack-based, or where ordinary 'new' doesn't allocate from a heap?

Literally you're probably correct, I don't bother checking.

As a matter of practice it is, however, a meaningless distinction.


"Meaningless distinction" between what ant what? Between stack and heap?
In the language Standard the two places where you should find 'stack' and
'heap' are the library templates and functions (namely, 'std::stack' and
'std::make_heap'). There are no other places. Whether or not there exist
implementations that do something different is irrelevant.

BTW, on some operating systems I've encountered, "stack" was actually
allocated on the "heap" [at program loading]. Now, how you attach heap
(where "ordinary 'new'" allocates), to the OP's question, I am not sure.
And how do you answer the question about the literals in terms of stack
and heap I am not sure either because on some systems they can be on the
stack and on some they will be in a constant data segment (not stack or
heap at all). Also, the location of static data and what you call "heap"
(C++ uses the term "free store") are not necessarily the same either.

So, whatever they teach to those kids in the school the OP attends, has
nothing to do with the language itself and everything to do with their
specific implementation of the language on their specific platform. And
we cannot help the OP, he would have to look in the textbooks his teacher
recommended. The validity of those textbooks is beyond the limits of this
thread.

IMNSHO.

V
Jul 23 '05 #5
* Victor Bazarov:
Alf P. Steinbach wrote:
* Victor Bazarov:
Things like "stack" or "heap" do not really exist in the language. They
are _implementation_details_ and should be talked about in a newsgroup
dedicated to your implementation.

Do you know of a C++ implementation that where 'auto' is not equivalent
to stack-based, or where ordinary 'new' doesn't allocate from a heap?

Literally you're probably correct, I don't bother checking.

As a matter of practice it is, however, a meaningless distinction.


"Meaningless distinction" between what ant what? Between stack and heap?


Between 'auto' and "stack-based", and between the area allocated by 'new'
and "heap-based".

In the language Standard the two places where you should find 'stack' and
'heap' are the library templates and functions (namely, 'std::stack' and
'std::make_heap'). There are no other places. Whether or not there exist
implementations that do something different is irrelevant.
There are even more meanings of the words, and nearly all of them as
irrelevant as the two you chose here.

BTW, on some operating systems I've encountered, "stack" was actually
allocated on the "heap" [at program loading].
That, if true, would also be irrelevant. From the program's point of
view there is a stack and how it was allocated isn't an issue at all.

Now, how you attach heap
(where "ordinary 'new'" allocates), to the OP's question, I am not sure.
To refresh your memory: it was mentioned by the OP and by you.

And how do you answer the question about the literals in terms of stack
and heap I am not sure either because on some systems they can be on the
stack and on some they will be in a constant data segment (not stack or
heap at all).
Questions 2, 3 and 4 are meaningless.

Also, the location of static data and what you call "heap"
(C++ uses the term "free store") are not necessarily the same either.
?

First of all, the heap is not a location, it is a set of locations accessed
via a set of functions. Second, static data are not heap-allocated. That's
part of the meaning of "static" (not the C++ keyword).

So, whatever they teach to those kids in the school the OP attends, has
nothing to do with the language itself and everything to do with their
specific implementation of the language on their specific platform. And
we cannot help the OP, he would have to look in the textbooks his teacher
recommended. The validity of those textbooks is beyond the limits of this
thread.


I would be surprised if this turned out to be directly quoted homework.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
Alf P. Steinbach wrote:
[..]
I would be surprised if this turned out to be directly quoted homework.


Really? I wouldn't.
Jul 23 '05 #7
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:ZS*******************@newsread1.mlpsca01.us.t o.verio.net...
Things like "stack" or "heap" do not really exist in the language. They
are _implementation_details_ and should be talked about in a newsgroup
dedicated to your implementation.


Although you are right, I really think you're swimming against the tide on
this. There are numerous references to "stack" and "heap" in the C++ FAQ.
And Stroustrup's TC++PL describes "heap" as a synonym for "free store" and
"on the stack" as a synonym for "auto". Not to mention all the people here
who use "stack" and "heap" in their answers. For better or worse, these
terms are entrenched.

DW
Jul 23 '05 #8

Victor Bazarov wrote:
Alf P. Steinbach wrote:
[..]
I would be surprised if this turned out to be directly quoted
homework.
Really? I wouldn't.


Thanks for your reply. BTW, it's not homework.

Jul 23 '05 #9

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

Similar topics

10
by: junky_fellow | last post by:
K&R say that, It is guaranteed that 1) a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and 2) back again without...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
6
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare...
12
by: bent lee | last post by:
Hi I need help with something thats bugging me for a while. The only way I can get the program to print something is if I do this in the while loop. i = 0; while ((c = getc(fp)) != EOF) {...
27
by: onkar | last post by:
#include<stdio.h> int main(void){ char a="abcde"; char *p=a; p++; p++; p='z'; printf("%s\n",p); return 0; }
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.