f



"some\0\0\0\0\0\0"

can i use such form of adding zeros to the strings/literals to 'pad' them with zeros ?
or this is not guwranteed to work? need that for 
assuring that strings for example arepadded to
some amount of bytes thus using fast compare which only checks some amount of data and do not bother for checking ends, etc 9you know what i mean)
0
fir
8/13/2015 7:30:19 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

5 Replies
1344 Views

Similar Articles

[PageSpeed] 59

On Thu, 13 Aug 2015 12:30:19 -0700, fir saw fit to publish the following:

> can i use such form of adding zeros to the strings/literals to 'pad'
> them with zeros ?

Yes you can. (I feel SOOO presidential right now)

> or this is not guwranteed to work?

As long as the char-array is big enough, it works.

 need that for assuring that strings
> for example arepadded to some amount of bytes thus using fast compare
> which only checks some amount of data and do not bother for checking
> ends, etc 9you know what i mean)

It should not really make a difference. If it does, you're probably doing 
something wrong.

0
Kleuske
8/13/2015 7:54:53 PM
> can i use such form of adding zeros to the strings/literals to 'pad' them with zeros ?

String literals are supposed to contain the characters you put in
them (plus the trailing \0), and that includes \0's at the end (or
in the middle or at the start).  The literal in the subject: line
should be of length 11, including 7 trailing \0's.

> or this is not guwranteed to work? need that for 

> assuring that strings for example arepadded to
> some amount of bytes thus using fast compare which only checks some amount of data and do not bother for checking ends, etc 9you know what i mean)

I'm really not sure how you are going to gain any speed with this.
If you're counting on doing word-at-a-time (or bigger chunks)
comparisons, remember that string literals aren't guaranteed to be
aligned, and depending on your code, the string you are comparing
against may not be either.


0
gordonb
8/13/2015 8:17:18 PM
W dniu czwartek, 13 sierpnia 2015 22:17:29 UTC+2 u=C5=BCytkownik Gordon Bur=
ditt napisa=C5=82:
> > can i use such form of adding zeros to the strings/literals to 'pad' th=
em with zeros ?
>=20
> String literals are supposed to contain the characters you put in
> them (plus the trailing \0), and that includes \0's at the end (or
> in the middle or at the start).  The literal in the subject: line
> should be of length 11, including 7 trailing \0's.
>=20
alright okay then, though i assume strlen would give only "4" here


> > or this is not guwranteed to work? need that for=20
>=20
> > assuring that strings for example arepadded to
> > some amount of bytes thus using fast compare which only checks some amo=
unt of data and do not bother for checking ends, etc 9you know what i mean)
>=20
> I'm really not sure how you are going to gain any speed with this.
> If you're counting on doing word-at-a-time (or bigger chunks)
> comparisons, remember that string literals aren't guaranteed to be
> aligned, and depending on your code, the string you are comparing
> against may not be either.

if i pad some literals up to some minimal value, (like all them padded up t=
o 16 butes and none larger) i can do some fastest compares - but dont matte=
r

as to alignment i dont know, probably in gcc they will be aligned to 4 anyw=
ay (or no?)
even if mislaligned sse compare should work
too (or no?) same as by-int compares..
0
fir
8/13/2015 8:40:17 PM
On Thu, 13 Aug 2015 12:30:19 -0700 (PDT)
fir <profesor.fir@gmail.com> wrote:

> can i use such form of adding zeros to the strings/literals to 'pad'
> them with zeros ? or this is not guwranteed to work? need that for 
> assuring that strings for example arepadded to
> some amount of bytes thus using fast compare which only checks some
> amount of data and do not bother for checking ends, etc 9you know
> what i mean)

Just write your own string structure eg pointer and length. It would be
far faster to check lengths first then compare if equal...
Having you string structure & interface functions has advantage that
you can perform any optimizations you like. 
0
Melzzzzz
8/13/2015 8:53:12 PM
On 13/08/15 22:40, fir wrote:
> W dniu czwartek, 13 sierpnia 2015 22:17:29 UTC+2 użytkownik Gordon
> Burditt napisał:
>>> can i use such form of adding zeros to the strings/literals to
>>> 'pad' them with zeros ?
>> 
>> String literals are supposed to contain the characters you put in 
>> them (plus the trailing \0), and that includes \0's at the end (or 
>> in the middle or at the start).  The literal in the subject: line 
>> should be of length 11, including 7 trailing \0's.
>> 
> alright okay then, though i assume strlen would give only "4" here
> 
> 
>>> or this is not guwranteed to work? need that for
>> 
>>> assuring that strings for example arepadded to some amount of
>>> bytes thus using fast compare which only checks some amount of
>>> data and do not bother for checking ends, etc 9you know what i
>>> mean)
>> 
>> I'm really not sure how you are going to gain any speed with this. 
>> If you're counting on doing word-at-a-time (or bigger chunks) 
>> comparisons, remember that string literals aren't guaranteed to be 
>> aligned, and depending on your code, the string you are comparing 
>> against may not be either.
> 
> if i pad some literals up to some minimal value, (like all them
> padded up to 16 butes and none larger) i can do some fastest compares
> - but dont matter
> 
> as to alignment i dont know, probably in gcc they will be aligned to
> 4 anyway (or no?) even if mislaligned sse compare should work too (or
> no?) same as by-int compares..
> 

No, string literals will not necessarily be aligned - and you don't have
control of their alignment.  (You can force alignment of variables and
consts, by using __attribute__((aligned)), or C11 _Alignas, but AFAIK
you can't change alignment of string literals in gcc.)

But on x86, misaligned accesses are legal even though they are slower
than aligned accesses.
0
David
8/14/2015 6:37:08 AM
Reply: