top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Where does store memory of initialized string in C?

+1 vote
640 views

In the following example

int  main()
{
  char *ptr ="shakti";
}

Where does ptr get memory? IN data segment or in stack or code segment ?
Please clarify.

posted May 4, 2014 by Shakti Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

"shakti" would store in Code section since it is read only whereas char *ptr stores in stack section since it comes into picture when main () function gets executed.

answer May 5, 2014 by Harshita
OK understood."shakti" would stores in code segment. But now I have another doubt, when ptr would get memory?  Compile time or Run time
0 votes

This is O/S dependent and not the Language dependent. You must executed code in TurboC which is a compiler running on DOS. In turboC we can modify the string since DOS does not have read only memory.
As far as DevC++ or GCC is concerned They run on Windows and Linus/Unix respectively, thus these have read only memory. The O/S has some of the pages marked as read only. These pages (In Paging System) are assigned specifically for storing the objects which have only read access. In page Map table for each virtual page there are bits assigned by which the access permission for the page is determined. Thus it can be said that for storing string literals ( "shakti") there is a different area in memory assigned and can be in any segment except stack segment(If u take the perspective of segmentation system). Actually the string literals have nothing to do with the segment.

answer May 5, 2014 by Prakash
Similar Questions
+3 votes

I am using gprof on an HP-UX system for a program compiled with HP's native aCC (using the -G option, which is documented as compatible with gprof).

I get this every time I run gprof on even a small sample gmon.out:

$ gprof  gmon.out > gprof.out    
gprof: out of memory allocating ********** bytes after a total of 15552512 bytes

This works on other HPs we have in house, though I don't have access to them to be able to track down why.

Any clues that might point me in the right direction?

4,294,967,256 seems like an awful lot of memory to handle a 8,647,174 byte gmon.out file....

...