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

interface problem in gcc

Hi,
I'm porting a project from Win32 to Linux(debian) and I found a problem
using interfaces.
For simplify the question, I created a small test and here is the code:
#include <stdio.h>
#define interface struct
interface ITest
{
virtual int printHello() = 0;
protected:
virtual ~ITest(){};
};

class CTest : public ITest
{
public:
CTest(){ }
virtual ~CTest() { }
private:
//ITest Interface
int printHello() { printf("Hello\n"); }
};

class CGetter
{
public:
CGetter() { test = new CTest; }
~CGetter() { delete test; }
void getTestPtr(const ITest*& t) const;
private:
CTest * test;
};

void CGetter::getTestPtr(const ITest*& t) const
{
t = test;
return;
}

int main( int argc, char *argv[] )
{
ITest * it;
CGetter g;
g.getTestPtr((const ITest*) it);
it->printHello();
return 0;
}

The problem is with the "it" pointer in main.
Debugging the CGetter::getTestPtr() fnc. I can see:
"test" pointer address = (CTest *) 0x8049c20
after a assignment,
"t" pointer address = (const ITest *&) @0xbffff88c: 0x8049c20
and back in main,
"it" pointer address points to: (ITest *) 0x4023f620
causing segmentation fault.
In Win32 this test works... Any Idea?

Thanks,

Jul 22 '05 #1
6 1397
sojalvo wrote:
class CGetter
{
public:
void getTestPtr(const ITest*& t) const;
}; ITest * it;
CGetter g;
g.getTestPtr((const ITest*) it);
Any compiler accepting this code is wrong: the result of the cast
is a temporary which is bound to a non-const reference. It is illegal
to bind temporaries to non-const references.

Apparently both compilers you tried got this wrong and illegally
compile your code. One chooses to actually pass a reference to
the original pointer (the Windows compiler you are using) and one
opted to create a true temporary object and pass this one to your
function. The net effect is that the assignment in 'getTestPtr()'
had an effect for one compiler but not the other, causing the code
to fail in situation.

In addition, your C style cast is also illegal! You cannot pass
a pointer to a non-const object to a function taking a reference
to a const object: assigning a pointer to a const object to the
reference would yield a non-const access path the object. This is,
of course, illegal! Your compiler would probably have warned you
about this problem if had used appropriate new-style casts (which
would, of course, have prohibited doing what you apparently tried
to do). You should declare your pointer 'it' properly in the first
place:

| ITest const* it = 0;
| CGetter g;
| g.getTestPtr(it);
Debugging the CGetter::getTestPtr() fnc. I can see:
"test" pointer address = (CTest *) 0x8049c20
after a assignment,
"t" pointer address = (const ITest *&) @0xbffff88c: 0x8049c20
and back in main,
"it" pointer address points to: (ITest *) 0x4023f620
causing segmentation fault.
You should have looked at the address of 'it' ('&it'), too: I'd
bet that it gives a different value than '0x4023f620'.
In Win32 this test works... Any Idea?


What do you mean by "works"? The code yields undefined behavior, i.e.
it does whatever it decides to do (of course, the compiler should
have rejected it in the first place). The effect may be what you had
expected but it may also be something entirely different.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #2
sojalvo wrote:
I'm porting a project from Win32 to Linux(debian) and I found a problem
using interfaces.
For simplify the question, I created a small test and here is the code:
#include <stdio.h>
#define interface struct
interface ITest
{
virtual int printHello() = 0;
It probably should be 'const':

virtual int printHello() const = 0;
protected:
virtual ~ITest(){}; ^
Extraneous semicolon.
};

