473,387 Members | 1,812 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,387 software developers and data experts.

strange runtime behaviour with gcc

Hi,

When compiled with gcc 3.3.3 and lower on various systems (tried cygwin,
linux, aix) the following code behaves strangely:

#include <iostream>

class Foo {
public:
typedef int subs[3];
void callme(subs s)
{
std::cout << s[2] << std::endl;
}
};

template<class foo_type>
class Bar
{
public:
void callme(typename foo_type::subs s)
{
std::cout << s[2] << std::endl;
}
};

int main()
{
int sz[]={10,20,30};
Foo f;
f.callme(sz); // should print 30 and it does
Bar<Foo> b;
b.callme(sz); // should print 30 but it does not with gcc
return 0;
}

The problem is that `Bar<Foo>::callme' somehow accesses uninitized space
in memory instead of the one sz[] points to. This is not the case with
any other compiler I had access to (visual c++ 7.1, visualage 6, intel
7 and 8).

Does the above code have semantic problems, or is this just a gcc-bug
(in which case I apologize for this gcc specific and thus off-topic(ish)
thread)?

- L.


Jul 23 '05 #1
5 1287
the code works well on my gcc 3.3.3 after i made the modification
below:
class Foo {
public:
typedef int* subs; //modification here
void callme(subs s)
{
std::cout << s[2] << std::endl;
}
};

interesting , and i want to why

Jul 23 '05 #2

"Levent" <sl**@pitt.edu> wrote in message
news:d7**********@usenet01.srv.cis.pitt.edu...
Hi,

When compiled with gcc 3.3.3 and lower on various systems (tried cygwin,
linux, aix) the following code behaves strangely:
Well, then give gcc the prize becuse the other compilers failed the task.

#include <iostream>

class Foo {
public:
typedef int subs[3];
undefined behaviour, an array object is not an integer nor can it ever be.
typedef is meant to create a new type definition, not to act like a pointee
converter.

<snip>
int main()
{
int sz[]={10,20,30};
Why aren't you encapsulating the array container? Why bother write the
classes Foo and Bar otherwise?
Foo f;
f.callme(sz); // should print 30 and it does
Bar<Foo> b;
b.callme(sz); // should print 30 but it does not with gcc
return 0;
}

The problem is that `Bar<Foo>::callme' somehow accesses uninitized space
in memory instead of the one sz[] points to.


Because subs is passed as an integer to Bar<Foo>callme's parameter, subs is
not an array. A compiler that allows ...

typedef int subs[3];

.... is allowing undefined behaviour. C++ implies strict type checking, not a
particular compiler's version of type redefinitions.

Try the code below in all compilers mentioned above:
Note that an array is not the appropriate container here. A vector would
have been more usefull. Foo's constructor initializes the array's elements.
The member functions callme() only need a reference to the container's
index.

The array is a private member of the Foo type, and the Bar type is
*composed* of a templated member that must support the callme() member
function.

#include <iostream>

class Foo
{
int subs[3];
public:
Foo()
{
std::cout << "Foo ctor invoked\n";
subs[0] = 10;
subs[1] = 20;
subs[2] = 30;
}
void callme(const int& i) const
{
std::cout << subs[i] << std::endl;
}
};

template<class foo_type>
class Bar
{
foo_type t;
public:
Bar() : t() { std::cout << "Bar ctor invoked\n"; }
void callme(const int& i) const
{
t.callme(i);
}
};

int main()
{
Foo f;
f.callme(2); // should print 30

Bar<Foo> b;
b.callme(2); // should print 30

return 0;
}

/* output:

Foo ctor invoked
30
Foo ctor invoked
Bar ctor invoked
30

*/

A vector is a much better choice than an array. Also, if Foo and Bar are
meant to have an "is_a" relationship (a Bar is_a Foo, a Car is_a Vehicle, a
Circle is_a shape), then you should have Bar inherit from Foo instead.

Jul 23 '05 #3
Peter Julian wrote:

A compiler that allows ...

typedef int subs[3];

... is allowing undefined behaviour.


Please review your C++ textbook. That code is correct and
declares that 'subs' is an alias for an array of 3 ints.

Jul 23 '05 #4
Peter Julian wrote:
"Levent" <sl**@pitt.edu> wrote in message
news:d7**********@usenet01.srv.cis.pitt.edu...
Hi,

When compiled with gcc 3.3.3 and lower on various systems (tried cygwin,
linux, aix) the following code behaves strangely:

Well, then give gcc the prize becuse the other compilers failed the task.


If you were right that the behavior of this code is undefined, it
wouldn't follow that any compiler failed in any way. Undefined behavior
is simply undefined. The language definition does not require any
particular behavior, so anything a compiler does is okay.
#include <iostream>

class Foo {
public:
typedef int subs[3];

undefined behaviour, an array object is not an integer nor can it ever be.
typedef is meant to create a new type definition, not to act like a pointee
converter.


The typedef says that 'subs' is an array of 3 ints.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5
Teddy wrote:
interesting , and i want to why

I just found out that this is a bug in gcc which is fixed in the new
release, 4.0.0:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20208

Jul 23 '05 #6

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

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
9
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
0
by: theintrepidfox | last post by:
Dear Group I came accross a very annoying behaviour of Visual Studio, giving me six hours of headache till I found the solution. This post is mainly for fellow developers for reference as it...
14
by: Bo Yang | last post by:
Following is my code: include <iostream> class Test{ public: Test(){}; void print(){ std::cout << "OK" << std::endl ; }; };
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
2
sgeklor
by: sgeklor | last post by:
Hi guys, I have a panel on a form and at runtime I create some controls on the panel. Then, also during runtime I want to clear the panel of all of its controls. The basic way to do this is with...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.