473,804 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


"Meaningles s 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.


"Meaningles s 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.********@com Acast.net> wrote in message
news:ZS******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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
353
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 change. My question is that, is it legal to dereference the intermediate pointer (the pointer with less strict alignment that we get after conversion) ? Does this intermediate pointer also point to the same memory location as the pointer (with more...
204
13141
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 = {0,1,2,4,9};
3
2730
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 storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to inhibit warning) keywords.c:60:11: Storage Keywords.Keyword released
3
2156
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 which is part of the "atoms" structure that is passed into the function from the outside. I have not yet found where it is allocated but I'm reasonably sure from other chunks of this code that it was by:
6
2311
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 an instance of class). With the container of STL, this may often result in a vector of smart pointers, or a smart pointer of STL container. Am I making things too complicated or this is an usual implementation? Thanks in advance. zl2k
12
1870
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) { bp = c }
27
2315
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
3523
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
2
2499
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 attempt to dereference the null pointer. A better definition of the function would be: static void showtext(const char **ptr) { if (ptr == NULL) {
0
9711
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
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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
10335
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
9169
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
7633
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...
0
6862
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();...
1
4306
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
3831
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.