4

I am doing this learning/practicing with arrays of objects and array of pointers to objects and I'm very confused about how to get dynamic arrays sizes.

I've got this:

private: 
    Client ** arr_client;

public:
    static string  members [];

then populated the array arr_client with tokenized data from the following static string array members:

static string members[] = {"Jhon Perez 623 22 12 1998"
                          ,"Louis Smith 199 02 12 1988"
                          ,"Daniel Martinez 106 02 01 2010"};


void load(void){

arr_client = new  Client * [(sizeof(members)/sizeof(string))*sizeof(Client)];

    for (i = 0; i < (sizeof(members)/sizeof(string)); i++){
        istringstream stream(members[i],ios_base::in);

        stream >> name;
        stream >> lastname;
        stream >> aux;
        id = atoi(aux.c_str());
        stream >> date;
        date.append(" ");
        stream >> aux;
        date.append(aux);
        date.append(" ");
        stream >> aux;
        date.append(aux);

        arr_client[i] = new Client(name,lastname,id,date);
    }
}

now, after the object array is full, I want to loop through arr_client but I cant seem to find the way to do it.

should I use:

for (int i =0; i < (sizeof(**arr_client)/sizeof(client)); i++)

or take the previous cal for size and do:

for (int i =0; i < (sizeof(members)/sizeof(string)); i++)

and be done with it? doesn't look that usefull to me... but then again I'm very new.

Few questions I've also got:

What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array.

How can count how many elements are in it so I could loop through it?

Could I use an std::iterator for arr_client?

Any tips would be much appreciated =)

and.... yes, I do know about <vector> and it's advantages but never hurts to know about these scenarios.

Will Raben
  • 97
  • 3
  • 11
  • The more stars you have in your program, the more you confuse everyone. Add one more and you will be a [Three Star Programmer](http://c2.com/cgi/wiki?ThreeStarProgrammer). Please try something useful instead! – Bo Persson Aug 04 '11 at 21:41
  • @Bo : Quoting your link `"One of the best programmers (as in useful to the organization) I've worked with recently was a convert from Human Resources, and fits the pattern you describe. So we want people who could be ThreeStarProgrammers but have the good taste and judgement not to want to be"` This summarises most of my thinking behind practicing these levels of indirections. – Will Raben Aug 05 '11 at 12:16
  • I have been a programmer for 30 years, and I cannot do multi-star code without using paper an pencil. But it doesn't matter, because you'll hardly ever use it. If you want to learn C++, my advice is to skip the C way of doing things and focus on what is C++. Most well written C code is just lousy as C++. They are two different languages. – Bo Persson Aug 05 '11 at 12:26
  • @Bo: Will do, thanks for the advice. I was already hating going back to C when there was so many and less problematic resources in c++ specially now that C++0x is inmminent... but I guess they haven't heard of it at my college :( – Will Raben Aug 05 '11 at 15:11

3 Answers3

1

First, I applaud you for trying to learn the fundamentals before adopting an advanced tool like vector.

There's no clean and reliable way to do what you ask. People usually remember the size of an array in a separate variable, like this:

unsigned int num_strings=3;
static string  members[num_strings];

If you really want to determine the size of an array at runtime, you can do it with a template:

template<typename T, size_t N>
size_t arraySize( T(&)[N] )
{
  return(N);
}
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Vector isn't an "advanced" tool, but a simple tool designed exactly to solve this kind of problem. Doing multi-star programming with new and delete is not a good way to learn C++, but more like a wasted effort. – Bo Persson Aug 04 '11 at 21:38
  • 2
    @Bo Persson: a matter of pedagogical philosophy. I think that every student should spend a few hours getting cut and bruised working with the primitive tools before moving up, it gives valuable insight. But you're right, I should have said "more advanced". – Beta Aug 04 '11 at 21:57
  • Just for the record, I'm not trying to build up my programming style on using 2 and 3 "stars" all the time. I do think that knowing the shortcomings of being a "Three Star Programmer" will help a lot to avoid it; it's always easier to do once you know the how and why of things imho. – Will Raben Aug 05 '11 at 12:06
0

What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array.

How can count how many elements are in it so I could loop through it?

You always need to know the size. You have to save it alongside the array, or you can't use it safely. That's why you should not use dynamic arrays, but rather safe containers like std::vector or Boost.MultiArray.

And you cannot use sizeof for that, as sizeof is a compile-time operator.

Could I use an std::iterator for arr_client?

You can use pointer_to_start as a range start and pointer_to_start + size as a range end whenever an iterator is expected.

Community
  • 1
  • 1
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
0

What if I don't know the size of that dynamically assinged array of objects? like in this case that im building upon a known sized array. How can count how many elements are in it so I could loop through it?

You can't. A pointer doesn't hold this information. You should keep it in some other way.

Could I use an std::iterator for arr_client?

No. std::iterator is a template which you should derive your custom iterator class from to avoid retyping a lot of typedefs. But any pointer is a ready-to-use random access iterator.

I suggest you read a good C++ book. My own vote goes for Lippmann's C++ Primer. It will help you a real lot

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • hey, thanks for the book list I come from java and this memory managing of c++ looks very appealing but it's just a mess in my head – Will Raben Aug 04 '11 at 19:48