Computer Science Canada

Initializing arrays

Author:  Justin_ [ Tue Aug 15, 2006 12:00 am ]
Post subject:  Initializing arrays

Its been a while since I last coded in c++. I'm wondering what the best way to create an array is if you don't know its size. Must you wait till you know its size or?

I'm basically creating a const unsigned character array to store the hex of a file, incase you wanted to know. So actually another good question to ask would be what is the best function to grab the length of a binary file?

Author:  wtd [ Tue Aug 15, 2006 12:40 am ]
Post subject: 

If you don't know the size you'll need? Then don't use an array.

Use one of the STL collections classes.

Author:  Null [ Tue Aug 15, 2006 10:26 am ]
Post subject: 

Just expanding what wtd said.

You should try to avoid arrays in C++ in general, as much better solutions exist. The common replacement is the std::vector<Type> containter, which handles its own memory. Here's an example:

code:

#include <iostream>
#include <vector>

const int SIZE = 10;

template <typename T>
void display(const std::vector<T> &vec) {
        //one way of printing a vector: using indexes
        for(int i = 0; i < vec.size(); i++)
                std::cout << vec[i] << std::endl;
}

int main() {
        std::vector<int> numbs;

        for(int i = 0; i < SIZE; i++)
                numbs.push_back(i + 1); //append an item

        display(numbs);
}

Author:  Justin_ [ Tue Aug 15, 2006 11:23 am ]
Post subject: 

Cool. The only thing is that I'm pretty sure the windows API doesn't work well with these containers.

Author:  md [ Tue Aug 15, 2006 11:26 am ]
Post subject: 

you can always do [syntax="cpp"]char *array = new chat[size];/syntax] Just remember to de-allocate the memory with
c++:
delete [] array;


I'm also pretty sure there is a way of getting a normal array out of a vector.

Author:  Justin_ [ Tue Aug 15, 2006 11:35 am ]
Post subject: 

Thanks.

Quote:

I'm also pretty sure there is a way of getting a normal array out of a vector.


Can anyone confirm this?

Author:  wtd [ Tue Aug 15, 2006 1:26 pm ]
Post subject: 

Null wrote:
Just expanding what wtd said.

You should try to avoid arrays in C++ in general, as much better solutions exist. The common replacement is the std::vector<Type> containter, which handles its own memory. Here's an example:

code:

#include <iostream>
#include <vector>

const int SIZE = 10;

template <typename T>
void display(const std::vector<T> &vec) {
        //one way of printing a vector: using indexes
        for(int i = 0; i < vec.size(); i++)
                std::cout << vec[i] << std::endl;
}

int main() {
        std::vector<int> numbs;

        for(int i = 0; i < SIZE; i++)
                numbs.push_back(i + 1); //append an item

        display(numbs);
}


In your code, though, since you do know the size of the vector, you would be best off passing that value to its constructor. That will preallocate that much memory, and avoid unnecessary reallocations.

code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

const int SIZE = 10;

int main() {
        std::vector<int> numbs(SIZE);

        for(int i = 0; i < SIZE; i++)
                numbs.push_back(i + 1); //append an item

        std::copy(numbs.begin(), numbs.end(),
                std::ostream_iterator<int>(std::cout, "\n"));
}

Author:  Justin_ [ Tue Aug 15, 2006 1:54 pm ]
Post subject: 

Laughing

If you are passing the addr of an array what is the syntax?

I've got

c++:

function(unsigned char& array); //preprocessor

unsigned char array[];
function(array);

Author:  wtd [ Tue Aug 15, 2006 2:04 pm ]
Post subject: 

Arrays are just glorified pointers. Understand that, and all will become clear.

Author:  Justin_ [ Tue Aug 15, 2006 2:19 pm ]
Post subject: 

I thought I did. Here is what I think it should be:

c++:

function (unsigned char* array); //preprocessor

unsigned char array[];
function(array);


Doesn't work though.

Author:  wtd [ Tue Aug 15, 2006 2:21 pm ]
Post subject: 

Perhaps if you showed the exact code you're using.

Author:  Justin_ [ Tue Aug 15, 2006 2:35 pm ]
Post subject: 

When I try to post it I get a "service temporarily unavailable" message.

So are you saying the above way is the correct way but exterior factors in the code could be impeding?

Author:  wtd [ Tue Aug 15, 2006 2:45 pm ]
Post subject: 

I'm saying that I find your psuedocode harder to understand than actual code would be.

Author:  Justin_ [ Tue Aug 15, 2006 2:54 pm ]
Post subject: 

Well the data base must be filling up or something... It won't post.

So here I'll give you some more.

c++:

//headers are good

give_me_the_array(unsigned char* array)//preprocessor.

int main()
{
   unsigned char array[];
   give_me_the_array(array);
   return 0;
}

void give_me_the_array(unsigned char* array)
{
   //do something to array
}


Author:  wtd [ Tue Aug 15, 2006 2:57 pm ]
Post subject: 

Why are you not providing a size for the array in its declaration.

If you wish to declare an array thusly, you should:

code:
int *foo;

// yada yada

foo = new int[some_integer];

Author:  Justin_ [ Tue Aug 15, 2006 3:03 pm ]
Post subject: 

Laughing Darn I knew I'd screw something up. The actual code does declare a size for the array... That is why I wanted to avoid using the code. Assume the rest of the code works dandily because it does, i've been testing it after every new function I write.

Actually I think you've answered me (although indirectly).
I now know that I am passing the array correctly to my function.


I wonder why it complains about a different preprocessor being an "undefined reference". (Remember I tested the code prior to building this array function and the other preprocesser was fine)

Author:  Justin_ [ Tue Aug 15, 2006 3:12 pm ]
Post subject: 

Got it working!

Okay, dunno what happened there... the code was definitely working. I suppose I must have accidently deleted from the other function declaration while creating the new function Sad

Author:  wtd [ Tue Aug 15, 2006 3:29 pm ]
Post subject: 

Forward declarations are not a use of the preprocessor.

Author:  Justin_ [ Tue Aug 15, 2006 3:35 pm ]
Post subject: 

Its been a long time since I used C++ so I was confusing a function prototype and a preprocessor, my bad.


: