473,411 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Does this invoke undefined behavior?

I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to
the following code. The question is whether the problem is undefined
behavior on the code's part or a compiler bug.

#include <stdlib.h>
#include <stdio.h>

struct frob {
char a;
int b;
};

static struct frob *test;

int frobit(void)
{
const char *move_things = malloc(10*sizeof(struct frob));
struct frob *tmp = realloc(test, 1000*sizeof(struct frob));

move_things = move_things;
if (tmp == NULL) {
fprintf(stderr, "realloc of test failed\n");
exit(1);
}

test = tmp;
return 11;
}

int main(void)
{
int i;

test = malloc(10 * sizeof(struct frob));
if (test == NULL) {
fprintf(stderr, "malloc of test failed\n");
exit(1);
}

for (i = 0; i < 10; i++) {
test[i].b = i;
}

i = 0;
printf("test[0]->b = %d\n", test[0].b);
test[i].b = frobit();
printf("test[0]->b = %d\n", test[0].b);
return 0;
}

OK, what is the problem? In gcc 2.96 the location of test[i].b in the
assignment was figured out after the frobit function returned. In gcc
3.2.3 it is figured out before the frobit function returns. And
the realloc within frobit() moves it.

I've tried figure out in the standard whether the LHS in the assignment
needs to be evaluated before/after the RHS or if that is undefined, but
I can't seem to find that. I see in the FAQ stuff like section 3.1,
does that apply here? Any pointer to the relevant standard section
appreciated.

Yes, yes, statics are evil. Usually. I know, not really relevant here,
not going to change in this case.

Thanks,

-David
Nov 14 '05 #1
1 1811
In <2v*************@uni-berlin.de> David Resnick <no**************@alum.mit.edu> writes:
I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to
the following code. The question is whether the problem is undefined
behavior on the code's part or a compiler bug.
Undefined behaviour, of course.
OK, what is the problem? In gcc 2.96 the location of test[i].b in the
assignment was figured out after the frobit function returned. In gcc
3.2.3 it is figured out before the frobit function returns. And
the realloc within frobit() moves it.

I've tried figure out in the standard whether the LHS in the assignment
needs to be evaluated before/after the RHS or if that is undefined, but
I can't seem to find that.
You won't find it, it's covered by the more general statement that the
order of evaluation of an expression is *unspecified*. If a certain
evaluation order renders the behaviour of your code undefined, your code
is considered as invoking undefined behaviour. And, since your realloc
call can move the resized memory block, all pointers inside the original
block have indeterminate values when realloc returns. By the wording of
C99 they have indeterminate values even if the block is NOT moved by
realloc:

The value of a pointer becomes indeterminate when
the object it points to reaches the end of its lifetime.
....
2 The realloc function deallocates the old object pointed to by
^^^^^^^^^^^^^^^^^^^^^^^^^^
ptr and returns a pointer to a new object that has the size
^^^^^^^^^^^^^^^
specified by size.
Yes, yes, statics are evil. Usually. I know, not really relevant here,
not going to change in this case.


Your code is broken and it's entirely up to you to decide how to fix it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
23
by: centurian | last post by:
I have compiled following program with g++/gcc 3.3.1 and "MS Visual C++ 6" and it ran without any errors. Does this thing make any sense? Is it of some use or some problem with grammar/CFG of...
6
by: Matt Kowalczyk | last post by:
Hello, Can someone explain to me why the following code works correctly? It would seem like it shouldn't. irc_client.c contains: #include "ircclient.h IRCClient* init_irc_client() {
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
5
by: Matt Kowalczyk | last post by:
How come this function doesn't work? void shift(char** list, int index) { char buff = (*list); memmove((*list)+1, *list, index); (*list) = buff; return; } I call it like so:
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
7
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
10
by: subramanian100in | last post by:
Consider the following code: #include <iostream> #include <cstdlib> using namespace std; int main() { const double& ref = 100;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.