class CTest : public ITest
{
public:
CTest(){ }
virtual ~CTest() { }
private:
//ITest Interface
int printHello() { printf("Hello\n"); }
And here too

int printHello() const { return printf("Hello\n"); }

(and you didn't have a 'return' there)
};

class CGetter
{
public:
CGetter() { test = new CTest; }
Think "initialisation" instead of "assignment".
~CGetter() { delete test; }
And read about the "Rule of Three".
void getTestPtr(const ITest*& t) const;
Why do you think you need the 'const' qualifiers?
private:
CTest * test;
};

void CGetter::getTestPtr(const ITest*& t) const
{
t = test;
return;
}

int main( int argc, char *argv[] )
If you don't use 'argc' and 'argv', there is no sense to have them.
{
ITest * it;
CGetter g;
g.getTestPtr((const ITest*) it);
it->printHello();
return 0;
}

The problem is with the "it" pointer in main.
Debugging the CGetter::getTestPtr() fnc. I can see:
"test" pointer address = (CTest *) 0x8049c20
after a assignment,
"t" pointer address = (const ITest *&) @0xbffff88c: 0x8049c20
and back in main,
"it" pointer address points to: (ITest *) 0x4023f620
causing segmentation fault.
In Win32 this test works... Any Idea?


The problem is in the fact that you probably have the 'test' object
sliced. Drop the 'const's int the getTestPtr and see if it makes
a difference.

V
Jul 22 '05 #3
"sojalvo" <so*****@yahoo.com> wrote in news:1102524964.298582.12290
@z14g2000cwz.googlegroups.com:
Hi,
I'm porting a project from Win32 to Linux(debian) and I found a problem
using interfaces.
No such thing as "interfaces" in Standard C++.
For simplify the question, I created a small test and here is the code:
#include <stdio.h>
Should be #include <cstdio>
#define interface struct
This is probably not a good idea. If you mean struct, why not just say
struct?
interface ITest
{
virtual int printHello() = 0;
protected:
virtual ~ITest(){};
};

class CTest : public ITest
{
public:
CTest(){ }
virtual ~CTest() { }
private:
//ITest Interface
int printHello() { printf("Hello\n"); }
};

class CGetter
{
public:
CGetter() { test = new CTest; }
~CGetter() { delete test; }
void getTestPtr(const ITest*& t) const;
private:
CTest * test;
};

void CGetter::getTestPtr(const ITest*& t) const
{
t = test;
return;
}

int main( int argc, char *argv[] )
{
ITest * it;
CGetter g;
g.getTestPtr((const ITest*) it);
it->printHello();
return 0;
}

The problem is with the "it" pointer in main.
Debugging the CGetter::getTestPtr() fnc. I can see:
"test" pointer address = (CTest *) 0x8049c20
after a assignment,
"t" pointer address = (const ITest *&) @0xbffff88c: 0x8049c20
and back in main,
"it" pointer address points to: (ITest *) 0x4023f620
causing segmentation fault.
In Win32 this test works... Any Idea?


I'm not sure how I can explain it, but you're doing some really funky
things with const and non-const stuff. And I'm thinking that the
compiler may be making a temporary for that casting of "it".

BTW: _Why_ do you want to cast "it"? Why not declare it as a const
pointer to begin with?
Jul 22 '05 #4
Andre Kostur <nn******@kostur.net> wrote in
news:Xn*******************************@207.35.177. 134:
The problem is with the "it" pointer in main.
Debugging the CGetter::getTestPtr() fnc. I can see:
"test" pointer address = (CTest *) 0x8049c20
after a assignment,
"t" pointer address = (const ITest *&) @0xbffff88c: 0x8049c20
and back in main,
"it" pointer address points to: (ITest *) 0x4023f620
causing segmentation fault.
In Win32 this test works... Any Idea?


I'm not sure how I can explain it, but you're doing some really funky
things with const and non-const stuff. And I'm thinking that the
compiler may be making a temporary for that casting of "it".

BTW: _Why_ do you want to cast "it"? Why not declare it as a const
pointer to begin with?


Umm... I meant a pointer to const ITest... not a const pointer....
Jul 22 '05 #5
Andre Kostur wrote:
"sojalvo" <so*****@yahoo.com> wrote:
#include <stdio.h>


Should be #include <cstdio>


I disagree! There are many errors in the code but this is *not* one
of them. The problem is that the real life has proven the approach
of defining C library names in namespace std to hard to implement.
Essentially, you'd need control over the C library: the standard C
library headers typically contain more than just the standard C
declarations. This causes problems without end. As a consequence,
most standard C++ libraries declare standard C names in the global
namespace and import them into namespace std. This practise may
masquarade errors surfacing with conforming implementations. The
same problem does not occur with the ".h" version, effectively
making them a better choice.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #6
Your response helps me!
Thanks very much!

Jul 22 '05 #7

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

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
2
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators ","...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.