Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > character assignment

Reply
Thread Tools

character assignment

 
 
Richard Tobin
Guest
Posts: n/a
 
      01-02-2007
In article <(E-Mail Removed)>,
David T. Ashley <(E-Mail Removed)> wrote:

>I'm only familiar with:
>
>char *p = "I like coconuts and grapes.";
>
>and it was my understanding that in that case that compiler was assigning a
>pointer into a static string pool.
>
>But I may be wrong there again, too! Beat me up more if I blew that one,
>too!


You are right, in that case the string may be in read-only memory and
(I think) shared with other strings.

-- Richard


--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-02-2007
In article <eneho1$1l50$(E-Mail Removed)>,
Richard Tobin <(E-Mail Removed)> wrote:
>In article <(E-Mail Removed)>,
>David T. Ashley <(E-Mail Removed)> wrote:


>>I'm only familiar with:


>>char *p = "I like coconuts and grapes.";


>>and it was my understanding that in that case that compiler was assigning a
>>pointer into a static string pool.


>You are right, in that case the string may be in read-only memory and
>(I think) shared with other strings.


In C89, literal strings need not be distinct, and need not be writable.
But in C89, I cannot find any indication of the scope or lifetime
of a string literal, so I do not think it is correct to say that
the compiler is necessarily "assigning a pointer into a static string pool".

There must be -something- about the lifetime, as it is well understood
that pointers to literals may be returned and may be stored and passed
around. The common usages are such that string literals must have
a lifetime "as if" equivilent to that of static variables, but if the
compiler can determine that a particular literal's address is not
being squirreled away, I see no documented limitation that would
prevent the compiler from effectively making the string literal local
to a routine -- possibly even coding the contents right into
the instruction space. I have these flashbacks to disassembling
in my Z80 days...
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      01-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:

> In C89, literal strings need not be distinct, and need not be writable.
> But in C89, I cannot find any indication of the scope or lifetime
> of a string literal, so I do not think it is correct to say that
> the compiler is necessarily "assigning a pointer into a static string pool".


C89 (or at least a late draft) says:

A character string literal has static storage duration and
type ``array of char,'' and is initialized with the given
characters.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-03-2007
Walter Roberson wrote:
>

.... snip ...
> prevent the compiler from effectively making the string literal
> local to a routine -- possibly even coding the contents right into
> the instruction space. I have these flashbacks to disassembling
> in my Z80 days...


What makes you think such coding into instructions isn't static
storage?

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


 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-03-2007
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> wrote:
>Walter Roberson wrote:


>... snip ...
>> prevent the compiler from effectively making the string literal
>> local to a routine -- possibly even coding the contents right into
>> the instruction space. I have these flashbacks to disassembling
>> in my Z80 days...


>What makes you think such coding into instructions isn't static
>storage?


a) It wouldn't be a "static pool of strings";
b) In my Z80 days, bank switching was used to extend memory, and
it was not uncommon for a swappable bank to include instructions. It
is debatable as to whether such a situation would be "static
storage" or not.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
WaterWalk
Guest
Posts: n/a
 
      01-05-2007

CptDondo wrote:
> I came across this bit of code:
>
> char devname[4] = "wl0";
> ...
> devname[2]++;
>
> This looks wrong to me.... I am chasing a bad pointer issue in this
> code, and I suspect this is not valid C.
>


Both are valid C. I remember that old C compilers would require this:
static char devname[4] = "wI0";
But it seems that since c89 this restraint no longer exists.

 
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
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
Pointer incompatible type assignment to character. gk245 C Programming 10 04-22-2006 05:37 AM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
Re: Character assignment causes bus error Giuseppe C Programming 1 06-24-2003 08:17 AM



Advertisments