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 10 1611
Christian Christmann wrote: 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.
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. 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.
Right, not even if i were of the integer type int. :-)
Jirka
Christian Christmann wrote: 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.
--
==============
Not a pedant
==============
Jirka Klaue wrote: 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. 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.
Right, not even if i were of the integer type int. :-)
My point exactly.
>> #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' ;-)
Christian Christmann wrote: #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.
Christian Christmann wrote: #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.
--
pete
Christian Christmann wrote: 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()
Ivanna Pee wrote: Christian Christmann wrote: 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.
--
pete
Christian Christmann <pl*****@yahoo.de> writes: 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.)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel Lidström |
last post by:
Hello!
I want to work with individual bytes of integers. I know that ints are
32-bit and will always be. Sometimes I want to work with the entire
32-bits, and other times I want to modify just...
|
by: sakitah |
last post by:
Here is what I have:
struct SubList
{
int BookId;
int WFreq;
};
struct Listing
{
|
by: Thes |
last post by:
Hi all,
I have a 2d float array to which I have declared a pointer thus:
float Matrix = {/* snipped initialisation */ };
float (*StoredMatrix);
So as to create a copy in a program...
|
by: junky_fellow |
last post by:
Consider a 3-dimensional array:
char a = { "abcd", "efgh", "ijkl" ,
"mnop", "qrst", "uvwx"
} ;
when i print the addresses for a,a and a they are same.
but when i access element a+1, a+1 and...
|
by: Dawn Minnis |
last post by:
Hey guys
If I have a program (see codeSnippet1) that I compile to be called test.o
Then run it as test.o n n 2 3 4
I want the code to be able to strip out the two characters at the start...
|
by: archilleswaterland |
last post by:
structures
typedef struct{
char name;
int age;
float balance;
}account;
account xyx;
accout *ptr;
|
by: harsha1305 |
last post by:
Hi all,
I need to create a pointer to array of structure.
Definition of structure:
typedef struct { char b_name;
unsigned long int sig;
unsigned long int count;
volatile unsigned char...
|
by: djhulme |
last post by:
Hi,
I'm using GCC. Please could you tell me, what is the maximum number of
array elements that I can create in C, i.e.
char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ;
I've...
|
by: ...vagrahb |
last post by:
I am having accessing individual rows from a multidimensional array
pass to a function as reference
CODE:
function Declaration
int Part_Buffer(char (*buffer),int Low, int High)
|
by: s0suk3 |
last post by:
This code
#include <stdio.h>
int main(void)
{
int hello = {'h', 'e', 'l', 'l', 'o'};
char *p = (void *) hello;
for (size_t i = 0; i < sizeof(hello); ++i) {
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |