473,386 Members | 1,698 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,386 software developers and data experts.

where do they store??

33
Expand|Select|Wrap|Line Numbers
  1. tmain(intargc, _TCHAR* argv[]){
  2. int*p = new int;
  3. *p = 0;
  4. 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
Nov 14 '09 #1
8 1830
RRick
463 Expert 256MB
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.
Nov 14 '09 #2
hanna88
33
thank you for your input :D
Nov 14 '09 #3
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.
Nov 14 '09 #4
donbock
2,426 Expert 2GB
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?
Nov 14 '09 #5
hanna88
33
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <string.h>
  3.  
  4. char*str; // bss
  5. int size; // bss
  6.  
  7. void foo(char*in){
  8.  
  9. // str - heap, *str-stack , str= new char[size];  -> text
  10. str= new char[size];  
  11.  
  12. int sz = strlen(in); // sz,in in stack
  13. if (sz>size)
  14.    strcpy(str,"bye"); // bye in stack
  15. else
  16.    strcpy(str,in);
  17. }
  18.  
  19.  
  20.  
thanks again
Nov 14 '09 #6
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
  1. 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.
  2. 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.
Nov 15 '09 #7
hanna88
33
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
Nov 16 '09 #8
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. str= new char[size];
  2. ...
  3. 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.
Expand|Select|Wrap|Line Numbers
  1. str = new char[size];
  2. ...
  3. {
  4.   static const char bye[] = "bye";
  5.   strcpy(str, bye);
  6. }
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.
Nov 16 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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...
17
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; };
4
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 -...
2
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...
1
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...
7
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...
1
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...
10
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. ...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
9
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...
0
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...
0
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...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.