Thread: Nested pointer arrays

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Nested pointer arrays

    Hi,
    I am writing a c++ simulation program and have come accross a bug, so i ask the follwing question.

    I have the following class

    Code:
    class Client{
    public:
       struct packet
    	{
    		int more;
    	};
    
        packet *transfer
    }
    Code:
    int main{
               
                Client *cl;
                cl = new Client [numOfCli];
                cin >> const;
                cl[0].transfer = new Client::packet [const];
                cl[1].transfer = new Client::packet [const];
    
               return 0;
    
    }
    My question is will the above code work as intended?
    I ask this because I thought when memory is allocated for the "cl" array
    Code:
     cl = new Client[numOfCli];
    , the compiler does not yet know the size of the "transfer" array, so how does it know how much memory to allocate for the "cl" array.

    In my program I am getting problems where variables such as "more" above are changing unexpectably, so I thought maybe there is a memory overlap somwhere.
    Last edited by KirkHammett; 09-04-2005 at 07:23 AM.

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    My question is will the above code work as intended?
    Well, it depends on what you intended, but if you intended it to compile and run then it won't. The biggest problem is that const is a type qualifier, not a valid variable name. You can't use const as a variable for input or as an array size because it doesn't have an address or a value.
    so how does it know how much memory to allocate for the "cl" array.
    transfer isn't an array though, it's a pointer. The compiler knows exactly how big a pointer is.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Sorry, const is just an arbitary variable name. It is actually called pos_A in my program. When the program is run pos_A is given a value.

    transfer isn't an array though, it's a pointer. The compiler knows exactly how big a pointer
    So what you are saying is that it is ok to allocate memory the way I have done it above?

    When I run my program if I use a static array (without dynamic memory allocation ) such as
    Code:
    packet transfer[10];
    instead of
    Code:
    packet *transfer;
    the program works fine. ie variables only change when intended.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So what you are saying is that it is ok to allocate memory the way I have done it above?
    Sure. The data corruption is probably caused by something else that seems unrelated.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer notation for Multi-dimensional arrays...
    By dcwang3 in forum C Programming
    Replies: 10
    Last Post: 11-13-2008, 06:05 PM
  2. Pointer Arrays
    By Dae in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2005, 04:39 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM