Thanks Jerry and "Synthetic" for your responses. On Monday 18 April 2005 21:36, Synthetic Cartoonz wrote:
On Monday 18 April 2005 05:48, Colin Carter wrote:
On Monday 18 April 2005 12:00, Synthetic Cartoonz wrote:
On Sunday 17 April 2005 12:22, Colin Carter wrote:
[snip]
NULL is most correctly used to describe a pointer value, but depending on it's context in the code, NULL could be used as a 0 value char, long, pointer, etc, though some compilers might cough up warnings about type conversions.
snip >
How about: char Buffer[] = "Hello Charlie"; Buffer[5] = '\0' Is this 'stuffing a NUL' ?
Yes. You turned Buffer into the string "Hello".
Or would you do a strncpy() ?
In FORTRAN we have a nice (called obsolete) facility called EQUIVALENCE used like:
snip >
This same memory can be referenced as a character string or an array of integers.
char Buffer[] = "Hello Charlie"; int * danger = (int *) Buffer;
There ya go. Same thing in C. "danger" has to be managed very carefully, or you'll end up dereferencing danger outside of the space of Buffer.
Bingo! Thanks for this - you don't mind if I use it... ;-)
IF this must be done it would be safer to define Buffer to a multiple of the sizeof the largest type that you were doing "equivalence". (Which is basically what you do in your FORTRAN equivalance example: integer 4 * 64 == 256 . character 1 * 256 == 256 .)
Yes, I definitely do this. Jerry also mentioned the correct use of multiples of "natural" units. I think I will use 64 bit integers (on my AMD64) because I think it ought to be faster than 32 bit, and memory is 'cheaper' than cpu speed, and one pays for memory once, but for cpu time every time you run the code.
char Buffer[ sizeof(long) * 5]; long * danger = (long *) Buffer;
strcpy(Buffer, "Hey There Charlie");
assumming 4 byte longs then *danger would point to the value incorporating "Hey ". Depending on the cpu word/byte endian organization that value as a long might be 0x48657920 or some other juggling of those four hex pairs.
And I'm gonna use this info too. I can code blind in FORTRAN, but always a bit nervous in C.
Still, in C or any other language permitting real address pointers this is flirting with doom if you are not careful. But the rewards... However, a long time ago I recall seeing something similar. A C compiler implemented long words as the smallest allocation unit, so the assembly implementation of the standard C libraries could copy, compare, and count multiple bytes at a time resulting in very fast string/memory operations. The string handling code relied on the fact that a flag was set when any byte within the long register was zero, so the code knew when it reached the actual end of the strings. This, of course, is entirely dependent on CPU architecture. I am glad that someone else knows of this. snip
Regards, Colin