Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer concept

Reply
Thread Tools

pointer concept

 
 
Philip Potter
Guest
Posts: n/a
 
      10-29-2007
Richard Heathfield wrote:
> Richard Tobin said:
>
>> In article <(E-Mail Removed)>,
>> Richard Heathfield <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-29-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      10-29-2007
Philip Potter said:

> Richard Heathfield wrote:
>> Richard Tobin said:
>>
>>> In article <(E-Mail Removed)>,
>>> Richard Heathfield <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2007
(E-Mail Removed) (Richard Tobin) writes:
> In article <(E-Mail Removed)>,
> Richard Heathfield <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-29-2007
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-29-2007
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

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      10-29-2007
On Oct 30, 7:29 am, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Richard Tobin) writes:
> > Richard Heathfield <(E-Mail Removed)> 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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-29-2007
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2007
Old Wolf <(E-Mail Removed)> writes:
> On Oct 30, 7:29 am, Keith Thompson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) (Richard Tobin) writes:
>> > Richard Heathfield <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2007
(E-Mail Removed) (Richard Tobin) writes:
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer concept in Ruby ? IƱaki Baz Castillo Ruby 11 04-25-2008 09:09 AM
Sonet Pointer justification Concept ekavirsrikanth@gmail.com VHDL 4 03-14-2008 06:51 AM
Problem in the aggregation concept.(during deletion of the pointer of other class). Aff@n C++ 1 10-16-2006 05:08 AM
pointer and variable scope concept a C Programming 2 07-10-2006 08:14 AM
RCR 304: reference/pointer concept Eric Mahurin Ruby 2 05-09-2005 03:27 PM



Advertisments