Re: Histogram of character frequencies



On Sun, 2 Dec 2007 06:39:20 -0800 (PST),
rajash@xxxxxxxxxxxxxxxxxxxxxxxx wrote:

James Kuyper wrote:
rajash@xxxxxxxxxxxxxxxxxxxxxxxx wrote:
Johannes Bauer wrote:
rajash@xxxxxxxxxxxxxxxxxxxxxxxx schrieb:

int x[256]; // frequencies
Global.

It's completely acceptable to have variables defined at file scope in
C!

What's acceptable is not always a good idea. Global objects have many
disadvantages; they should be avoided except when necessary; they aren't
necessary in this case.

In this case they help simplify the code - the array gets initialized
to 0 at compile-time, instead of needing extra code for an
initialization loop bad for efficiency!

But you could achieve the same effect without any of the problems that
global variables cause simply by declaring the array static inside the
function.

snip

OK you're right I should remember that. However I don't think it's the
end of the world - the standard library is always linked in so the
right functions will be found in the end by the linker.

The headers have very little to do with what the linker will link in
with your code and everything to do with the compiler generating
correct code. Leaving out stdlib.h and calling malloc introduces
undefined behavior. Leaving out string.h and passing anything other
than a void* or char* to memcpy or memset introduces undefined
behavior.

snip


Remove del for email
.



Relevant Pages

  • Re: [Lit.] Buffer overruns
    ... >>unit boundaries inhibit the complete analysis. ... >>important facts about a program are not reflected in the source code. ... For another, the linker ... trigger undefined behavior as I understand the term. ...
    (sci.crypt)
  • Re: This group
    ... <SNIP> ... commuter train to hell. ... and closed it down due to lack of interest, leaving the last few holdouts ... eternity (a much longer time than most people suppose) wandering from one ...
    (rec.outdoors.fishing.fly)
  • Re: ! Urgent ! Protect yourself and your loved ones...
    ... >She just spent the last 4 hours cleaning the kitchen. ... Ungrateful bastard! ... I am leaving you" ...
    (rec.motorcycles)
  • Re: compiling glib-2.6.x on solaris (long)
    ... >> by monkeying with the linker. ... The special switch you're referring to is ... The equivalent is not necessary for Solaris ld because by ...
    (comp.unix.solaris)
  • Re: ++ devils
    ... We just tried the following expressions in MSVC one by one:- ... It seems that these are problemswith 'sequence points'. ... Many of the constructs that invoke undefined behavior are just plain ...
    (comp.lang.c)