how to get the size of an array of pointers to objects

This is a discussion on how to get the size of an array of pointers to objects within the C++ Programming forums, part of the General Programming Boards category; Can you tell that I've only been using C++ for two weeks and I'm already going bananas with it? Anyway: ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    how to get the size of an array of pointers to objects

    Can you tell that I've only been using C++ for two weeks and I'm already going bananas with it? Anyway: I'm writing a neural network simulation program - purely as a hobby, this isn't homework or anything. If I pass an array of pointers to objects, to a function, how can that function work out how many elements are in the array? Here's what I mean, and how I've tried to do it so far:

    Code:
    void Neuron::AdjustWeights(TrainingSet *trdata[], double eta)
    {
    	short nt=sizeof(*trdata)/sizeof(*(trdata[0]));
    //	blah
    }
    It compiles fine, but when I run it I get that delightful dialog box saying the program's crashed. What am I doing wrong?

    Thanks in advance!

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    You can't do that. But you can use std::vector instead to store arrays and pass by reference to the function that takes the array. This way you can extract the size using the size() member function.
    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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    As Elysia says, use a std::vector if you're wanting to make best use of C++.

    That sizeof() thing only works on arrays which are in scope. As soon as you pass an array to a function, all the size information disappears (all you've got is a pointer).
    The only alternative is to pass the size of the array as an additional parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    OK thanks. For now I'm passing the number of elements as an extra argument, as I don't know what this std::vector is. But I'll find out sometime. I'm on day 12 of SAMS Teach Yourself in 21 days, but the rest is gonna take a lot longer than 9 days!

    Ta again.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    It acts as a simple array. You can use it as a simple array too. To add an element, you'd typically just call push_back. You can resize it too with resize, then you can just do vector[pos] = something.
    Even if you don't understand it, I suggest you try it. It's not difficult. It basically emulates a real array.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  2. Replies: 11
    Last Post: 09-22-2006, 05:21 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM

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