All Questions
Tagged with c99 undefined-behavior
43 questions
2
votes
1
answer
116
views
Is it safe and defined behaviour to cast a pointer to another level of indirection?
I am working with nested sparse arrays of pointers and want to generalise allocation and initialisation of the pointers at each level.
I decided to use a for loop to iterate over each level, however, ...
1
vote
1
answer
122
views
Undefined behavior with pointer casts in C99 and MISRA C:2012
I'm working on a C99 project that requires us to follow the MISRA C:2012 standard.
Rule 11.3 essentially prevents from casting a pointer between different object types without declaring a justified ...
2
votes
2
answers
134
views
Is this use of va_copy undefined behaviour?
I made a function that prints pairs of keys and values, where the keys are trusted compile time literal strings which possibly can contain printf specifiers. Now my question is: is this function legal ...
8
votes
2
answers
223
views
Undefined Behaviour in C99 related to adjusted parameters
I don't understand the following undefined behaviour from C99 standard:
An adjusted parameter type in a function definition is not an object
type (6.9.1)
From the Standard, parameters of a function ...
-1
votes
2
answers
98
views
Meaning and example of Undefined behaviours related to constant expression in C99
I don't understand the Undefined Behaviours in C99 related to constant expression.
For example:
An expression that is required to be an integer constant expression
does not have an integer type; has ...
3
votes
1
answer
926
views
Is a conversion from a double to a float always potentially invoking undefined behaviour?
Here is the relevant part of the standard (6.3.1.5.2 Real floating types) of C99 :
When a double is demoted to float, a long double is demoted to double or
float, or a value being represented in ...
0
votes
1
answer
69
views
Checking whether buffers are interleaved - defined behaviour?
Suppose I need a function that checks whether two arbitrary buffers are interleaved*. One straightforward idea that comes to mind would be this:
bool isInterleaved(uint* buf1, // pointer to first ...
9
votes
0
answers
132
views
When are bitwise operations undefined in C? [duplicate]
This question is about what the C standard(s) require, not what the mainstream compilers can be reliably expected to do. Let's focus on C99, though input about how C89/ANSI or C11 diverge would also ...
-1
votes
2
answers
126
views
Efficient conversion from Indeterminate Value to Unspecified Value
Sometimes in C it is necessary to read a possibly-written item from a partially-written array, such that:
If the item has been written, the read will yield the value that was in fact written, and
If ...
2
votes
2
answers
180
views
Is this use of the Effective Type rule strictly conforming?
The Effective Type rule in C99 and C11 provides that storage with no declared type may be written with any type and, that storing a value of a non-character type will set the Effective Type of the ...
95
votes
3
answers
9k
views
Printing null pointers with %p is undefined behavior?
Is it undefined behavior to print null pointers with the %p conversion specifier?
#include <stdio.h>
int main(void) {
void *p = NULL;
printf("%p", p);
return 0;
}
The question ...
1
vote
1
answer
129
views
General-purpose char buffer used as array of structs with flexible array member
You can't have arrays of structures with flexible array members.
This is the TL;DR of this question. And thinking about it, it makes perfect sense.
However, may one simulate an array of structures ...
2
votes
1
answer
321
views
How to avoid trap representations when doing XOR bit-cancellation on signed ints?
As as suggested solution for Given three numbers, find the second greatest of them, I wrote:
int second_largest(int a, int b, int c) {
int smallest = min(min(a, b), c);
int largest = max(max(a,...
9
votes
3
answers
3k
views
Is negating INT_MIN undefined behaviour?
Let's say I have a variable i that comes from external sources:
int i = get_i();
Assuming i is INT_MIN and two's complement representation, is -i undefined?
4
votes
1
answer
176
views
Is using any indeterminate value undefined or just those stored in objects with automatic storage?
According to C99 J.2, the behavior is undefined when:
The value of an object with automatic storage duration is used while it is
indeterminate
What about all the other cases where an object has ...
3
votes
1
answer
914
views
Where in the C99 standard does it say that signed integer overflow is undefined behavior?
Where in the C99 standard does it say that signed integer overflow is undefined behavior?
I see the comment about unsigned integer overflow being well-defined (see Why is unsigned integer overflow ...
2
votes
4
answers
423
views
Is strcmp(p, "\n") undefined behavior when p points to a character?
Check the min ex:
#include <stdio.h>
#include <string.h>
int main(void) {
char newline = '\n';
char* p = &newline;
if(strcmp(p, "\n") == 0) {
printf("ok\n");
}...
57
votes
5
answers
3k
views
Does a[a[0]] = 1 produce undefined behavior?
Does this C99 code produce undefined behavior?
#include <stdio.h>
int main() {
int a[3] = {0, 0, 0};
a[a[0]] = 1;
printf("a[0] = %d\n", a[0]);
return 0;
}
In the statement a[a[0]] = 1;...
0
votes
1
answer
78
views
Potentially undefined behaviour with restricted pointers
Here are four code fragments. Why is this code guaranteed (or not guaranteed) to produce well defined behaviour?
Restricted "circular references":
struct B;
struct A { struct B *restrict b1, *...
17
votes
5
answers
1k
views
Can "sizeof(arr[0])" lead to undefined behavior?
There is a well known pattern of figuring out array length:
int arr[10];
size_t len = sizeof(arr) / sizeof(arr[0]);
assert(len == 10);
This pattern applies to static arrays and auto arrays of ...
6
votes
3
answers
893
views
Initializer with constant expression having possible overflow in C99
Is this valid C99 code? If so, does it define an implementation-defined behavior?
int a;
unsigned long b[] = {(unsigned long)&a+1};
From my understanding of the C99 standard, from §6.6 in the ...
1
vote
3
answers
124
views
Can I use restrict qualifier in this function?
I read the standard but still cannot be sure:
#include <stdio.h>
#include <string.h>
void repl(char *restrict ap){
char *cp=strchr(ap,(int)'m');
*cp='M';
}
int main(){
char ...
5
votes
1
answer
294
views
Can unsigned integer incrementation lead to undefined defined behavior?
After reading the 32 bit unsigned multiply on 64 bit causing undefined behavior? question here on StackOverflow, I began to ponder whether typical arithmetic operations on small unsigned types could ...
9
votes
1
answer
404
views
Getting address of rvalue in C99
The following code compiles and works:
#include <stdio.h>
void print(void* x)
{
printf("%d", *(int*)x);
}
int main()
{
print(&((struct { int x, y; }){ .x = 1, .y = 2 })); //...
9
votes
3
answers
259
views
Is a goto in alloca's function scope valid?
The C standard prohibits a goto into a function scope where a VLA exists.
A VLA and the call to alloca function should have the same result on low level.
(I could be wrong, as I'm just a C, not a ...
14
votes
4
answers
1k
views
Sequence points and side effects: Quiet change in C11?
C99 §6.5 Expressions
(1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or ...
1
vote
1
answer
56
views
Shouldn't be a Function with return type but without return in body be errnoeous?
#if _OWN_DEBUG_LEVEL > 0
void *GetPostArgs(TYPE *Fcgx_Request, FILE *fpDebugPointer)
#else
void *GetPostArgs(TYPE *Fcgx_Request)
#endif
{
...
if (...)
{
return ...
1
vote
3
answers
321
views
Setting value equal to itself
I don't know where to search for this (probably the standard but still don't know what to search for), so I will ask this here.
If in some execution array[i2] will be set to array[i] where i2 happens ...
1
vote
2
answers
138
views
Could invoking a void statement cause undefined behavior?
Imagine this:
int X;
X = X;
this would be undefined behavior as
1 The behavior is undefined in the following circumstances:
[...]
The value of an object with automatic storage duration is ...
0
votes
3
answers
159
views
The comma operator example in ansi c 1999 TC3
When I was trying to figure out, !0's result is implementation defined expecting that it shall be unequal to zero I just read something what confused me.
(By the way may it be on some implementations ...
-3
votes
1
answer
88
views
Starts static's life time really on program execution?
Does a static variable really exist for the whole program execution?
I know there is no sense in this code snippet, but I'm asking myself, as I understood the c99 standard,
when I'm getting into the ...
0
votes
3
answers
218
views
What happens by modifying read only memory?
Is an identifier qualified by const in every case stored as read only?
Or will it be determined on run time? And what exactly will and/or could happen when I'm going to write into "Read only" memory.
...
3
votes
3
answers
638
views
Skipping switch cases via false loop is a valid operation?
Would this be legal code or breaking any rules?
switch (expr)
{
do
{
case 6:
/*...*/
if (/*...*/)
break;
case 7:
/*...*/
...
0
votes
2
answers
285
views
Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing?
I'm working now for some weeks with c99 focusing undefined behaviour.
I wanted to test some strange code while trying to respect the rules.
The result was this code:
(plz forgive me the variable ...
4
votes
3
answers
172
views
Whats the use of zero sized memblocks?
By the worry of my last days/weeks when I figured out that much of my code does break c99 rules, what is leading into undefined behaviour, I started explicitly reading the ISO/IEC 9899:TC3 draft paper....
9
votes
3
answers
325
views
Undefined behavior: when attempting to access the result of function call
The following compiles and prints "string" as an output.
#include <stdio.h>
struct S { int x; char c[7]; };
struct S bar() {
struct S s = {42, "string"};
return s;
}
int main()
{
...
2
votes
2
answers
117
views
Does this invoke undefined behavior with linkage in C?
From section (6.2.2/7) C99 Standard
7. If, within a translation unit, the same identifier appears with
both internal and external linkage, the behavior is undefined.
While the following ...
5
votes
1
answer
664
views
C99: Is it possible to portably determine if two pointers point within the same aggregate?
In c99, my understanding is that comparing two pointers which do not point within the same aggregate results in undefined behavior. Given an aggregate A, a pointer p_good which is known to point ...
4
votes
1
answer
147
views
On a platform where NULL is represented as 0, has a compiler ever generated unexpected code for NULL <= p
In C99, equality == does not seem ever to be undefined. It can produce 1 by accident if you apply it to invalid addresses (for instance &x + 1 == &y may be true by accident). It does not ...
38
votes
5
answers
10k
views
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.
...
54
votes
3
answers
2k
views
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 ...
5
votes
1
answer
1k
views
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(), ...
2
votes
3
answers
625
views
Order of assignment evaluation (Have I found my first compiler bug?)
This code has an interesting bug:
some_struct struct_array1[10] = {0};
some_struct struct_array2[10] = {0}
int i;
for (i = 0;
i < sizeof(struct_array1) / sizeof(struct_array1[0]);
...