Author |
Message |
Justin_
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Null
![](http://img118.imageshack.us/img118/214/drzoidberg3in.gif)
|
Posted: 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);
}
|
|
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: 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
I'm also pretty sure there is a way of getting a normal array out of a vector. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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"));
}
|
|
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: Tue Aug 15, 2006 1:54 pm Post subject: (No subject) |
|
|
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);
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Aug 15, 2006 2:04 pm Post subject: (No subject) |
|
|
Arrays are just glorified pointers. Understand that, and all will become clear. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Tue Aug 15, 2006 2:21 pm Post subject: (No subject) |
|
|
Perhaps if you showed the exact code you're using. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Justin_
|
Posted: 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
}
|
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: 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]; |
|
|
|
|
|
![](images/spacer.gif) |
|