C
Christian Christmann
Hi,
I'm wondering if my small example is not
"dangerous":
#define SIZE 10
char global[SIZE];
char* globalPtr = global;
int main()
{
char i;
for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}
I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?
Chris
I'm wondering if my small example is not
"dangerous":
#define SIZE 10
char global[SIZE];
char* globalPtr = global;
int main()
{
char i;
for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}
I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?
Chris