undefined-behavior

Undefined behaviour (?) in C with char arrays

when i try char bla[32] = "foobar"; int i; putchar(bla[i]); with strlen(bla) < i < 32, bla[i] is always \0. but isn't this in fact undefined behaviour, and should be avoided? ...

Operator precedence in j = j++ * ++j

Possible Duplicates: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc) How do we explain the result of the expression (++x)+(++x)+(++x)? Why does the following program print 10? Isn't it supposed to print 12? int j=2; j = j++ * ++j; printf("%d", j); ...

Does explicitly calling destructor result in Undefined Behavior here?

In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code: #include <iostream> using namespace std; class some{ public: ~some() { cout<<"some's destructor"<<endl; } }; int main() { some s; s.~some(); } and the answer is: some's destructor some's destructor I learned f...

Can I use a category to override a method which is itself in category on the superclass?

@interface MySuperclass : NSObject { } @end @interface MySuperclass (MyCategory) - (void)myMethod; @end @interface MySubclass : MySuperclass { } @end @interface MySubclass (MyOtherCategory) - (void)myMethod; @end Is it defined which implementation of -myMethod will be called? Kochan states in Programming in Objective-C that...

Invalidating set of pointers through indirection

Consider the following program. It creates a set of pointer-to-ints, and uses a custom indrect_less comparator that sorts the set by the value of the pointed-to integer. Once this is done, I then change the value of one of the pointed-to integers. Then, it can be seen the order of the set is no longer sorted (I suppose because the set...

Implicit declaration in C

Does the following program invoke Undefined Behaviour in C? int main() { printf("Printf asking: Where is my declaration ?"); } In the above program there is an implicit declaration of printf(), so is the above code fully standard compliant or it just has some implementation specific behaviour? ...

Can UB cause several single-threaded app runs to produce different outputs?

Is it possible for code that meets the following conditions to produce different outputs for each run for the same input? The code is single threaded, though it does link against a thread-safe runtime library. There are no explicit calls to rand() or time() or their friends. There are some heap memory allocations. There might be some...

Is the "struct hack" technically undefined behavior?

What I am asking about is the well known "last member of a struct has variable length" trick. It goes something like this: struct T { int len; char s[1]; }; struct T *p = malloc(sizeof(struct T) + 100); p->len = 100; strcpy(p->s, "hello world"); Because of the way that the struct is laid out in memory, we are able to overlay ...

Is unsigned char a[4][5]; a[1][7]; undefined behavior?

One of the examples of undefined behavior from the C standard reads (J.2): — An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6) If the declaration is changed from int a[4][5] to unsigned char a[4]...

Can we change the value of a constant through pointers?

Possible Duplicate: constants and pointers in C #include <stdio.h> int main() { const int a = 12; int *p; p = &a; *p = 70; } Will it work? ...

++i + ++i + ++i in Java vs C

Possible Duplicate: How do we explain the result of the expression (++x)+(++x)+(++x)? int i=2; i = ++i + ++i + ++i; Which is more correct? Java's result of 12 or C = 13. Or if not a matter of correctness, please elaborate. Thank you. ...

Is i += ++i undefined behavior in C++0x?

I'm very convinced with the explanation I've found that said that i = ++i is not undefined as far as C++0x is concerned, but I'm unable to judge whether the behavior of i += ++i is well-defined or not. Any takers? ...

In C99, is f()+g() undefined or merely unspecified?

I used to think that in C99, even if the side-effects of functions f and g interfered, and although the expression f() + g() does not contain a sequence point, f and g would contain some, so the behavior would be unspecified: either f() would be called before g(), or g() before f(). I am no longer so sure. What if the compiler inlines t...

Strange HTML comment behavior

In my code, I have a form with radio buttons. The first radio button is displayed but the second (Repeat Every Week) is not? If I remove the comment markers then it all works fine. But why is it happening? <!------------> <%= radio_button_tag 'repeat_daily', 'freq' %>Repeat Every Day<br/> <!------------> ...some code... <!-----------> <...

How to implement `memmove` in standard C without an intermediate copy?

From the man page on my system: void *memmove(void *dst, const void *src, size_t len); DESCRIPTION The memmove() function copies len bytes from string src to string dst. The two strings may overlap; the copy is always done in a non-destructive manner. From the C99 standard: 6.5.8.5 When tw...

const_cast and UB

$5.2.11/7 - "[Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1). ]" The wordings of this section (C++03) are surprising to me. What is suprising are tw...

is `x-- > 0 && array[x]` well defined behavior in c++?

can i use x on both sides of a boolean expression when I post-increment it on the left side? the line in question is: if(x-- > 0 && array[x]) { /* … use x … */ } is that defined through the standard? will array[x] use the new value of x or the old one? ...

Stringstream write() problem while "overwriting"

Currently, I have a stringstream called Data. I am seeking to the beginning of the stringstream by using: Data.seekp(0, std::ios::beg); Then, I try writing 2 integers to the first 8 bytes of the stringstream (previously, the first 8 bytes were set to 0) Data.write(reinterpret_cast<char*>(&dataLength),sizeof(int)); Data.write(reinterp...