Pointer to array as member data...

This is a discussion on Pointer to array as member data... within the C++ Programming forums, part of the General Programming Boards category; Can anyone explain what is wrong with this code? This is a simplification of a problem I isolated in another ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Pointer to array as member data...

    Can anyone explain what is wrong with this code? This is a simplification of a problem I isolated in another class. I am using Microsoft Visual C++ in Visual Studio .NET 2003 but using unmanaged code here.

    Code:
    class c
    {
    public:
            c () {pa = new int [2];}
    
            int f ()
            {
                    pa [0] = 1;
                    pa [1] = 1;
    
                    return 0; // breakpoint here
            }
    
    private:
            int* pa;
    };
    
    int main ()
    {
            c* pc = new c;
            pc -> f();
    
            return 0;
    }
    Querying the contents of pa at the breakpoint gives pa[0] == 1 but pa[1] == 0. Using numbers other than 1 yields the right result for pa[0] only, and if pa is extended beyond 2 parts it again only works for pa[0]. Help!!!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I just tested the code in Dev-c++, and it works fine there. Both pa[0] and pa[1] is 1

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I also tested in Visual Studio 2005 and it works fine too. I don't see why it wouldn't work as expected.
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by JulesGlass View Post
    Can anyone explain what is wrong with this code?
    For starters, no destructor, assignment operator, or copy constructor.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    For starters, no destructor, assignment operator, or copy constructor.
    I think the compiler makes them for you? (which I guess I can argue is a good idea or not when using pointers)

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Yes, it does. I can see that this is a "starting out" class, so you don't need them just yet.
    However, since you are allocating memory, it's generally recommended that you overload a copy constructor and assignment operator since otherwise you will get unexpected results if you makes copies of the class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by h3ro View Post
    I think the compiler makes them for you? (which I guess I can argue is a good idea or not when using pointers)
    Yes, unless the desired behavior is a shallow copy of the pointer, this code is broken. At the very least one should forbid copying and assignment.

    The lack of a destructor will result in a memory leak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

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