string vs const int memory alloc

Discussion in 'C Programming' started by Vulcan Fire, Apr 12, 2005.

  1. Vulcan Fire

    Vulcan Fire Guest

    Hi

    Why is that

    int *p = 12;
    core dumps

    & not
    char *p = "some_str";

    Shouldnt the int also be written in the data segment??


    Ad Varma
     
    Vulcan Fire, Apr 12, 2005
    #1
    1. Advertising

  2. Vulcan Fire

    Chris Dollin Guest

    Vulcan Fire wrote:

    > Hi
    >
    > Why is that
    >
    > int *p = 12;
    > core dumps


    It should fail to compile. 12 isn't a valid int* value.

    > & not
    > char *p = "some_str";
    >
    > Shouldnt the int also be written in the data segment??


    What's a "data segment"?

    --
    Chris "electric hedgehog" Dollin
     
    Chris Dollin, Apr 12, 2005
    #2
    1. Advertising

  3. On Tue, 12 Apr 2005 03:10:41 -0700, Vulcan Fire wrote:

    > Hi
    >
    > Why is that
    >
    > int *p = 12;
    > core dumps


    This is not valid C, you cannot implicitly convert an integer to a
    pointer, except for the special case of 0 which is a null pointer
    constant. Any C compiler should complain about the line above.

    > & not
    > char *p = "some_str";


    If you had

    int *p = (int *)12;

    you would be convertinbg the value 12 to a pointer type. C doesn't define
    what the result of this will be, it certainly doesn't guarantee that it is
    a valid pointer.

    In the second case you have a string literal. String literals define (i.e.
    create) an object of type array of char. In other words they create
    something legitimate for a pointer to point at. Other constants such as 12
    or 'X' are simply values are don't create their own objects.

    > Shouldnt the int also be written in the data segment??


    C doesn't have a real concept of "data segment". Since 12, and indeed
    (int *)12 are just values they don't exist in objects so storage
    type/location/method is not really applicable. A reasonable compiler might
    well encode values in the instruction stream where it can. However as far
    as C is concerned the only important things are:

    1. is the code valid,

    2. if so what behaviour is required of it.

    The question of how such behaviour is brought about is not important and
    could well vary from one compiler to the next.

    Lawrence
     
    Lawrence Kirby, Apr 12, 2005
    #3
  4. Vulcan Fire

    Darius Guest

    Vulcan Fire wrote:
    > Hi
    >
    > Why is that
    >
    > int *p = 12;
    > core dumps
    >
    > & not
    > char *p = "some_str";
    >
    > Shouldnt the int also be written in the data segment??
    >
    >
    > Ad Varma


    when you write int *p=12;
    compiler should return a warning because you are tryin to converrt an
    integer to pointer without a cast. So if you really want to do this for
    some reason you should cast it as int *p=(int *)12; but then if you do
    something like *p=<integer>, the behaviour.

    but when you write something like char *p="hello hell"
    "hello hell" string literal is stored at some static memory(may be DS)
    and a valid pointer is returned. thus this is a valid syntax.
     
    Darius, Apr 12, 2005
    #4
  5. Vulcan Fire

    Darius Guest

    Vulcan Fire wrote:
    > Hi
    >
    > Why is that
    >
    > int *p = 12;
    > core dumps
    >
    > & not
    > char *p = "some_str";
    >
    > Shouldnt the int also be written in the data segment??
    >
    >
    > Ad Varma


    when you write int *p=12;
    compiler should return a warning because you are tryin to converrt an
    integer to pointer without a cast. So if you really want to do this for
    some reason you should cast it as int *p=(int *)12; but then if you do
    something like *p=<integer>, the behaviour is undefined.

    but when you write something like char *p="hello hell"
    "hello hell" string literal is stored at some static memory(may be DS)
    and a valid pointer is returned. thus this is a valid syntax.
     
    Darius, Apr 12, 2005
    #5
  6. Vulcan Fire wrote:
    > Hi
    >
    > Why is that
    >
    > int *p = 12;
    > core dumps
    >
    > & not
    > char *p = "some_str";
    >
    > Shouldnt the int also be written in the data segment??


    Because "some_str" is a string, and strings actually resolve as pointers
    (anytime a string literal is used in C, it is either interpretted as an
    array or a pointer).

    12 is an integer, and always resolves to 12. There are no instances
    where 12 will resolve to a pointer to valid memory. Just FYI, it DID
    allocate memory in the data segment -- for the pointer, which now has
    the value 12, which probably points to inaccessible memory. If you
    wanted p to point to a valid memory location, you can do

    int i = 12; /* reserve storage for the integer */
    int *p = &i; /* get the pointer to said storage */

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, Apr 12, 2005
    #6
  7. Vulcan Fire

    Vulcan Fire Guest

    got it. thanks!
     
    Vulcan Fire, Apr 19, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    976
    Bob Hairgrove
    Oct 30, 2004
  2. ThazKool
    Replies:
    1
    Views:
    457
  3. Replies:
    11
    Views:
    1,125
  4. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,146
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page