g++ 3.3 version on Linux 64-bit: passing base class pointer corrupts variable data

This is a discussion on g++ 3.3 version on Linux 64-bit: passing base class pointer corrupts variable data within the C++ Programming forums, part of the General Programming Boards category; Hello, My project contains a 1) shared library 2)executable containing a static library The shared library has an interface used ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    31

    g++ 3.3 version on Linux 64-bit: passing base class pointer corrupts variable data

    Hello,

    My project contains a 1) shared library 2)executable containing a static library

    The shared library has an interface used by the static library and takes as input a pointer to a base class (have used a structure not a class).

    In the static library I am creating the derived structure object using "new" and then assigning it to base class pointer and passing it to the interface function.

    However, when I print out the values in the shared library they are all 0

    Can anyone tell me what might be the issue ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you cut down your example to the simplest compilable example and post it? (I.e., does it matter about shared library/static library, or class vs structure, etc. I'm guessing "no".)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Some things to try:
    • Are you compiling with optimizations enabled? Try turning optimizations off, in case the compiler is doing some vtable optimizations.
    • Also try doing a clean rebuild, if the size of the base class changed for example then new could be doing the wrong thing.
    • Did you try printing the values in the static library after the allocation of the object? What is the value of the pointer there? Perhaps it's 0 too if you have some strange allocators or you're out of memory (unlikely, but possible).
    • Try forcing the base class to have a vtable by adding a virtual destructor to it.


    And for your edification: C++ dlopen mini HOWTO
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    31
    In my shared library, I have

    Code:
    struct Base
    {
     /* 4 unsigned long members are there here */
    }
    
    struct Derived : public Base
    {
        /* 2 unsigned long members are here */
    }
    In my static library, I have a class member

    Code:
    Base* b;
    In my cpp file I have done

    Code:
    Derived* d = new Derived;
     assigned all members of it
    b = d;
    Have passed pointer 'b' through a pure virtual function accepting Base* pointer as input.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Here's the problem with your non-example.
    Code:
    C:\Documents and Settings\Owner>more test.cpp
    #include <iostream>
    struct Base {
       unsigned long a, b, c, d;
       virtual void foo(Base *b) =0;
    };
    struct Derived: public Base {
       unsigned long e, f;
       void foo(Base *b);
    };
    void Derived::foo(Base *b)
    {
       std::cout<<b->a << ' ' <<b->b<< ' ' <<b->c<< ' ' << b->d<< '\n';
    }
    int main()
    {
       Derived *d = new Derived;
       d->a = d->b = d->c = d->d = 1;
       d->foo(d);
       delete d;
    }
    
    C:\Documents and Settings\Owner>test
    1 1 1 1
    I wrote this to your specification, and it ends up telling us nothing about your problem. You need to show what you are doing without taking shortcuts.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    31
    Code:
    #include <iostream>
    
    struct Base {
       unsigned long a, b, c, d;
    };
    
    struct Derived: public Base {
       unsigned long e, f;
    };
    
    int main()
    {
       Derived *d = new Derived();
       Base* b;
    
       d->e = 10;
       d->f = 100;
       d->a = d->b = d->c = d->d = 1;
    
       b = d;
    
       func(b);
    }
    func() is in the shared library. When I write the program separately (standalone) I see no issues. Only in my production code I see this issue and only on Linux 64-bit.
    gcc 3.3.3. Hence, I am finding it difficult to explain the problem.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    2,490
    my suggestion would be to upgrade your compiler. gcc 3.3.3 came out around 2003, and so it is ancient in terms of compilers. 4.6 is the generally available version, and if your observations show a bug in gcc or its associated libraries, it has probably been fixed in the last 8 years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-19-2011, 10:03 PM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  5. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21