Sponsored Content
Top Forums Programming -Warray-bounds option to GCC compiler Post 303013096 by bakunin on Wednesday 14th of February 2018 10:57:00 PM
Old 02-14-2018
Quote:
Originally Posted by milhan
If I assign 10000 to index, program give segmentation fault, but still compiles with no warnings. -Warray-bounds switch is on.
OK, now i understand better what your question is about. For reference i modified your program this way:

Code:
#include <iostream>
using namespace std;

int main()
{
        const int index=1000;
        int numbers[8] = {1,2,3,4,5,6,7,8};

        numbers[index] = 999;
        cout << numbers[index] << endl;

        return 0;
}

and used this compiler version:

Code:
# g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.

with the same results.

The problem IMHO is a misunderstanding of runtime-bounds-checking and compile-time bounds-checking. Runtime bounds-checking is included in the frontend (if at all) and this is only done for C but not for C++ according to this source. The link also mentions other options (mudflap, MIRO, valgrind, ...), which i haven't tested at all (i haven't programmed anything worthwhile in a HLL for a long time).

As far as i have understood the GNU compiler suite allows for switching on runtime bounds-checking, which the C-frontend does provide but the g++-frontend does not.

None of the frontends do compile-time bounds-checking (which you seem to be after) and, frankly, i doubt this is possible to do in a general way. The compiler would have to guess at compile-time which values any expression could evaluate to, which not only would need arbitrary computing time but also would need to decide the "Entscheidungsproblem - Wikipedia", which is proven to be undecidable (see also "halting problem in Turing-machines").

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

cc compiler / gcc

:confused: I have a question concerning gcc. IŽd like to install the gcc on my Mac OS X, but when I try to run the configure command I get the following message: floriant% ./configure ./configure: read-only variable: PWD Configuring for a powerpc-apple-darwin5.4 host. *** This configuration... (2 Replies)
Discussion started by: florian.turck
2 Replies

2. Programming

gcc compiler

i write c++ code it run perfectely with g++ compiler but same code when i compile with GCC compiler it gives linker error , followed these linker error /tmp/ccfZtXOQ.o(.text+0x22): In function `main': conf_system.cpp: undefined reference to `operator new(unsigned int)'... (5 Replies)
Discussion started by: munnu
5 Replies

3. UNIX for Dummies Questions & Answers

xl C/C++ compiler to GCC compiler

Hi, we are converting from IBM-AIX(xl c/c++ compiler) to Linux(GCC complier). As a part of this i need to change the CFLAGS. The xl c/c++ complier CFLAGS is CFLAGS := $(CDEBUG) $(PROJECT_INCLUDE_DIRS) $(COBJECT_MODE) -qcpluscmt -qmakedep -qcheck=all \ -qalign=bit_packed $(LINT_FLAGS)... (0 Replies)
Discussion started by: pbattu1
0 Replies

4. AIX

AIX 5.3 gcc compiler

Hi there I've got a problem getting my mysql libraries to work. every time I compile my source code it gives my a compiler error. Cannot find a rule to create target /usr/include/mysql/mysql.h AND /usr/include/mysql/mysql.h: Permission denied Is anyone fimiliar with this error, and can... (1 Reply)
Discussion started by: cipher#1
1 Replies

5. Ubuntu

gcc compiler

where to download gcc compiler for ubuntu? how to install? how to build and run "c programs"? screen shots if possible.....:b::D tutorials too:cool: (5 Replies)
Discussion started by: villanarun
5 Replies

6. Shell Programming and Scripting

gcc compiler

I am using open suse linux. I want to install gcc compiler in my machine. I ahve checked man gcc and man cc. But it's not there. Can someone help me (4 Replies)
Discussion started by: pritish.sas
4 Replies

7. Programming

gcc compiler

Which gcc compiler release had the Arm 9 multicore support?Whether the compiler that used for the single Arm 9 core can be used for its multicore systems ? If gcc not support,please tell me which are the compilers that are available for Arm 9 multicore systems (including commerical).Whether... (0 Replies)
Discussion started by: sujith4u87
0 Replies

8. UNIX for Dummies Questions & Answers

cc compiler and gcc compiler

hi, can we install gcc compiler in unix based OS(sun solar,IBM AIX,HP,etc) and also can we install sun cc compiler in AIX environment and vice versa. and more ..is linux support cc compiler regards Ajay (3 Replies)
Discussion started by: ajaysahoo
3 Replies

9. Programming

A question regarding the gcc compiler ...

