pointer concept

Discussion in 'C Programming' started by gsingh, Oct 29, 2007.

  1. gsingh

    gsingh Guest

    Hi All

    char *p="str";

    How much memory will be allocated for this.
    gsingh, Oct 29, 2007
    #1
    1. Advertising

  2. gsingh said:

    > Hi All
    >
    > char *p="str";
    >
    > How much memory will be allocated for this.


    At least four bytes for the string literal, and sizeof p bytes for the
    pointer.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 29, 2007
    #2
    1. Advertising

  3. gsingh

    jk Guest

    I believe that there will be 4 bytes ('s', 't', 'r', '\0') allocated
    for the string, and sizeof(char*) for the pointer (that will vary on
    different systems) (the size of the pointer, though, has nothing to do
    with where.or how much space the thing it is pointing to is/takes)

    cheers

    On Oct 29, 1:34 am, gsingh <> wrote:
    > Hi All
    >
    > char *p="str";
    >
    > How much memory will be allocated for this.
    jk, Oct 29, 2007
    #3
  4. gsingh

    Larry__Weiss Guest

    Richard Heathfield wrote:
    > gsingh said:
    >
    >> Hi All
    >>
    >> char *p="str";
    >>
    >> How much memory will be allocated for this.

    >
    > At least four bytes for the string literal, and sizeof p bytes for the
    > pointer.
    >


    Why "at least" ? Alignment constraints?
    Does sizeof include any byte-count required because of alignment?

    - Larry
    Larry__Weiss, Oct 29, 2007
    #4
  5. Larry__Weiss said:

    > Richard Heathfield wrote:
    >> gsingh said:
    >>
    >>> Hi All
    >>>
    >>> char *p="str";
    >>>
    >>> How much memory will be allocated for this.

    >>
    >> At least four bytes for the string literal, and sizeof p bytes for the
    >> pointer.
    >>

    >
    > Why "at least" ?


    Because I couldn't remember whether the Standard required the
    implementation to allocate no more memory for string literals than they
    actually need, so "at least" was a safe answer.

    > Alignment constraints?


    I'd have thought that unlikely in this case, but yes, perhaps.

    > Does sizeof include any byte-count required because of alignment?


    No, sizeof yields the exact size of its operand in bytes. (For structs,
    this can include some padding bytes inserted for alignment reasons.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 29, 2007
    #5
  6. Richard Heathfield <> writes:
    > Larry__Weiss said:
    >> Richard Heathfield wrote:
    >>> gsingh said:
    >>>> char *p="str";
    >>>>
    >>>> How much memory will be allocated for this.
    >>>
    >>> At least four bytes for the string literal, and sizeof p bytes for the
    >>> pointer.

    >>
    >> Why "at least" ?

    >
    > Because I couldn't remember whether the Standard required the
    > implementation to allocate no more memory for string literals than they
    > actually need, so "at least" was a safe answer.


    It depends on what you mean by "allocated". The size of the string
    literal itself (more precisely, of the static array object specified
    by the string literal) is exactly 4 bytes. The compiler might reserve
    some additional space before or after it in memory, for whatever
    reason it likes, or for no reason at all. I'd argue that any such
    space is not allocated *for* the string literal, but the standard
    doesn't mention this and it's not a particularly meaningful question.

    [snip]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 29, 2007
    #6
  7. gsingh

    Default User Guest

    Re: pointer concept - TPA

    jk wrote:

    > I believe that there will be 4 bytes ('s', 't', 'r', '\0') allocated



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
    Default User, Oct 29, 2007
    #7
  8. gsingh <> writes:

    > char *p="str";
    >
    > How much memory will be allocated for this.


    Such questions don't have simple answers. If the compiler can detect
    that 'p' is not required, it may not actually allocate any memory at
    all. It is even possible (depending on the context) that the compiler
    might eliminate the string literal!

    You can say that 'p' will require 'sizeof p' bytes to hold its value,
    but even that does not say much about allocated memory. The
    implementation may have to align the variable 'p' in some particular
    way.

    --
    Ben
    Ben Bacarisse, Oct 29, 2007
    #8
  9. In article <>,
    Richard Heathfield <> wrote:

    >Because I couldn't remember whether the Standard required the
    >implementation to allocate no more memory for string literals than they
    >actually need, so "at least" was a safe answer.


    How could you possibly tell? (So long as sizeof returns the expected
    answer.)

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Oct 29, 2007
    #9
  10. Richard Tobin said:

    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>Because I couldn't remember whether the Standard required the
    >>implementation to allocate no more memory for string literals than they
    >>actually need, so "at least" was a safe answer.

    >
    > How could you possibly tell? (So long as sizeof returns the expected
    > answer.)


    I couldn't. And here's another question, while we're in the mood: how could
    I possibly care? :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 29, 2007
    #10
  11. Richard Heathfield wrote:
    > Richard Tobin said:
    >
    >> In article <>,
    >> Richard Heathfield <> wrote:
    >>
    >>> Because I couldn't remember whether the Standard required the
    >>> implementation to allocate no more memory for string literals than they
    >>> actually need, so "at least" was a safe answer.

    >> How could you possibly tell? (So long as sizeof returns the expected
    >> answer.)

    >
    > I couldn't. And here's another question, while we're in the mood: how could
    > I possibly care? :)


    Well, in the extreme case you might be a bit miffed if a 3-character
    (plus null terminator) string literal used all available memory to store
    itself!
    Philip Potter, Oct 29, 2007
    #11
  12. gsingh

    Eric Sosman Guest

    Larry__Weiss wrote:
    > Richard Heathfield wrote:
    >> gsingh said:
    >>
    >>> Hi All
    >>>
    >>> char *p="str";
    >>>
    >>> How much memory will be allocated for this.

    >>
    >> At least four bytes for the string literal, and sizeof p bytes for the
    >> pointer.
    >>

    >
    > Why "at least" ? Alignment constraints?
    > Does sizeof include any byte-count required because of alignment?


    It depends on what your definition of "for" is.

    The implementation is always permitted to use more
    memory, more registers, more CPU time, more pixie dust
    than you might think is "necessary." (It usually has a
    reason that the implementors thought was a good one.)
    The nameless array created for the string literal above
    is four bytes long, but it might be accompanied by more
    memory that is not part of the array. For example, the
    DECC compiler on Alpha stuffed short literals into eight-
    byte slots apart from the memory used for longer strings;
    eight-byte accesses were particularly favored on Alpha.

    However, any such extra memory is not part of the
    anonymous array object. sizeof("str") is four, exactly,
    even if those four bytes wallow in the middle of a forty-
    megabyte sea of wasted memory. So the question of how many
    bytes are allocated for the literal depends on whether your
    idea of "for" means "as a part of" or "on account of," with
    the answers "four" and "at least four," respectively.

    sizeof(anyThingOrType) includes any padding that the
    anyThingOrType needs to ensure its own proper alignment,
    but does not include uncalled-for overhead.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 29, 2007
    #12
  13. Philip Potter said:

    > Richard Heathfield wrote:
    >> Richard Tobin said:
    >>
    >>> In article <>,
    >>> Richard Heathfield <> wrote:
    >>>
    >>>> Because I couldn't remember whether the Standard required the
    >>>> implementation to allocate no more memory for string literals than
    >>>> they actually need, so "at least" was a safe answer.
    >>> How could you possibly tell? (So long as sizeof returns the expected
    >>> answer.)

    >>
    >> I couldn't. And here's another question, while we're in the mood: how
    >> could I possibly care? :)

    >
    > Well, in the extreme case you might be a bit miffed if a 3-character
    > (plus null terminator) string literal used all available memory to store
    > itself!


    Oh, I dunno... it might be worth it, just to have a fresh addition to my
    store of "horrible warning" anecdotes.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 29, 2007
    #13
  14. (Richard Tobin) writes:
    > In article <>,
    > Richard Heathfield <> wrote:
    >>Because I couldn't remember whether the Standard required the
    >>implementation to allocate no more memory for string literals than they
    >>actually need, so "at least" was a safe answer.

    >
    > How could you possibly tell? (So long as sizeof returns the expected
    > answer.)


    By examining the generated code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 29, 2007
    #14
  15. gsingh

    CBFalconer Guest

    gsingh wrote:
    >
    > char *p="str";
    >
    > How much memory will be allocated for this.


    sizeof p

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 29, 2007
    #15
  16. gsingh

    CBFalconer Guest

    Richard Heathfield wrote:
    > gsingh said:
    >
    >> char *p="str";
    >>
    >> How much memory will be allocated for this.

    >
    > At least four bytes for the string literal, and sizeof p bytes
    > for the pointer.


    The literal may not be needed. For example:

    char *o = "First str";
    char *p = "str";

    in which *p may well point to the "str" portion of "First str".

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 29, 2007
    #16
  17. gsingh

    Old Wolf Guest

    On Oct 30, 7:29 am, Keith Thompson <> wrote:
    > (Richard Tobin) writes:
    > > Richard Heathfield <> wrote:
    > >>Because I couldn't remember whether the Standard required the
    > >>implementation to allocate no more memory for string literals than they
    > >>actually need, so "at least" was a safe answer.

    >
    > > How could you possibly tell? (So long as sizeof returns the expected
    > > answer.)

    >
    > By examining the generated code.


    There might not be any generated code, depending on
    which optimisations the compiler is able to make.
    Old Wolf, Oct 29, 2007
    #17
  18. In article <>,
    Keith Thompson <> wrote:

    >>>Because I couldn't remember whether the Standard required the
    >>>implementation to allocate no more memory for string literals than they
    >>>actually need, so "at least" was a safe answer.


    >> How could you possibly tell? (So long as sizeof returns the expected
    >> answer.)


    >By examining the generated code.


    Well yes, but by "you" I mean a conforming program, which is the
    arbiter of standardness (by the "as if" rule).

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Oct 29, 2007
    #18
  19. Old Wolf <> writes:
    > On Oct 30, 7:29 am, Keith Thompson <> wrote:
    >> (Richard Tobin) writes:
    >> > Richard Heathfield <> wrote:
    >> >>Because I couldn't remember whether the Standard required the
    >> >>implementation to allocate no more memory for string literals than they
    >> >>actually need, so "at least" was a safe answer.

    >>
    >> > How could you possibly tell? (So long as sizeof returns the expected
    >> > answer.)

    >>
    >> By examining the generated code.

    >
    > There might not be any generated code, depending on
    > which optimisations the compiler is able to make.


    If there's no generated code, then the amount of allocated space is
    zero (though it's still the same in the abstract machine).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 29, 2007
    #19
  20. (Richard Tobin) writes:
    > In article <>,
    > Keith Thompson <> wrote:
    >>>>Because I couldn't remember whether the Standard required the
    >>>>implementation to allocate no more memory for string literals than they
    >>>>actually need, so "at least" was a safe answer.

    >
    >>> How could you possibly tell? (So long as sizeof returns the expected
    >>> answer.)

    >
    >>By examining the generated code.

    >
    > Well yes, but by "you" I mean a conforming program, which is the
    > arbiter of standardness (by the "as if" rule).


    A conforming program can examine its own machine code. (Remember the
    extremely weak defintion of "conforming program".)

    If you're thinking of strictly conforming programs, the standard
    doesn't limit itself to those. Correct programs must still work
    correctly.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 29, 2007
    #20
    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. a

    pointer and variable scope concept

    a, Jul 9, 2006, in forum: C Programming
    Replies:
    2
    Views:
    325
    Chris Dollin
    Jul 10, 2006
  2. Aff@n
    Replies:
    1
    Views:
    362
    Ian Collins
    Oct 16, 2006
  3. Replies:
    4
    Views:
    1,660
  4. Eric Mahurin

    RCR 304: reference/pointer concept

    Eric Mahurin, May 9, 2005, in forum: Ruby
    Replies:
    2
    Views:
    103
    Austin Ziegler
    May 9, 2005
  5. Iñaki Baz Castillo

    Pointer concept in Ruby ?

    Iñaki Baz Castillo, Apr 24, 2008, in forum: Ruby
    Replies:
    11
    Views:
    269
    Iñaki Baz Castillo
    Apr 25, 2008
Loading...

Share This Page