Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Initializing arrays
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Aug 15, 2006 12:40 am   Post subject: (No subject)

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

Use one of the STL collections classes.
Null




PostPosted: Tue Aug 15, 2006 10:26 am   Post subject: (No 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);
}
Justin_




PostPosted: Tue Aug 15, 2006 11:23 am   Post subject: (No subject)

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




PostPosted: Tue Aug 15, 2006 11:26 am   Post subject: (No 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.
Justin_




PostPosted: Tue Aug 15, 2006 11:35 am   Post subject: (No 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?
wtd




PostPosted: Tue Aug 15, 2006 1:26 pm   Post subject: (No 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"));
}
Justin_




PostPosted: Tue Aug 15, 2006 1:54 pm   Post subject: (No 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);
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Aug 15, 2006 2:04 pm   Post subject: (No subject)

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




PostPosted: Tue Aug 15, 2006 2:19 pm   Post subject: (No 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.
wtd




PostPosted: Tue Aug 15, 2006 2:21 pm   Post subject: (No subject)

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




PostPosted: Tue Aug 15, 2006 2:35 pm   Post subject: (No 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?
wtd




PostPosted: Tue Aug 15, 2006 2:45 pm   Post subject: (No subject)

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




PostPosted: Tue Aug 15, 2006 2:54 pm   Post subject: (No 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
}

wtd




PostPosted: Tue Aug 15, 2006 2:57 pm   Post subject: (No 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];
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: