473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::call me' 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 1303
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.ed u> 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>::call me' 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.ed u> 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
3459
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 print 2.
2
1976
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 std::exception. We have a base class which all processes derive from which is always instantiated in main surrounded by a try/catch(std::exception) which catches all exceptions that have not be handled at a higher level. The catch block cleans up and...
3
4876
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 Developer (SP3 for Office, SP1 for developer, JET40SP8) on Windows XP Home Edition (SP1). The same behaviour occurs on Windows 98 too.
9
1659
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
1429
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 took me ages reading through tons of posts till I found an answer. However, I'm also interested why Visual Studio behaves that way. If anyone has a theorie on it please let me know.
14
1294
by: Bo Yang | last post by:
Following is my code: include <iostream> class Test{ public: Test(){}; void print(){ std::cout << "OK" << std::endl ; }; };
3
2519
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 Runtime>.AddService(trackingService); trackingService.IsTransactional = false; trackingService.UseDefaultProfile = true; This works just fine.
2
1727
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 the following code: For Each this_ctrl As Object In target_panel.Controls this_ctrl.Dispose() Next What happens when I use this code is that only every second control is removed from the panel! Yet, when I replace the .Dispose() method...
8
5321
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.