-
tmain(intargc, _TCHAR* argv[]){
-
int*p = new int;
-
*p = 0;
-
cout<< *p << endl;}
where will p and *p will be stored? and in what other condition this can be changed to other part in memory like stack,heap,BSS,data,code??
my ans for this would be p stored in heap due to the new command and *p will be stored in BSS since it is initialised to zero. but im not sure whether that is right.
need some conformation..
thanks
8 1830
I agree that the new will generate data from the heap.
As for the pointer, it is located the stack, because it is a local variable to main.
BSS usually refers to global variables that are automatically initialized to a 0 value. In this case, the pointer is initialized to 0, but the pointer is not global.
thank you for your input :D
Banfa 9,065
Expert Mod 8TB @RRick
The pointer is not initialised to zero, the pointer is initialised to an unknown value that is the address of an int that has been allocated on the heap.
The value 0 is then assigned to the int allocated on the heap.
Nothing in this code initialises anything to 0.
On the whole it is not very easy to store data in the code segement (which holds program code) , for a description of the various segements read this and then come back here is you have further questions.
There are some typical rules that are followed by typical compilers for where certain kinds of variables are stored; but I don't believe there are any definitive rules that apply for all C/C++ compilers.
Do you think you're being asked for the typical rules; or do you think you're being asked how some particular odd-ball compiler does it?
if im right does it means...
a) bss stores unitialized global variable and static member
b) data stores the initialized global variable
c) stack is the local variable without static and new/free/malloc//.... command
d) heap is the one with new//malloc...etc command
is that right? so what about text? -
-
#include <string.h>
-
-
char*str; // bss
-
int size; // bss
-
-
void foo(char*in){
-
- // str - heap, *str-stack , str= new char[size]; -> text
-
str= new char[size];
-
-
int sz = strlen(in); // sz,in in stack
-
if (sz>size)
-
strcpy(str,"bye"); // bye in stack
-
else
-
strcpy(str,in);
-
}
-
-
-
thanks again
Banfa 9,065
Expert Mod 8TB @hanna88
You said malloc'd data is in the stack and on the heap. It can't be in 2 places. Ignoring for the moment that there is no requirement for there to be a stack or a heap (I have to say I have yet to com accross an implementation where there wasn't), malloc'd (including data allocated with new) data is normally on the heap (if the implementation has one). @hanna88
The text segment is another name for the code segment, that is it holds program executable code. @hanna88 - You have now indicated that str is on bss and stack your first statement on line 4 is correct the code on line 11 does not change the location of str. The stack contains things automatically allocated when a block of code, such as a function is entered. *str is not automatically created you created it with new. It has been created with new therefore it is on the heap. You do not find data in the text segment as it holds executable code. Also you appear tothink that there are 3 objects in 3 different locations but there are only 2 objects str and *str, they are stored in different places.
- No "bye" is not an automatically created variable, it is a constant. Some platforms (embedded platforms for instance) have a special constant data segment and store this segment in non-modifiable memory so they are truly constant. Most PC platforms (Linux and Windows) just put these into the data segment since they don't have non-modifiable memory. Numerical constants go into the code or text segment, that is they appear directly in the executable code. A variable declared as const would also go into the constant data segment if a platform has one.
A variable declared as const would also go into the constant data segment if a platform has one
can you give me an example for this?
let say the variable is declare in the main func wouldnt it be in stack instead?
thank you for your reply
- str= new char[size];
-
...
-
strcpy(str, "bye");
The string constant "bye" is not an explicit variable in your program. (What is the name of the variable? It has no name.)
Typically, the compiler generates code as if you had written the following. - str = new char[size];
-
...
-
{
-
static const char bye[] = "bye";
-
strcpy(str, bye);
-
}
Some compilers will put the implicit variable bye in a special constant segment, some will put it in the text segment, and some will put it in the data segment. The advantage of putting it in the constant or text segment is that this provides run-time support for const-ness.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mark |
last post by:
Hi - I have set-up security for my users - the security is held in a
text field, separated by a comma.
If the users a member of groups 1, 5 and 6 - the usergroups field is set
to 1,5,6 - to...
|
by: Jonas Rundberg |
last post by:
Hi
I just started with c++ and I'm a little bit confused where stuff
go...
Assume we have a class:
class test {
private:
int arr;
};
|
by: Guadala Harry |
last post by:
Is there any way for one Session to remove and update objects in another
Session? I seriously doubt it, but thought I'd ask. Here's why: I have some
data that is unique per user (or per session -...
|
by: V. Jenks |
last post by:
I have a C# class that generates a string based on 2
integers. One integer is equal to int.MinValue and the
other is equal to int.MaxValue. Each time my class
generates a new string from those 2...
|
by: David |
last post by:
One thing that's always puzzled me about implementing encryption on
remote asp.net apps is where to store the keys. The demo code indicate
that you include them in a configuration file, but this...
|
by: Alan Silver |
last post by:
Hello,
I am just looking at VWD and seeing what needs doing to take an existing
site I've written by hand and importing it into VWD. I've already
discovered that I need to rename my code-behind...
|
by: rdemyan via AccessMonster.com |
last post by:
I'm trying to implement a licensing scheme. There are three types of
licenses:
Trial - good for 30 to 60 days
Interim - good for 1 year
Fully Paid - no expiration
Everything is working fine...
|
by: Mike9900 |
last post by:
Hello,
I would like to store application expiration date in a file and store that
file in a secure place, so the application can access the file for all the
users on that computer.
...
|
by: Miroslaw Makowiecki |
last post by:
Where can I download Comeau compiler as a trial version?
Thanks in advice.
|
by: Smithers |
last post by:
Please consider this humble method:
public void ResetCounters()
{
m_TotalExceptionsDetected = 0;
m_TotalMessagesSent = 0;
}
Given no further information, would you wrap those two lines in a...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |