M
Mr. Berserker
I was posting stuff to a mailing list when a friend, Prof. Corbessero
and I came up with this one. Perhaps you can help resolve this, or add
anything else worth knowing?? Maybe it should be added to the FAQ for
further reference...
The bounding checks lack is neat. It seems C is the Barry Goldwater of
programming languages. Like like I was saying, passing the array
symbol
(using too much emacs lisp) to 'void foo(char *bar)' means that I have
to
'manually' go thru. I suppose I could use a #define, so the program
knows
where the sections are cut off, if you know what I mean. In the
function
foo, if I were to say 'printf("%s", (bar + 1));' I'd get 'oo'. Head
down
to the right spot in the array, past the null that ends 'Foo', and
I'll
get the next sequence of chars, I printf and get 'Bar'. So in other
words,
using the 'char *bar' style means I have to go thru, as though the
array
was a a strand of sausages, with the pinches being nulls.
See, when I use that, the pointer is set up so that I can kind of
treat it
like the original array, and not in the 'sausage' fashion above. Try
some
printf's to grok it.
Depends! If you are doing them again and again, that adds up. A macro
might work, but that is bloat in memory as opposed to CPU strain.
Stuff
that you want to run real fast, either because you have to (boss
said),
you want to (you are doing something like FluxBox), or you want to
look
kewl in front of all the FreeBSD and GNU/Linux users of the fairer sex
(why? duh), might want such an optimisation. Sometimes guddy nuff aint
guddy nuff.
See the bit about the sizeof() use. I was aware that it is a pointer.
What
I am asking is: how can I define, within a function, a pointer to a
multidimensional array, that I can treat like a multi-dimensional
array.
If ya don't grok it yet, here are the 'sausage' and 'function' method:
Sausage:
printf("%s", (foo + 1));
Yields 'oo'.
Function:
printf("%s", (foo + 1));
Yields 'Bar'.
printf("%s", ((foo + 1) + 1));
Yields 'ar'. (Actually, my printf syntax in the last bit might be
wrong. I
could nonchalantly d'l an illegal copy of Visual C at this Windoze box
but I don't trespass into people's yards to steal pieces of stinking,
fly-eaten dog
sh*t, so I won't illegally grab a copy of VC which is equivalent
thereto, and I think I'm above theft unlike M$.)
When actually
Well, yeah. I kinda figured that'n.
--
Top Ten Things Not to Say to a Girl, #3:
"compared to most emacs modes, this keymap you have defined for me
really
bites the bag. perhaps you could make some adjustments and
byte-compile
your e lisp source again? and my hands never got so sticky using
emacs,
why is that? you should try cold booting yourself, that fixes all lisp
problems..."
....and #8:
"STRAITER EMPY IN THE TWENTY-FOURTH-AND-A-HALF CENTURY!!!!"
and I came up with this one. Perhaps you can help resolve this, or add
anything else worth knowing?? Maybe it should be added to the FAQ for
further reference...
Ahh. This is my area... C stores arrays in a form called "column
major" order, which actually means the rows come first (don't ask!).
So, here is what a 3x4 array looks like in memory
-------------------------------------------------------------------------
| 0,0 | 0,1 | 0,2 | 0,3 | 1,0 | 1,1 | 1,2 | 1,3 | 2,0 | 2,1 | 2,2 | 2,3 |
-------------------------------------------------------------------------
When you pass an array to a function, all that is really passed is the
address of the first element. With the above layout in mind, as long
as the function knows how long the rows are (ie, the number of
columns) and the size of the base type of the array, it can always
figure out the address of any one element. Since C does not have
bounds checking on arrays, it doesn't need to know the number of rows.
Since it knows the length of each row and the size of each element, it
can "skip" over the rows until it finds the element you are
requesting.
The bounding checks lack is neat. It seems C is the Barry Goldwater of
programming languages. Like like I was saying, passing the array
symbol
(using too much emacs lisp) to 'void foo(char *bar)' means that I have
to
'manually' go thru. I suppose I could use a #define, so the program
knows
where the sections are cut off, if you know what I mean. In the
function
foo, if I were to say 'printf("%s", (bar + 1));' I'd get 'oo'. Head
down
to the right spot in the array, past the null that ends 'Foo', and
I'll
get the next sequence of chars, I printf and get 'Bar'. So in other
words,
using the 'char *bar' style means I have to go thru, as though the
array
was a a strand of sausages, with the pinches being nulls.
if you want position 2,1
the compiler generates an expression to skip over two rows, which
would be
2 (rows) x 4 (things in each row) x W (width in bytes of the base
type)
then the compiler just skips over one more spot to find the 1 column
in that row.
In the general form, for an N dimension array, you need to specify all
the dimensions except the first.
e.g,
double x[3][4]...[n]
should be passed as
void f(double a[][3][4]...[n]) { }
See, when I use that, the pointer is set up so that I can kind of
treat it
like the original array, and not in the 'sausage' fashion above. Try
some
printf's to grok it.
I am not sure what you are asking in part (b). Function calls are not
that expensive.
Depends! If you are doing them again and again, that adds up. A macro
might work, but that is bloat in memory as opposed to CPU strain.
Stuff
that you want to run real fast, either because you have to (boss
said),
you want to (you are doing something like FluxBox), or you want to
look
kewl in front of all the FreeBSD and GNU/Linux users of the fairer sex
(why? duh), might want such an optimisation. Sometimes guddy nuff aint
guddy nuff.
You are not passing the whole array.
See the bit about the sizeof() use. I was aware that it is a pointer.
What
I am asking is: how can I define, within a function, a pointer to a
multidimensional array, that I can treat like a multi-dimensional
array.
If ya don't grok it yet, here are the 'sausage' and 'function' method:
Sausage:
printf("%s", (foo + 1));
Yields 'oo'.
Function:
printf("%s", (foo + 1));
Yields 'Bar'.
printf("%s", ((foo + 1) + 1));
Yields 'ar'. (Actually, my printf syntax in the last bit might be
wrong. I
could nonchalantly d'l an illegal copy of Visual C at this Windoze box
but I don't trespass into people's yards to steal pieces of stinking,
fly-eaten dog
sh*t, so I won't illegally grab a copy of VC which is equivalent
thereto, and I think I'm above theft unlike M$.)
When actually
defining an array, you must specify all the dimensions so the compiler
can generate code to allocate the correct amount of memory.
Well, yeah. I kinda figured that'n.
--
Top Ten Things Not to Say to a Girl, #3:
"compared to most emacs modes, this keymap you have defined for me
really
bites the bag. perhaps you could make some adjustments and
byte-compile
your e lisp source again? and my hands never got so sticky using
emacs,
why is that? you should try cold booting yourself, that fixes all lisp
problems..."
....and #8:
"STRAITER EMPY IN THE TWENTY-FOURTH-AND-A-HALF CENTURY!!!!"