Accessing array by a pointer

  • Thread starter Christian Christmann
  • Start date
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
 
V

Vladimir S. Oka

Christian said:
Hi,

I'm wondering if my small example is not
"dangerous":

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()

Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}

As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.
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?

No. Whichever way you look at it.
 
J

Jirka Klaue

Vladimir S. Oka:
Christian Christmann:
{
char i; [...]
I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.

Which is an integer, see 6.2.5 Types - 4, 6 and 7.
No. Whichever way you look at it.

Right, not even if i were of the integer type int. :)

Jirka
 
P

pemo

Christian said:
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?

Say you altered your code - I've changed i from char to a long int - so
that, in the loop, the value assigned to *globalPtr is /more/ obviously
larger than a char.


#define SIZE 10

char global[SIZE];

char * globalPtr = global;

int main(void)
{
long int i;

for(i = 100000; i < 100009; ++i)
{
*globalPtr = i;

globalPtr++;
}

return 0;
}

On the first iteration, the value of i in binary is 11000011010100000, and
assigning that to a char will result *not* in some memory overwrite, but a
loss of data, e.g. the char at address globalPtr would be perhaps have the
value 10100000, i.e., it got just CHAR_BITs worth of the long.
 
C

Christian Christmann

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()

Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}

As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.

Sorry, 'char i' was supposed to be 'int i' ;-)
 
V

Vladimir S. Oka

Jirka said:
Vladimir S. Oka:
Christian Christmann:
{
char i; [...]
I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.

Which is an integer, see 6.2.5 Types - 4, 6 and 7.

Yes, of course, but if the OP knew that (or where to find it) he
wouldn't be asking the question. Being pedantic has its place and time.
I doubt this is either.
Right, not even if i were of the integer type int. :)

My point exactly.
 
V

Vladimir S. Oka

Christian said:
#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()

Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}

As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.

Sorry, 'char i' was supposed to be 'int i' ;-)

Yet another good reason for oft repeated request for the code to be
pasted in, not typed in. Still, my original reply holds. You're also
advised to read pemo's.
 
P

pete

Christian said:
#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()

Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}

As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the

No, you're not. Your `i` is of type `char`.

Sorry, 'char i' was supposed to be 'int i' ;-)

Since the int values happen to be within the minimum
ranges of char (0 to 127),
there are no complications with the conversion.
Converting an out of range value to type char
is implementation defined.

Assignment is according to value, not representation.
In an assignment operation,
if the left and right operands have different types
then the right operand must be converted to the type
of the left.

If you have

int i = 0; /* or any other int value */
char g;

Then the only difference between

g = i;

and

g = (char)i;

is that the first one is more likely to generate a warning.
The two statements have the same meaning.
They're both expression statements and
both expressions have the same value
and the same side effects.
 
I

Ivanna Pee

Christian said:
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

memset()
 
P

pete

Ivanna said:
Christian said:
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

memset()

memset has nothing to do with the original post.
 
K

Keith Thompson

Christian Christmann said:
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?

A simple assignment, such as your "(*globalPtr) = i;", simply copies a
value (determined by evaluating the right hand side) to an object
(determined by the left hand side). The expression is converted, if
necessary, to the target type; it will never write any data outside
the target object.

You said elsewhere that you meant i to be an int, not a char. That
just means that the value is converted from int to char. There could
be problems with the conversion if the value is to large to fit in a
char, but the worst thing that can happen is that some
implementation-defined value is assigned to the target (or, in C99,
that an implementation-defined signal is raised); it will never
clobber memory outside the target object. (Any problems occur on the
conversion, not on the assignment.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top