| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Array Problem |
RS> This latter point, about the elements of arrays being set to zero,
RS> was another thing I hadn't heard of before. As I understood it, C
RS> doesn't initialize a local variable when declared, unless it is
RS> explicitly initialized at the same time, or in the case of an
RS> array, implicitly initialized by being assigned a const * to the
RS> area of memory where the area data resides.
Not entirely true. For example:
char some_array[25];
This will allocate 25 chars, WITHOUT initializing it. (Note that an array
of C++ objects, to quickly step off topic, WILL be initialized with its
default constructor in this case.)
However, the line:
int array[25] = { 1, 2, 3, 4 };
will allocate 25 ints, WITH initialization: the first four ints as 1
through 4, the rest to (int)0.
Also, the array has a *COPY* of the data, not a const* to the data area.
This means that:
int i;
for (i = 0; i < 15; ++i)
{
int array[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
array[i] = 999;
print_array(array, 15);
}
will call "print_array()" 15 times, and each time only ONE array
member will be 999. Further, since on protected-mode systems (Win32, OS/2,
unix), the data area is set as read-only, the above would NOT work if the
15's were changed to 10's and the "int array[15]" were changed to
"int* const array" - it would cause an access violation (GPF on
Windows, SYS3175 on OS/2, ...) on the first time through the loop.
RS> As for pointers, I never rely on them having _any_ particular value
RS> (NULL included!) unless I've explicitly set that value myself. Are
RS> you saying that if I have a line like char *whee[10]; that all ten
RS> pointers will be NULL pointers before a call to malloc() or some
RS> such?
Nope - you didn't make ANY initialization effort. Changing it to:
char *whee[10] = {NULL};
will initialize ALL of them to NULL (the first because of explicit
initialization, the rest because of implicit NULL initialization).
RS> Hmm. I bet my compiler issues a warning.
DM> Have you tried it?
RS>
RS> Tried my compiler? Yes, I use it frequently. Tried to see if it
RS> issues a warning in this context? Not yet. I was merely surmising
I was asking the latter. :-)
RS> In case you're wondering why I bother to keep using it, it's
RS> because a) it was free for me, b) I have all the manuals and
RS> original disks, and c) I'm too lazy/cheap to acquire another. :/
Fair enough. :-) However, I'd also suggest getting GCC (DJGPP if you're
on DOS). Before you protest about being forced to make your software
freely available rather than charging (i.e., shareware), you only have to
if you link in GPL'd code (as opposed to LGPL, which the standard C library
is). Generally, this only applies to the stdcpp library, or to the regexp
library - you are free to write even commercial software if you don't use
these libraries (possibly writing clean versions yourself if you need
them).
---
* Origin: Tanktalus' Tower BBS (1:250/102)SEEN-BY: 396/1 632/0 371 633/260 262 267 270 371 634/397 635/506 728 639/252 SEEN-BY: 670/218 @PATH: 250/102 201 99 396/1 633/260 635/506 728 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.