It might be a simple one but I have this question bothering me for sometime. When we do a symbol search inside the library directory (i.e. /usr/lib/*) via tools like nm; it takes a while to give us the results. However, its very quick when gcc is invoked to compile a program with the very same... (11 Replies)
Discussion started by: Praveen_218
11 Replies
FBB::Pattern(3bobcat)						  Pattern matcher					     FBB::Pattern(3bobcat)

NAME
FBB::Pattern - Performs RE pattern matching SYNOPSIS
#include <bobcat/pattern> Linking option: -lbobcat DESCRIPTION
Pattern objects may be used for Regular Expression (RE) pattern matching. The class is a wrapper around the regcomp(3) family of functions. By default it uses `extended regular expressions', requiring you to escape multipliers and bounding-characters when they should be inter- preted as ordinary characters (i.e., *, +, ?, ^, $, |, (, ), [, ], {, } should be escaped when used as literal characters). The Pattern class supports the use of the following (Perl-like) special escape sequences:  - indicating a word-boundary d - indicating a digit ([[:digit:]]) character s - indicating a white-space ([:space:]) character w - indicating a word ([:alnum:]) character The corresponding capitals (e.g., W) define the complementary character sets. The capitalized character set shorthands are not expanded inside explicit character-classes (i.e., [ ... ] constructions). So [W] represents a set of two characters: and W. As the backslash () is treated as a special character it should be handled carefully. Pattern converts the escape sequences d s w (and outside of explicit character classes the sequences D S W) to their respective character classes. All other escape sequences are kept as is, and the resulting regular expression is offered to the pattern matching compilation function regcomp(3). This function will again interpret escape sequences. Consequently some care should be exercised when defining patterns containing escape sequences. Here are the rules: o Special escape sequences (like d) are converted to character classes. E.g., --------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- d [[:digit:]] [[:digit:]] 3 --------------------------------------------------------- o Ordinary escape sequences (like x) are kept as-is. E.g., --------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- x x x x --------------------------------------------------------- o To specify a literal escape sequence, it must be written twice. E.g., --------------------------------------------------------- Specify: Converts to: regcomp uses: Matches: --------------------------------------------------------- \x \x x x --------------------------------------------------------- NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. INHERITS FROM
- TYPEDEF
o Pattern::Position: A nested type representing the offsets of the first character and the offset beyond the last character of the matched text or indexed subexpression, defined as std::pair<std::string::size_type, std::string::size_type>. CONSTRUCTORS
o Pattern(): The default constructor defines no pattern, but is available as a placeholder for, e.g., containers requiring default constructors. A Pattern object thus constructed cannot be used to match patterns, but can be the lvalue in assignments where another Pattern object is the rvalue. However, it can receive a pattern using the member setPattern() (see below). An FBB::Errno object is thrown if the object could not be constructed. o Pattern(std::string const &pattern, bool caseSensitive = true, size_t nSub = 10, int options = REG_EXTENDED | REG_NEWLINE): This constructor compiles pattern, preparing the Pattern object for pattern matches. The second parameter determines whether case sensitive matching will be used (the default) or not. Subexpressions are defined by parentheses pairs. Each matching pair defines a subexpression, where the order-number of their opening parentheses determines the subexpression's index. By default at most 10 sub- expressions are recognized. The options flags may be: REG_EXTENDED: Use POSIX Extended Regular Expression syntax when interpreting regex. If not set, POSIX Basic Regular Expression syntax is used. REG_NOSUB: Support for substring addressing of matches is not required. The nmatch and pmatch parameters to regexec are ignored if the pattern buffer supplied was compiled with this flag set. REG_NEWLINE: Match-any-character operators don't match a newline. A non-matching list ([^...]) not containing a newline does not match a newline. Match-beginning-of-line operator (^) matches the empty string immediately after a newline, regardless of whether eflags, the execu- tion flags of regexec, contains REG_NOTBOL. Match-end-of-line operator ($) matches the empty string immediately before a newline, regardless of whether eflags contains REG_NOTEOL. Pattern offers copy and move constructors. MEMBER FUNCTIONS
All members of std::ostringstream and std::exception are available, as Pattern inherits from these classes. o std::string before() const: Following a successful match, before() returns the text before the matched text. o std::string beyond() const: Following a successful match, beyond() returns the text beyond the matched text. o size_t end() const: Returns the number of matched elements (text and subexpressions). end() is the lowest index value for which position() returns two std::string::npos values (see the position() member function, below). o void match(std::string const &text, int options = 0): Match a string with a pattern. If the text could not be matched, an Errno exception is thrown , using Pattern::match() as its pre- fix-text. Options may be: REG_NOTBOL: The match-beginning-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above) This flag may be used when different portions of a string are passed to regexec and the beginning of the string should not be interpreted as the beginning of the line. REG_NOTEOL: The match-end-of-line operator always fails to match (but see the compilation flag REG_NEWLINE) o std::string matched() const: Following a successful match, this function returns the matched text. o std::string const &pattern() const: This member function returns the pattern that is offered to regcomp(3). It returns the contents of a static string that is overwrit- ten at each construction of a Pattern object and at each call of the setPattern() member function. o Pattern::Position position(size_t index) const: With index == 0 the fully matched text is returned (identical to matched()). Other index values return the corresponding subexpres- sions. std::string::npos, std::string::npos is returned if index is at least end() (which may happen at index value 0). o void setPattern(std::string const &pattern, bool caseSensitive = true, size_t nSub = 10, int options = REG_EXTENDED | REG_NEWLINE): This member function installs a new compiled pattern in its Pattern object. This member's parameters are identical to the second constructor's parameters. Refer to that constructor for details about the parameters. Like the constructor, an FBB::Errno exception is thrown if the new pattern could not be compiled. o void swap(Pattern &other): The contents of the current object and the other object are swapped. OVERLOADED OPERATORS
o Pattern &operator=(Pattern &other): A standard overloaded assignment operator. o std::string operator[](size_t index) const: Returns the matched text (for index 0) or the text of a subexpression. An empty string is returned for index values which are at least end(). o Pattern &operator<<(int matchOptions): Defines match-options to be used with the following overloaded operator. o bool operator<<(std::string const &text): Performs a match(text, matchOptions) call, catching any exception that might be thrown. If no matchOptions were set using the above overloaded operator, none are used. The options set this way are not `sticky': when necessary, they have to be re-inserted before each new pattern matching. The function returns true if the matching was successful, false otherwise. EXAMPLE
/* driver.cc */ #include "driver.h" #include <bobcat/pattern> using namespace std; using namespace FBB; void showSubstr(string const &str) { static int count = 1; cout << "String " << count++ << " is '" << str << "' "; } int main(int argc, char **argv) { { Pattern one("one"); Pattern two(one); Pattern three("a"); Pattern four; three = two; } try { Pattern pattern("aap|noot|mies"); { Pattern extra(Pattern(pattern)); } if (pattern << "noot") cout << "noot matches "; else cout << ": noot doesn't match "; } catch (Errno const &e) { cout << e.why() << ": compilation failed" << endl; } string pat = "\d+"; while (true) { cout << "Pattern: '" << pat << "' "; try { Pattern patt(pat, argc == 1); // case sensitive by default, // any arg for case insensitive cout << "Compiled pattern: " << patt.pattern() << endl; Pattern pattern; pattern = patt; // assignment operator while (true) { cout << "string to match : "; string st; getline(cin, st); if (st == "") break; cout << "String: '" << st << "' "; try { pattern.match(st); Pattern p3(pattern); cout << "before: " << p3.before() << " " "matched: " << p3.matched() << " " "beyond: " << pattern.beyond() << " " "end() = " << pattern.end() << endl; for (size_t idx = 0; idx < pattern.end(); ++idx) { string str = pattern[idx]; if (str == "") cout << "part " << idx << " not present "; else { Pattern::Position pos = pattern.position(idx); cout << "part " << idx << ": '" << str << "' (" << pos.first << "-" << pos.second << ") "; } } } catch (Errno const &e) { cout << e.why() << ": " << st << " doesn't match" << endl; continue; } } } catch (Errno const &e) { cout << e.why() << ": compilation failed" << endl; } cout << "New pattern: "; if (!getline(cin, pat) || !pat.length()) return 0; } } FILES
bobcat/pattern - defines the class interface SEE ALSO
bobcat(7), regcomp(3), regex(3), regex(7) BUGS
Using Pattern objects as static data members of classes (or as global objects) is potentially dangerous. If the object files defining these static data members are stored in a dynamic library they may not be initialized properly or timely, and their eventual destruction may result in a segmentation fault. This is a well-known problem with static data, see, e.g., http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15. In situations like this prefer the use of a (shared, unique) pointer to a Pat- tern, initialzing the pointer when, e.g., first used. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::Pattern(3bobcat)
All times are GMT -4. The time now is 01:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy