Table with negative index.

K

klays

Hi all,

I have created char array, when I use tab[-10] compiler shouts out:
error: size of array 'tab' is negative

I tried to cheat compiler and a little modify this code.

Look at the code:

void check(int lenght, int index) {
char tab[lenght];

tab[index] = 'a';
printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
}

Call:
check(-10, -2);

Output:
Character: a, Sizeof: -10

My questions are:
answer = Is it correct initializing array?

if (YES == answer) {

Why do compiler allow to this instruction?
}
else
{
Why not ?
}

Please, explain me it.

Thanks in advance
Klays
 
P

Pascal J. Bourguignon

Hi all,

I have created char array, when I use tab[-10] compiler shouts out:
error: size of array 'tab' is negative

I tried to cheat compiler and a little modify this code.

Look at the code:

void check(int lenght, int index) {
char tab[lenght];

This will initialize the array at run-time.
tab[index] = 'a';
printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
}

Call:
check(-10, -2);

Output:
Character: a, Sizeof: -10

My questions are:
answer = Is it correct initializing array?
No.

if (YES == answer) {

Why do compiler allow to this instruction?
}
else
{
Why not ?
}

Please, explain me it.

Arrays cannot have negative size.
 
K

klays

Look at the code:
void check(int lenght, int index) {
char tab[lenght];

This is not legal in C++. The size of an array has to be a compile-time
constant. You'll have to ask your compiler vendor what it means.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Hi Pete,

I have tried to compile this code for gcc/g++ version 4.1.1 and I saw
that compiler does not raise objection for g++. Maybe I should set
some compiler flags so that compile clear C++. The second issue is:
how tab array is placed in memory for positive and negative size of
tab ( i.e tab[-6] and tab[6]).

Regards
Klays
 
J

James Kanze

On 2008-04-18 05:45:09 -0400, (e-mail address removed) said:
Look at the code:
void check(int lenght, int index) {
char tab[lenght];
This is not legal in C++. The size of an array has to be a
compile-time constant. You'll have to ask your compiler
vendor what it means.
I have tried to compile this code for gcc/g++ version 4.1.1
and I saw that compiler does not raise objection for g++.
Maybe I should set some compiler flags so that compile clear
C++.

Like most compilers, g++ doesn't compile C++ (only something
vaguely similar) unless you give it special options. In the
case of g++, -std=c++98 -pedantic are necessary, at least.
The second issue is: how tab array is placed in memory for
positive and negative size of tab ( i.e tab[-6] and tab[6]).

The size is an size_t, which is an unsigned type, so a
declaration like "tab[ -6 ]" is going to be one mighty big
array. (Since the memory actually used is on the stack, it's
hard to imagine it not overflowing the stack.)

In C++, of course, the expression must be an integral constant >
0, or the compiler should complain.
 
J

Jim Langston

Hi all,

I have created char array, when I use tab[-10] compiler shouts out:
error: size of array 'tab' is negative

I tried to cheat compiler and a little modify this code.

Look at the code:

I'm guessing here, since most of this is undefined behavior and is a
compiler extention.
void check(int lenght, int index) {
char tab[lenght];

The parameter for an array should be unsigned. You are passing it a signed
value (int). It may be that it is silently converted to unsigned.
tab[index] = 'a';
printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
}

Call:
check(-10, -2);

Most likely this is not producing an array of -10 entries, but 4294967286
entries or so (my math might be off). Whatever -10 is in 2's complement
converted to a signed int. Then it is not checking array element -2 but
4294967294 or so. Your printf is treating the output of sizeof as a signed
int, but it actually produces a size_t which is unsigned int.
Output:
Character: a, Sizeof: -10

My questions are:
answer = Is it correct initializing array?

Define "correct". If you mean a very large array because of the conversion
from signed to unsigned, maybe. Again, it depends on your definition of
"correct".
if (YES == answer) {

Why do compiler allow to this instruction?

Because it is a compiler extention and you are passing a signed value to
something that expects an unsgined value.
 
K

klays

Hello,

You are right. It is really unsigned int type (size_t type)
when we use -std=c++98 -pedantic compiler flag then compiler
raise the alarm. Thanks for detail explanation.

Regards,
Klays
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top