Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sprintf: anomalous behavior

Reply
Thread Tools

sprintf: anomalous behavior

 
 
Marc Thrun
Guest
Posts: n/a
 
      05-15-2006
lovecreatesbeauty schrieb:
> pemo wrote:
>> Although I certainly know what sizeof(s1) will equate to, I sadly *did* mean
>> sizeof. I'm just being dumber than I normally am lately!

>
> But I wonder why not use strlen? Two sizeofs count two more null chars
> in.
>
> char *s1 = "PIPPO";
> char *s2 = "PLUTO";
> char buffer[strlen(s1) + strlen(s2) + 1];
>
> buffer : PIPPOPLUTO 11
>
> char s1_2[] = "PIPPO" ;
> char s2_2[] = "PLUTO";
> char buffer_2[sizeof(s1_2) + sizeof(s2_2) + 1];
>
> buffer_2: PIPPOPLUTO 13
>
> --
> lovecreatesbeauty
>


Because sizeof evaluates to a compile time constant. This can be used to
declare an array even in non C99 where variable length arrays were
introduced. In pre-C99 implementations when using strlen() you have to
use malloc() to allocate sufficient memory.

--
Marc Thrun
http://www.tekwarrior.de/
 
Reply With Quote
 
 
 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      05-16-2006

Marc Thrun wrote:
> Because sizeof evaluates to a compile time constant. This can be used to
> declare an array even in non C99 where variable length arrays were
> introduced. In pre-C99 implementations when using strlen() you have to
> use malloc() to allocate sufficient memory.


Is it suggested that I had better not to write the most/latest
standard-compliant code for better backwark compatibility? If so, what
is the value of the various computer programming language standards.

 
Reply With Quote
 
 
 
 
Ivan78
Guest
Posts: n/a
 
      05-16-2006
of course, but my question is why this istruction works on digital
while on HPUX no?
thanks
Ivano

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-16-2006
Ivan78 wrote:
> of course, but my question is why this istruction works on digital
> while on HPUX no?


Please provide context. Most people do not use Google and there is no
guarantee that people have easy access (or have ever seen) the post you
are replying to. See the Google section at
http://clc-wiki.net/wiki/Intro_to_clc and the pages it links to.

I'm guessing you are talking about unsigned behaviour. Try crossing the
road wearing a blind fold and ear defenders. If you don't get run over
consider why. The answer is blind luck. See
http://clc-wiki.net/wiki/Undefined_behaviour
Also search the group for "undefined behaviour".
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

Ivan78 wrote:
> of course, but my question is why this istruction works on digital
> while on HPUX no?
> thanks
> Ivano


Please quote context. Read: <http://cfaj.freeshell.org/google/>

In reply to your question: that's the beauty of undefined behaviour. It
needs no reason why, literally /anything/ can happen (look up "demons
flying out of your nose"). Your implementations are not evenrequired to
be consistent (i.e. today it may work in one way, tomorrow it may blow
up a small island).

In real life, if you dig deep into how your two implementations are
designed, you may be able to see the exact mechanism that produces one
behaviour or another. Still, relying on any of this would be a Bad
Thing.

 
Reply With Quote
 
Marc Thrun
Guest
Posts: n/a
 
      05-16-2006
lovecreatesbeauty schrieb:
> Marc Thrun wrote:
>> Because sizeof evaluates to a compile time constant. This can be used to
>> declare an array even in non C99 where variable length arrays were
>> introduced. In pre-C99 implementations when using strlen() you have to
>> use malloc() to allocate sufficient memory.

>
> Is it suggested that I had better not to write the most/latest
> standard-compliant code for better backwark compatibility? If so, what
> is the value of the various computer programming language standards.
>


Many modern C implementations still do not support C99 features, or
support them in a different manner. Therefore it is reasonable to write
code compilable in C99 implementations and pre-C99 implementations for
increased portability.

--
Marc Thrun
http://www.tekwarrior.de/
 
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
Anomalous behaviour when compiling regular expressions? Python 0 03-13-2006 10:32 AM
VERY odd routing behavior when attempting VPN connections over Wifi Robert Gordon Wireless Networking 0 08-25-2005 04:04 PM
Question about behavior of View Available Networks window mszablocky Wireless Networking 4 07-24-2005 08:28 PM
[FF] middle button behavior Gillou Firefox 0 05-01-2004 08:46 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments