Gcc Error Operation On May Be Undefined
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site
Wsequence-point Gcc
About Us Learn more about Stack Overflow the company Business Learn more about werror=sequence-point hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join c sequence point the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up warning “operation on may be undefined” up vote 3 down vote favorite 1 When I use overloading [][] operators in c++ to create a minimal matrix class class matrix { private: vector elems_; size_t nrows_; size_t ncols_; public: T const* operator[] ( size_t const r ) const { return &elems_[r * ncols_]; } T* operator[] ( size_t const r ) { return &elems_[r * ncols_]; } matrix (); matrix ( size_t const nr, size_t const nc ) : elems_( nr * nc ), nrows_( nr ), ncols_( nc ) { } matrix ( size_t const nr, size_t const nc, T const *data) : elems_( nr * nc ), nrows_( nr ), ncols_( nc ) { size_t ptr=0; for (int i=0;ihere for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up why *foo ++= *++foo may be undefined? up vote 2 down vote favorite it should set the current character to next character. For example: while( *foo ) http://stackoverflow.com/questions/21625554/warning-operation-on-variable-may-be-undefined { if(baa(*foo)) *foo++ = *++foo; foo++; } But I get the following errors: error: operation on ‘foo’ may be undefined [-Werror=sequence-point] cc1: all warnings being treated as errors Can anyone explain why that? that isn't valid C syntax? c pointers gcc share|improve this question asked Mar 31 '12 at 0:38 Jack 5,9741878146 1 Search on SO first ("C sequence point" would be a good start). The error message says it all (or most of it): an http://stackoverflow.com/questions/9951993/why-foo-foo-may-be-undefined object cannot be modified twice in the same sequence point in a well-defined manner. This is just how C is defined (or not-well-defined as the case may be). –user166390 Mar 31 '12 at 0:40 I'm not a C programmer, but from the look of things, it looks exactly like what the error says, *foo doesn't have a value to it, i.e. undefined. You can't add something to something that doesn't exist. –Hosh Sadiq Mar 31 '12 at 0:41 You're incrementing 3 times inside the loop. Sure this is what you want? –Niklas B. Mar 31 '12 at 0:42 @Hosh: there's no such thing as an undefined value in C –Niklas B. Mar 31 '12 at 0:43 possible duplicate of Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...) –Oliver Charlesworth Mar 31 '12 at 9:31 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted Let's take a closer look at this expression: *foo++ = *++foo *foo++ evaluates as *(foo++) (postfix ++ has higher precedence than unary *); you take the current value of foo and dereference it, and advance foo as a side effect. *++foo evaluates as *(++foo) (both unary * and ++ have the same precedence, so they are applied left-to-right); you take the value o
warning: operation on 'zero' may be undefined From: Paul Schlie To: Date: Fri, 01 Oct 2004 10:18:36 -0400 Subject: Re: warning: operation on 'zero' http://gcc.gnu.org/ml/gcc/2004-10/msg00024.html may be undefined For my own edification, it's not clear to me https://gcc.gnu.org/ml/gcc-help/2011-04/msg00564.html that the instruction sequence: int zero = 125; zero = (++zero)%3; is ambiguous in any way, as I was under the impression that the left hand side of an assignment expression will be assigned the resulting value of the evaluation of it's right hand side, which unambiguously logically equivalent to: int gcc error zero = 125; zero = ((zero = zero + 1) % 3) ; Where the compiler would hopefully recognize the opportunity to eliminate the unnecessary intermediate assignment of zero = zero + 1, as zero is not declared as being volatile, nor implied due to a lack of a sequence point. What am I missing? Thanks, -paul- On Friday 01 October 2004 03:57, Mathieu gcc error operation Malaterre wrote: > I am trying gcc4, and turning -Wall reveal a strange warning: > > int main() > { > int zero = 125; > zero = (++zero)%3; > } > > g++ -Wall foo.cxx > > foo.cxx: In function `int main()': > foo.cxx:4: warning: operation on 'zero' may be undefined > > What does this mean ? Paul Brook wrote: > You incrementing and assigning to the same variable in one statement, which > invokes undefined behaviour. > The code you wrote can be interpreted as > zero = zero%3; > or > zero = (zero % 3) + 1 > or possibly even something entirely different. Dave Korn wrote: > I'm nitpicking, but considering it's a pre-increment, I'd have said > > zero = (zero + 1) % 3; > or > zero = zero + 1; > > were the interpretations. And the problem is the lack of a sequence point > between them. Giovanni Bajo wrote: > The error message could use a clarification though. Follow-Ups: Re: warning: operation on 'zero' may be undefined From: Andrew Pinski Re: warning: operation on 'zero' may be undef
I'm getting a "operation on xxx may be undefined" error in GCC 4.5.1 that I didn't use to get in 4.3.4 From: Staffan Tjernstrom To: gcc-help at gcc dot gnu dot org Date: Thu, 28 Apr 2011 17:08:55 -0500 Subject: I'm getting a "operation on xxx may be undefined" error in GCC 4.5.1 that I didn't use to get in 4.3.4 The following code is giving a operation on p_parent may be undefined warning using 4.5.1 The warning did not occur on 4.3.4, and although it's probably me being macro-blind, I can't for the life of me see the conflict. Nothing that I can see in the while statement should be modifying p_parent? staffan@bypass:~/src/flop> gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/4.5/lto-wrapper Target: x86_64-suse-linux Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.5 --enable-ssp --disable-libssp --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --program-suffix=-4.5 --enable-linux-futex --without-system-libunwind --enable-gold --with-plugin-ld=/usr/bin/gold --with-arch-32=i586 --with-tune=generic --build=x86_64-suse-linux Thread model: posix gcc version 4.5.1 20101208 [gcc-4_5-branch revision 167585] (SUSE Linux) #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3)) #define rb_color(r) ((r)->rb_parent_color & 1) #define rb_is_red(r) (!rb_color(r)) #define rb_is_black(r) rb_color(r) #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0) #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0) #define PHYSICAL_ADDR( r ) ((struct rb_node *)((size_t)(r)+(size_t)((r)?p_base:0))) #define RELATIVE_ADDR( r ) ((struct rb_node *)((size_t)(r)-(size_t)((r)?p_base:0))) static void rb_insert_color(struct rb_node *p_node, struct rb_root *p_root) { struct rb_node *p_phys_node = PHYSICAL_ADDR(p_node); struct rb_node *p_parent, *p_phys_parent, *p_grandparent, *p_phys_grandparent; while ( ( p_phys_parent = ( PHYSICAL_ADDR( p_parent = rb_parent( p_phys_node ) ) ) ) && rb_is_red( p_phys_parent ) ) { } Follow-Ups: Re: I'm getting a "operation on xxx may be undefined" error in GCC 4.5.1