I am considering purchasing the C/C++ compiler. Does it support arround bounds checking for declared arrays such as double a[5]? Array overwrites for such arrays are not trapped by tools such as purify or valgrind so must be caught by the compiler.
Hi Gary,
Yes, pgcc and pgCC do support array bounds checking when your code is compiled with “-Mbounds”. For example:
% cat array.c
int main() {
double a[5];
int i;
for (i=0; i <= 6; ++i) {
a[i] = 0;
}
}
% pgcc -Mbounds array.c
% a.out
Error: PGC-F-Subscript out of range for array a (array.c: 7)
subscript=5, upper bound=4, dimension=1
Hope this helps,
Mat
could somebody tell me why bound-checking -Mbounds doesn’t work with the following code?
I compile with:
$ pgCC -I/usr/include/x86_64-linux-gnu -Mbounds bounds_check.cpp -o run
I run with
$ ./run 8
if the size of the array is fixed in compile time, then bounds check is working and reports where the bounds where exceeded. But it is highly unlikely that one will ever use fixed in compile-time bounds of his arrays.
double a[10];
But it doesn’t work for arrays allocated:
double* a = new double[10];
So what is the purpose of this feature if it is working only for .001% of the cases?
$ cat bounds_check.cpp
#include <iostream>
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: %s n(=array size)\n", argv[0]);
return 1;
}
int n = int(strtol(argv[1], NULL, 0));
double a[n];
int i;
for (i = 0; i <= n; ++i)
{
a[i] = 1;
}
double sum = 0.0;
for (i = 0; i <= 6; ++i)
{
sum += a[i];
}
printf("sum is: %lf\n", sum);
}
Hi raca,
I’ll need to have a compiler engineer look at why the bounds checking code isn’t being added to the C++ code in this instance. The same code written in C works fine.
- Mat
% cat test.c
#include <stdlib>
#include <stdio>
#include <malloc>
int main(int argc, char** argv)
{
if (argc != 2)
{
printf("usage: %s n(=array size)\n", argv[0]);
return 1;
}
int n = strtol(argv[1], NULL, 0);
double a[n];
int i;
for (i = 0; i <= n; ++i)
{
a[i] = 1;
}
double sum = 0.0;
for (i = 0; i <= 6; ++i)
{
sum += a[i];
}
printf("sum is: %lf\n", sum);
exit(0);
}
% pgcc -Mbounds test.c
% a.out 10
sum is: 7.000000
% a.out 3
Error: PGC-F-Subscript out of range (test.c: 26)
subscript=4, upper bound=3, dimension=1
Hello there,
is there any progress with this issue?
Hi raca,
They determined that is a compiler issue and is being tracked as TPR#18453. No time line yet on when it will be fix but it should be sooner rather than later.
- Mat
Hi,
Is this issue fixed yet? I’m thinking of purchasing the pgi compiler only because it has bounds checking, but don’t want to do so if it’s not yet wokring for c++ code…
Thanks
Mark
Hi Mark,
Thanks for checking. Yes, TPR #18453 was fixed in the 12.4 release. Though, it looks like the engineer who fixed the TPR mislabelled the ticket so it didn’t show-up in our release notes as being fixed.
- Mat
% pgcpp -Mbounds bounds.cpp -V12.3
% a.out 3
sum is: 4.000000
% pgcpp -Mbounds bounds.cpp -V12.4
% a.out 3
Error: PGC-F-Subscript out of range for array _42543_10_a (bounds.cpp: 24)
subscript=4, upper bound=3, dimension=1