PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

Free OpenACC Webinar

does C/C++ compiler support array bounds checking?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
gary_ferland



Joined: 13 Apr 2006
Posts: 2

PostPosted: Thu Apr 13, 2006 5:47 am    Post subject: does C/C++ compiler support array bounds checking? Reply with quote

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.
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6208
Location: The Portland Group Inc.

PostPosted: Thu Apr 13, 2006 7:53 am    Post subject: Reply with quote

Hi Gary,

Yes, pgcc and pgCC do support array bounds checking when your code is compiled with "-Mbounds". For example:
Code:
% 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
Back to top
View user's profile
raca



Joined: 19 Jan 2012
Posts: 2

PostPosted: Thu Jan 19, 2012 8:20 am    Post subject: Array bound checking -Mbounds Reply with quote

could somebody tell me why bound-checking -Mbounds doesn't work with the following code?

I compile with:
Code:
$ pgCC -I/usr/include/x86_64-linux-gnu -Mbounds bounds_check.cpp -o run


I run with
Code:
$ ./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.

Code:
double a[10];


But it doesn't work for arrays allocated:
Code:
double* a = new double[10];


So what is the purpose of this feature if it is working only for .001% of the cases?

Code:

$ 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);
}
Code:
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6208
Location: The Portland Group Inc.

PostPosted: Thu Jan 19, 2012 5:04 pm    Post subject: Reply with quote

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

Code:
% 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
Back to top
View user's profile
raca



Joined: 19 Jan 2012
Posts: 2

PostPosted: Wed Feb 15, 2012 2:05 pm    Post subject: Reply with quote

Hello there,
is there any progress with this issue?
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group