Connecting Tech Pros Worldwide Help | Site Map

where do they store??

Member
 
Join Date: Oct 2009
Posts: 33
#1: 1 Week Ago
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
RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#2: 1 Week Ago

re: where do they store??


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.
Member
 
Join Date: Oct 2009
Posts: 33
#3: 1 Week Ago

re: where do they store??


thank you for your input :D
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#4: 1 Week Ago

re: where do they store??


Quote:

Originally Posted by RRick View Post

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.

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.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 830
#5: 1 Week Ago

re: where do they store??


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?
Member
 
Join Date: Oct 2009
Posts: 33
#6: 1 Week Ago

re: where do they store??


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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#7: 1 Week Ago

re: where do they store??


Quote:

Originally Posted by hanna88 View Post

c) stack is the local variable without static and new/free/malloc//.... command
d) heap is the one with new//malloc...etc command

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

Quote:

Originally Posted by hanna88 View Post

is that right? so what about text?

The text segment is another name for the code segment, that is it holds program executable code.

Quote:

Originally Posted by hanna88 View Post

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. // SEE NOTE 1
  11. str= new char[size];  
  12.  
  13. int sz = strlen(in); // sz,in in stack
  14. if (sz>size)
  15.    strcpy(str,"bye"); // SEE NOTE 2 bye in stack
  16. else
  17.    strcpy(str,in);
  18. }

  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.
Member
 
Join Date: Oct 2009
Posts: 33
#8: 1 Week Ago

re: where do they store??


Quote:
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
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 830
#9: 1 Week Ago

re: where do they store??


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