comp.lang.ada
 help / color / mirror / Atom feed
From: gisle@kondor.ii.uib.no (Gisle S�lensminde)
Subject: Re: bitwise comparators
Date: 2000/01/19
Date: 2000-01-19T00:00:00+00:00	[thread overview]
Message-ID: <slrn88bkqa.s8i.gisle@kondor.ii.uib.no> (raw)
In-Reply-To: yecya9mpsv8.fsf@king.cts.com

In article <yecya9mpsv8.fsf@king.cts.com>, Keith Thompson wrote:
>Jeff Carter <jrcarter010@earthlink.net> writes:
>[...]
>> > With true arrays, do you mean out of bound checking, etc?  This can be done
>> > with _proper_ programming in C!
>> 
>> C does not have arrays; it only has different notations for address
>> arithmetic.
>
>That's a slight exaggeration.  C does have array types and array
>objects.  For example this:
>    int a[10];
>declares a as an array of 10 ints, very much like Ada's
>    A: array(0 .. 9) of Integer;
>It does not, contrary to popular misconception, declare a as a
>pointer.
>
>What often causes confusion is that, in most expression contexts, a
>reference to the name of an array object "decays" to a pointer to the
>array's first element.
>
>C arrays are not first-class types, but they do exist.


The semantics of C array is confusing for a lot of people, and 
they are frequently incorrectly used. C arrays are passed by 
reference to functions, but not if they are placed inside a struct.
I have frequently seen code like this one:

(Which not even gives warning on several compilers, like sunpro CC)

char* dig2hex(char digest[20]){
  int i;
  char hexstr[40];
  /* insert calculation here */

  return hexstr; /* returns a pointer to a local 
		    - may corrupt data later*/
}


This code returns a pointer to a local variable, and since these
data is placed on the stack, they will typically be overwritten
later. Instead you can place the array inside a struct, like this:


typedef struct hexstruct {
  char hexstr[40];
} hex;

hex dig2hex(char digest[20]){
  int i;
  hex tmphex;
  /* insert calculation here */

  return tmphex; /* return the struct including array by copy - ok*/
}

As long as the array is placed inside a struct you get by copy 
semantics, while an array on it's own behaves like a different
notation for a pointer.

A strange behavior here, is that the struct must be typedefed.
Otherwise it won't compile.


--
Gisle S�lensminde ( gisle@ii.uib.no )   

ln -s /dev/null ~/.netscape/cookies




  reply	other threads:[~2000-01-19  0:00 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-15  0:00 bitwise comparators Alexander Van Hecke
2000-01-15  0:00 ` David C. Hoos, Sr.
2000-01-16  0:00 ` DuckE
2000-01-17  0:00   ` Alexander Van Hecke
2000-01-17  0:00     ` Jeff Carter
2000-01-17  0:00       ` Alexander Van Hecke
2000-01-17  0:00         ` David Starner
2000-01-17  0:00           ` Alexander Van Hecke
2000-01-17  0:00             ` David Starner
2000-01-18  0:00             ` Fraser
2000-01-18  0:00               ` Bertrand Augereau
2000-01-19  0:00                 ` Ted Dennison
2000-01-19  0:00                   ` Marin D. Condic
2000-01-19  0:00                     ` Ted Dennison
2000-01-18  0:00             ` Preben Randhol
2000-01-18  0:00           ` Ted Dennison
2000-01-17  0:00         ` Gautier
2000-01-17  0:00           ` Alexander Van Hecke
2000-01-17  0:00             ` David Starner
2000-01-18  0:00             ` Gautier
2000-01-18  0:00           ` Ted Dennison
2000-01-18  0:00         ` Jeff Carter
2000-01-18  0:00           ` Keith Thompson
2000-01-19  0:00             ` Gisle S�lensminde [this message]
2000-01-19  0:00             ` Ole-Hjalmar Kristensen
2000-01-19  0:00             ` Jeff Carter
2000-01-19  0:00               ` Keith Thompson
2000-01-19  0:00               ` David Starner
2000-01-18  0:00         ` Pascal Obry
2000-01-21  0:00         ` Ada vs. C/C++ (was re: bitwise something-or-other) Mark Lundquist
2000-01-21  0:00           ` Mark Lundquist
2000-01-24  0:00           ` Hyman Rosen
2000-01-17  0:00     ` bitwise comparators David C. Hoos, Sr.
2000-01-17  0:00     ` Mike Silva
2000-01-17  0:00       ` Brian Rogoff
2000-02-05  0:00         ` Ashley Deas Eachus
2000-02-05  0:00           ` Jeff Carter
2000-02-06  0:00           ` Andy
2000-02-07  0:00           ` Brian Rogoff
2000-02-09  0:00             ` Robert Iredell Eachus
2000-01-17  0:00       ` Alexander Van Hecke
2000-01-17  0:00         ` Mike Silva
2000-01-18  0:00           ` Charles Hixson
2000-01-17  0:00         ` Gautier
2000-01-17  0:00         ` David Starner
2000-01-17  0:00     ` tmoran
2000-01-17  0:00     ` Matthew Heaney
2000-01-17  0:00     ` Gautier
2000-01-18  0:00       ` Keith Thompson
2000-01-19  0:00         ` Ole-Hjalmar Kristensen
2000-01-18  0:00     ` DuckE
2000-01-18  0:00     ` Ted Dennison
2000-01-16  0:00 ` Matthew Heaney
2000-01-16  0:00 ` Jeff Carter
2000-01-16  0:00 ` Bryce Bardin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox