Kevin P. Fleming | 1 Aug 2009 02:41
Favicon
Gravatar

Spurious warning in gcc SVN trunk

Just for kicks, I pulled a copy of SVN trunk today, built it, and tried
compiling some existing code. While it caught a number of problems that
previous versions did not, it also reported a couple of cases that seem
spurious. Here's a small example:

> #include <stdlib.h>
> #include <stdio.h>
> 
> int main(int argc, char **argv)
> {
>         switch (argc) {
>         case 0:
>         {
>                 char *foo = malloc(1);
>                 if (!foo)
>         TEST_LABEL:
>                 {
>                         printf("test\n");
>                         return 0;
>                 }
>                 free(foo);
>                 break;
>         }
>         case 1:
>         {
>                 char *foo = "xyz";
>                 if (argv[0]) {
>                         goto TEST_LABEL;
>                 }
>                 printf(foo);
(Continue reading)

Klesk | 1 Aug 2009 11:11
Picon

How to add a class to the tree

Let's say I have code like this:

SampleClass object;

which of course fails to compile because there is no SampleClass.

How to programmically create a class named SampleClass to make it
possible to complie ?

--

-- 
Klesk

Amittai Aviram | 1 Aug 2009 15:57
Picon
Favicon
Gravatar

Re: How to add a class to the tree

On Aug 1, 2009, at 5:11 AM, Klesk wrote:

> Let's say I have code like this:
>
> SampleClass object;
>
> which of course fails to compile because there is no SampleClass.
>
> How to programmically create a class named SampleClass to make it
> possible to complie ?
>
>
> --  
> Klesk

Hi, Kiesk!

#include <iostream>

class SampleClass {
  private:
    int id;
  public:
   SampleClass() {
    id = 42;
  }
  int get_id() {
    return id;
  }
};
(Continue reading)

Alireza Haghdoost | 1 Aug 2009 16:28
Picon

Segmentation fault after compilation with gcc-3.4 on 64-bit Linux

Dear All

I have a big ANSI-C application which should compile by gcc 3.4 or
earlier gcc versions, It works fine on any 32-bit Linux but when I
compile it on 64-bit one, application execution failed by Segmentation
fault error. I mean that gcc-3.4 on 64-bit machine compiles my
application well but when I try to run compiled application, it failed
by Segmentation Fault error.
I have tried several compilation Flags like -mtune=nocona and -m64 but
no one works for me and I still have Segmentation fault in the
execution of my application only on 64-bit Linux.
I will appreciate if some one here can help me.

---
Best regards,
Alireza Haghdoost

John S. Fine | 1 Aug 2009 16:53
Picon

Re: Segmentation fault after compilation with gcc-3.4 on 64-bit Linux

Forget compilation flags and don't ask about or blame gcc.  There are 
bugs in your C code and that makes it crash.

Many 32 bit applications are filled with questionable casts and other 
constructs that assume things such as ints being the same size as 
pointers, that are correct in 32 bit mode but not in 64 bit mode.

When you port a large application like that from 32 bit to 64 bit, you 
have to be prepared to find and fix those bugs.

I've also seen a number of examples of code that is just wrong, even in 
32 bit mode, but shows no symptoms until you build it in 64 bit mode.  
That includes things like using fields in a structure after freeing the 
memory containing that structure and things like testing uninitialized 
stack variables and behaving correctly only if they are non zero.  
Almost anything that changes the size or layout of your program or data 
might change those errors from symptom free to seg fault.  It just 
happened that a switch from 32 bit to 64 bit did it.

If you know how to use a debugger, you can look at the code, data and 
callback stack at the point of the seg fault and deduce what bug caused 
the seg fault.  If you're lucky, finding and fixing a few of those bugs 
will solve the whole problem.  But many questionable casts and other 
usages that only works in 32 bit mode produce wrong results rather than 
seg faults when run in 64 bit.  If you program has those bugs, you need 
a good understanding of the whole program, rather than just some 
competence with a debugger, in order to find and fix the bugs.

You should be asking yourself why you need this program to run in 64 bit 
mode and whether it is really worth the effort.  If I were doing it, I 
(Continue reading)

Klesk | 1 Aug 2009 17:08
Picon

Re: How to add a class to the tree

Hi

I would like something else... I think I wasn't specific enough..
let's try again :)
I have:

SampleClass object;

While parsing gcc will try to find what 'SampleClass' is, but  because
it was not defined by user it will signal an error. Generally I would
like to avoid getting an error without modifying source given above,
so I would like to create programmically a simple class when it's
impossible to find  it.

By 'create programmically' I mean: write a function which will create
a tree_node (I think it's needed)  representing a class named
'SampleClass' and then add it to the tree representing source code.
Function would called when there's no definition for 'SampleClass'.

2009/8/1 Amittai Aviram <amittai.aviram <at> yale.edu>:
> On Aug 1, 2009, at 5:11 AM, Klesk wrote:
>
>> Let's say I have code like this:
>>
>> SampleClass object;
>>
>> which of course fails to compile because there is no SampleClass.
>>
>> How to programmically create a class named SampleClass to make it
>> possible to complie ?
(Continue reading)

Andrew Haley | 1 Aug 2009 18:33
Picon
Favicon

Re: Segmentation fault after compilation with gcc-3.4 on 64-bit Linux

Alireza Haghdoost wrote:
> Dear All
> 
> I have a big ANSI-C application which should compile by gcc 3.4 or
> earlier gcc versions, It works fine on any 32-bit Linux but when I
> compile it on 64-bit one, application execution failed by Segmentation
> fault error. I mean that gcc-3.4 on 64-bit machine compiles my
> application well but when I try to run compiled application, it failed
> by Segmentation Fault error.
> I have tried several compilation Flags like -mtune=nocona and -m64 but
> no one works for me and I still have Segmentation fault in the
> execution of my application only on 64-bit Linux.
> I will appreciate if some one here can help me.

Assuming you're on a supported platform, Valgrind memcheck is your friend.

http://valgrind.org/docs/manual/mc-manual.html

Andrew.

John (Eljay) Love-Jensen | 1 Aug 2009 19:58
Picon
Favicon

RE: How to add a class to the tree

Hi Klesk,

For an opaque object, you'll need a creator and destructor function for SampleClass.

class SampleClass;
extern SampleClass* CreateSampleClass();
extern void DestroySampleClass(SampleClass*);

Then you'll be able to do:

SampleClass* object = CreateSampleClass();

Sincerely,
--Eljay

________________________________________
From: gcc-help-owner <at> gcc.gnu.org [gcc-help-owner <at> gcc.gnu.org] On Behalf Of Klesk [kleskmail <at> gmail.com]
Sent: Saturday, August 01, 2009 4:11 AM
To: gcc-help <at> gcc.gnu.org
Subject: How to add a class to the tree

Let's say I have code like this:

SampleClass object;

which of course fails to compile because there is no SampleClass.

How to programmically create a class named SampleClass to make it
possible to complie ?

(Continue reading)

Michael Eager | 2 Aug 2009 04:48

Re: declaration order of member variables in struct

Martin Ettl wrote:
> Hello,
> 
> i have following source, that compiles when the order of the declarations is as following:
> 
> #include <iostream>
> #include <cstring>
> 
> template <int N, int Low=1, int Upp=N> struct Root 
> {
>   static const int  mean = (Low+Upp)/2;
>   static const bool down = ((mean*mean)>=N);
>   static const int  ret  = Root<N,(down?Low:mean+1),(down?mean:Upp)>::ret;
> }; 
> template <int N, int Mid> struct Root<N,Mid,Mid>
> {static const int ret = Mid;};
> 
> int main()
> {
> 	std::cout << Root<10,1,10>::ret << std::endl;
> }
> 
> But, when i reorder the declaration sequence in struct Root as followed:
> 
> template <int N, int Low=1, int Upp=N> struct Root 
> {
>   static const int  ret  = Root<N,(down?Low:mean+1),(down?mean:Upp)>::ret;
                                      ^^^^     ^^^^     ^^^^ ^^^^

Neither down nor mean are defined before they are referenced.
(Continue reading)

John Fine | 2 Aug 2009 15:03
Picon

Re: declaration order of member variables in struct

I think Martin was asking why the usual exception to pre declaration 
doesn't apply.
In defining a class or struct, you can normally use symbols before you 
declare them.
I don't know what makes this example different.

Michael Eager wrote:
> Martin Ettl wrote:
>>
>> template <int N, int Low=1, int Upp=N> struct Root {
>>   static const int  ret  = 
>> Root<N,(down?Low:mean+1),(down?mean:Upp)>::ret;
>                                      ^^^^     ^^^^     ^^^^ ^^^^
>
> Neither down nor mean are defined before they are referenced.
>
> ...
> Each symbol (variable or type) in C/C++ must be declared before it is 
> used.
>
>


Gmane