For Programmers: Free Programming Magazines  


Home > Archive > C > October 2007 > pointer concept









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author pointer concept
gsingh

2007-10-29, 3:58 am

Hi All

char *p="str";

How much memory will be allocated for this.

Richard Heathfield

2007-10-29, 3:58 am

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
jk

2007-10-29, 3:58 am

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 <gs.alca...@gmail.com> wrote:
> Hi All
>
> char *p="str";
>
> How much memory will be allocated for this.



Larry__Weiss

2007-10-29, 3:58 am

Richard Heathfield wrote:
> gsingh said:
>
>
> 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
Richard Heathfield

2007-10-29, 3:58 am

Larry__Weiss said:

> Richard Heathfield wrote:
>
> 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
Keith Thompson

2007-10-29, 3:58 am

Richard Heathfield <rjh@see.sig.invalid> writes:
> Larry__Weiss said:
>
> 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) kst-u@mib.org <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"
Ben Bacarisse

2007-10-29, 3:58 am

gsingh <gs.alcatel@gmail.com> 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
Richard Tobin

2007-10-29, 7:57 am

In article <L4SdnUMW0dcB6LjaRVnyjgA@bt.com>,
Richard Heathfield <rjh@see.sig.invalid> 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 Heathfield

2007-10-29, 7:57 am

Richard Tobin said:

> In article <L4SdnUMW0dcB6LjaRVnyjgA@bt.com>,
> Richard Heathfield <rjh@see.sig.invalid> wrote:
>
>
> 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
Philip Potter

2007-10-29, 6:57 pm

Richard Heathfield wrote:
> Richard Tobin said:
>
>
> 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!
Eric Sosman

2007-10-29, 6:57 pm

Larry__Weiss wrote:
> Richard Heathfield wrote:
>
> 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
esosman@ieee-dot-org.invalid
Richard Heathfield

2007-10-29, 6:57 pm

Philip Potter said:

> Richard Heathfield wrote:
>
> 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
Keith Thompson

2007-10-29, 6:58 pm

richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <L4SdnUMW0dcB6LjaRVnyjgA@bt.com>,
> Richard Heathfield <rjh@see.sig.invalid> wrote:
>
> How could you possibly tell? (So long as sizeof returns the expected
> answer.)


By examining the generated code.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"
CBFalconer

2007-10-29, 6:58 pm

Richard Heathfield wrote:
> gsingh said:
>
>
> 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

2007-10-29, 6:58 pm

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

Old Wolf

2007-10-29, 6:58 pm

On Oct 30, 7:29 am, Keith Thompson <ks...@mib.org> wrote:
> rich...@cogsci.ed.ac.uk (Richard Tobin) writes:
>
>
> By examining the generated code.


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

Richard Tobin

2007-10-29, 6:58 pm

In article <lnodeh7p1m.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:

[color=darkred]
[color=darkred]
>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.
Keith Thompson

2007-10-29, 6:58 pm

Old Wolf <oldwolf@inspire.net.nz> writes:
> On Oct 30, 7:29 am, Keith Thompson <ks...@mib.org> wrote:
>
> 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) kst-u@mib.org <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

2007-10-29, 6:58 pm

richard@cogsci.ed.ac.uk (Richard Tobin) writes:
> In article <lnodeh7p1m.fsf@nuthaus.mib.org>,
> Keith Thompson <kst-u@mib.org> wrote:
>
>
>
> 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) kst-u@mib.org <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"
karthikbalaguru

2007-10-29, 9:57 pm

On Oct 29, 1:38 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> gsingh <gs.alca...@gmail.com> writes:
>
>
> 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!


Interesting way of thinking :):)

Karthik Balaguru

Richard Heathfield

2007-10-30, 3:57 am

Keith Thompson said:

> Old Wolf <oldwolf@inspire.net.nz> writes:
<snip>[color=darkred]
>
> If there's no generated code, then the amount of allocated space is
> zero (though it's still the same in the abstract machine).


C can be interpreted. Interpreters do not (necessarily) generate code in
the sense of an object file that you can examine. So do C interpreters run
their programs in 0 memory?

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

2007-10-30, 3:57 am

CBFalconer said:

> Richard Heathfield wrote:
>
> 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".


That's actually a good example of the string literal needing more than four
bytes.

--
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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com