Thread: More information regarding "Data Segment"

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    More information regarding "Data Segment"

    I am going to execute the below program

    Code:
    #include<stdio.h>
    int main
    {
    char *p="howareyou";
    return 0;
    }
    in the above program , where will store the variable "p" ? is it store in "Code segment" or "Data segment" ?

    why it store in data segment, If it store in "Data segment", ?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Neither. p is an automatic local variable. It is automatically created at the start of main and disappears when main is done. It exists on the stack. The string it points to, however, should exist in the data segment. The code segment is just for machine instruction. The data segment is for data, like string literals.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI Thanks for your reply.

    Just I want to know the code segment starting pointing address and ending pointing address. is there any way to find these address details?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nkrao123@gmail.
    Just I want to know the code segment starting pointing address and ending pointing address. is there any way to find these address details?
    Why do you want to know this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Light,

    Code:
    #include<stdio.h>
    int main
    {
    char *p="howareyou";
    printtf ("%u\n",p);
    return 0;
    }
    Actually I fell that "howareyou" would be store in Code(data) segment. while executing the code, i can find the address of "howareyou" by using the printf("%u\n",p).

    so if i know the Code(data) segment starting point and end point, then the valud of "p" should be in between this values of staring and ending.

    Hope you understand my point . Please let me know solutions for this.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Code:
    $ cat foo.c
    #include<stdio.h>
    int main ()
    {
    char *p="aaaaaaaabbbbbbbb";
    return 0;
    }
    $ gcc -c foo.c
    $ size foo.o
       text	   data	    bss	    dec	    hex	filename
         92	      0	      0	     92	     5c	foo.o
    $ nm foo.o
    0000000000000000 T main
    $ objdump -d foo.o
    
    foo.o:     file format elf64-x86-64
    
    
    Disassembly of section .text:
    
    0000000000000000 <main>:
       0:	55                   	push   %rbp
       1:	48 89 e5             	mov    %rsp,%rbp
       4:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
       b:	00 
       c:	b8 00 00 00 00       	mov    $0x0,%eax
      11:	c9                   	leaveq 
      12:	c3                   	retq   
    $ objdump -s foo.o
    
    foo.o:     file format elf64-x86-64
    
    Contents of section .text:
     0000 554889e5 48c745f8 00000000 b8000000  UH..H.E.........
     0010 00c9c3                               ...             
    Contents of section .rodata:
     0000 61616161 61616161 62626262 62626262  aaaaaaaabbbbbbbb
     0010 00                                   .               
    Contents of section .comment:
     0000 00474343 3a202855 62756e74 7520342e  .GCC: (Ubuntu 4.
     0010 342e332d 34756275 6e747535 2920342e  4.3-4ubuntu5) 4.
     0020 342e3300                             4.3.            
    Contents of section .eh_frame:
     0000 14000000 00000000 017a5200 01781001  .........zR..x..
     0010 1b0c0708 90010000 1c000000 1c000000  ................
     0020 00000000 13000000 00410e10 4386020d  .........A..C...
     0030 06000000 00000000                    ........
    Now on my machine, string constants end up in a section for read-only data (.rodata)

    Some other machines would put string constants into the code section.
    Some ancient fossils would put them in the data section.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    nkr, you haven't answered laserlight's question. All you have done is reiterate what you want to do. Laserlight asked you why want to do that.

    Given your obvious ducking of the question, I will assume your reason is the same as others who ask what you have. That reason is generally malicious, unethical, or illegal, such as a desire to write a virus, trojan horse, or a self-modifying (and therefore mutating) executable. And that is against the guidelines of this site. Refer, specifically, to point 6 in the forum guidelines.
    Last edited by grumpy; 02-04-2012 at 12:32 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Salem,

    I goggled some material and understand the below things.

    1. code segment ( mostly it is Read only . often it have readwrite also) ( can you let me know one small example for readwrite scenario?)
    2. data segment
    a. Initialized data segment ( it has read or readwrite)
    b. unInitialized data segment ( it has read or readwrite)
    4. stack
    5. Heap.

    in your above example " char *p="aaaaaaaabbbbbbbb"; " goes to code segment or Data segment ?

    Hi salem,

    I have one more doubt. Till now i know only code, data, stack and heap segments. .rodata is new segment or how it is? pleace can you explain me little bit more about it. i gone through the URL (http://www.bravegnu.org/gnu-eprog/c-startup.html ). but i did not understand much.

    Thanks in advance
    Last edited by nkrao123@gmail.; 02-04-2012 at 01:30 AM.

  9. #9
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi Light/Grumpy,

    Actually my thing is to verify that "howareyou" address is in code segment. Thats why if i know the code segment staring address and ending address. According that i can validate that one as "howareyou" would be in that segment. that is my intention .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am getting "segment fault" for simple program. Can help any one
    By nkrao123@gmail. in forum C Programming
    Replies: 7
    Last Post: 08-29-2011, 08:57 AM
  2. "Segment Violation" error, fopen("r+")
    By jiboso in forum C Programming
    Replies: 1
    Last Post: 03-10-2011, 09:57 AM
  3. "Data Fork"/"Alternate Data Streams"
    By phantomotap in forum Tech Board
    Replies: 3
    Last Post: 08-06-2010, 11:01 AM
  4. char* ptr="HELLO"; String Literal:Stack/Heap/Data Segment
    By forumuser in forum C Programming
    Replies: 9
    Last Post: 09-20-2007, 04:53 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM