LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2010, 10:28 AM   #1
AmuthaA
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Rep: Reputation: 0
Copy Constructor in GCC4.4


In GCC4.4 the below code gives error:
Suppose i have a class Point and and am using copy constructor in my code and GCC4.4 gives error for it.
1.Point::Point(const Point &p)
2.{
3.x = p.x; //x is member of Point
4.y = p.y;
5.}
6.main()
7.{
8.Point p1;
9.Point p2 = p1;
10.}
The error is :
3: error: dereferencing pointer '<anonymous>' does break strict-aliasing
4: error: dereferencing pointer '<anonymous>' does break strict-aliasing

Its happen only in GCC4.4
Please help to solve this.
 
Old 04-23-2010, 10:45 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Do you have a small entire compilation unit (something someone else could compile and duplicate your result) that shows this problem?

The error message you quoted looks unlikely for the code you posted.
 
Old 04-23-2010, 02:27 PM   #3
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
dose it build using gcc 4.1 ,4.2 , 4.3 ???
if so you might want to read the gcc porting page
gcc 4.3
http://gcc.gnu.org/gcc-4.3/porting_to.html
gcc 4.4
http://gcc.gnu.org/gcc-4.4/porting_to.html
 
Old 04-23-2010, 02:52 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by AmuthaA View Post
In GCC4.4 the below code gives error:
Suppose i have a class Point and and am using copy constructor in my code and GCC4.4 gives error for it.
1.Point::Point(const Point &p)
2.{
3.x = p.x; //x is member of Point
4.y = p.y;
5.}
6.main()
7.{
8.Point p1;
9.Point p2 = p1;
10.}
The error is :
3: error: dereferencing pointer '<anonymous>' does break strict-aliasing
4: error: dereferencing pointer '<anonymous>' does break strict-aliasing

Its happen only in GCC4.4
Please help to solve this.
My C++ is very rusty, still, I decided to try:


Code:
sergei@amdam2:~/junk> cat -n Point.cxx
     1  class Point
     2    {
     3    private:
     4      double x;
     5      double y;
     6
     7
     8    public:
     9      Point()
    10        {
    11        x = 0.0;
    12        y = 0.0;
    13        }
    14
    15      Point(double x1, double y1)
    16        {
    17        x = x1;
    18        y = y1;
    19        }
    20
    21      Point(const Point &p);
    22    };
    23
    24  Point::Point(const Point &p)
    25    {
    26    x = p.x; //x is member of Point
    27    y = p.y;
    28    }
    29
    30  int main()
    31    {
    32    Point p1;
    33    Point p2 = p1;
    34
    35    return 0;
    36    }
sergei@amdam2:~/junk> ~/AFSWD/install/gcc-4.4.3/binsh/g++ -Wall -Wextra Point.cxx -o Point
sergei@amdam2:~/junk>
.

I.e. no errors/warnings. Probably my code is quite wrong, I don't know.
 
Old 04-26-2010, 01:05 AM   #5
AmuthaA
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
i am using GCC4.4. Is there any solution to resolve the problem.
 
Old 04-26-2010, 01:33 AM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
you could try passing
" -fno-strict-aliasing " to gcc
 
Old 04-26-2010, 06:24 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by AmuthaA View Post
i am using GCC4.4. Is there any solution to resolve the problem.
Did you look into my post:

http://www.linuxquestions.org/questi...5/#post3945631

? I used gcc-4.4.3 and had no problem whatsoever.

Why won't you publish your full code here ? If you do, I'll try to compile it with gcc-4.4.3.

AFAIK, there is no gcc-4.4, there is gcc-4.4.0. What does

Code:
g++ --version
command display ?
 
Old 04-26-2010, 07:26 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Edit: Oops! I ignored something that might be important in the first post. The line 6 shown there is not an acceptable way to declare main(). You must have a return type for main (it should be int). I expect that is just an artifact of cutting out and posting incomplete code, in which case the rest of what I said below is still valid. But if you actually declared main() incorrectly, fix that and then see what error messages change.

Quote:
Originally Posted by Sergei Steshenko View Post
I.e. no errors/warnings. Probably my code is quite wrong, I don't know.
Your code is OK and it is further confirmation that the portion of code posted by AmuthaA is not the portion of code with the actual problem.

Quote:
Originally Posted by johnsfine View Post
Do you have a small entire compilation unit (something someone else could compile and duplicate your result) that shows this problem?

The error message you quoted looks unlikely for the code you posted.
Quote:
Originally Posted by AmuthaA View Post
i am using GCC4.4. Is there any solution to resolve the problem.
Ignoring all replies and asking again for help while providing no new information is not a good plan for actually getting help.


Since it appears you haven't posted the code with the actual problem, no one can tell you what to correct.

If you really think the problem is the compiler version, copy the full module source code that Sergei posted and compile it with your version of gcc. If it fails then that supports the idea it is something in your compiler version. If it succeeds, that proves the problem is in part of your source code that you haven't shown us yet (because Segei's lines 24 through 36 are the same as the partial source code you posted).

Last edited by johnsfine; 04-26-2010 at 07:38 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 09:50 PM
copy constructor causing confusing error message. (gcc c++) qwijibow Programming 6 09-21-2005 08:34 PM
Copy constructor in C++ prenayan Linux - General 3 11-13-2003 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration