Author |
Message |
Justin_
|
Posted: Fri Aug 18, 2006 7:27 pm Post subject: Vectors + Arrays |
|
|
Let's open a discussion on how Vectors jibe with arrays.
Whilst making my file binder I noticed that if I put an array into the vector it scrambles the first 8 bytes of the DOS stub. This is not a huge problem since a PE executable doesn't require the first 8 bytes, but I still require an explanation. I doubt anyone here knows but:
Here's an example:
c++: |
#include <vector>
#include <windows.h>
#include "file.h"
using std::cout;
using std::endl;
int main()
{
File file("gen.exe");
unsigned char* pFile = file.getData();
cout << "First byte of dos signature should be equal to 'M': "
<< *pFile
<< endl;
return 0;
}
//getData returns an 'unsigned char*'
/**OutPut
First byte of dos signature should be equal to 'M': M
*/
|
Let's put the char array into a vector now and see if the first byte is still 'M'.
c++: |
#include <vector>
#include <windows.h>
#include "file.h"
using std::cout;
using std::endl;
int main()
{
std::vector<File> fileVector;
fileVector.reserve(1);
fileVector.push_back(File("gen.exe"));
unsigned char* pFileVec = fileVector[0].getData();
cout << "First byte of dos signature should be equal to 'M': "
<< *pFileVec
<< endl;
return 0;
}
//remeber: getData returns an 'unsigned char*'
/**OutPut
First byte of dos signature should be equal to 'M': x
*/
|
Note how the M turns into an x? And it goes even deeper than that! The first 8 bytes of our executable will be different as I mentioned above. Like I say: they are scrambled (not removed).
***Again I'm sorry if any of my code is messed up, to make me a better coder please inform me of my mistakes. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Fri Aug 18, 2006 9:56 pm Post subject: (No subject) |
|
|
What's with your file type? Why not use one of the fstream classes? |
|
|
|
|
 |
Justin_
|
Posted: Sat Aug 19, 2006 1:12 am Post subject: (No subject) |
|
|
From what I could see, fstream didn't provide what I needed. So I made the File class to store information about a given file, including the bytes of the file stored in an array.
Besides, what point would there be in having classes in C++ if the standard libraries didn't support them? I think the problem is in the way a vector deals with arrays. |
|
|
|
|
 |
Justin_
|
Posted: Sat Aug 19, 2006 1:38 am Post subject: (No subject) |
|
|
After taking a second look at fstream, it would be possible to store the bytes of a file in a char buffer using the seekg method to set the get pointer to the beginning of the file then get the length and read it into the buffer. When I first started out on this I didn't know anything so I used some methods from the stdlib to do the same thing.
About the vectors, how could a vector store an array in the first place?
I mean the vector makes room for each object by calculating the size of all the data types and a little for the methods it contains. When dealing with an array, there could be any number of bytes in it, so how would it know the appropriate size to make itself?
There are two possibilities I can think of. Either the array itself is stored seperately in memory, (like on the heap - but I thought that only happened when you use the "new" operator). Or second, it magically resizes each object in the vector as the array becomes larger. (Which is unlikely, I should think.)
You're a wealth of knowledge wtd, do you know the answer? |
|
|
|
|
 |
wtd
|
Posted: Sat Aug 19, 2006 7:30 am Post subject: (No subject) |
|
|
Arrays are just glorified pointers. You could store a pointer to a stack-allocated array in a vector, but the usual caveats about scope apply.
You could also have a vector of vectors, or as you mentioned, heap allocated arrays. |
|
|
|
|
 |
wtd
|
Posted: Sat Aug 19, 2006 8:56 am Post subject: (No subject) |
|
|
Ah. I'm pretty sure that ifstream lacks a copy constructor and assignment operator. This means that it does not satisfy the requirements placed on items that can be in a vector. |
|
|
|
|
 |
Justin_
|
Posted: Sat Aug 19, 2006 1:58 pm Post subject: (No subject) |
|
|
And with that in mind, C++ needs to add some functionalities to its standard libraries. fstream is a terrible IO class if you ask me, it does not comply to c++ standards, it doesn't even accept strings as arguments but rather opts for C-Style arrays... |
|
|
|
|
 |
Justin_
|
Posted: Sun Aug 20, 2006 7:50 pm Post subject: (No subject) |
|
|
Actually I take that back, fstream is okay. But it isn't working for me  |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Sun Aug 20, 2006 9:45 pm Post subject: (No subject) |
|
|
Justin_ wrote: But it isn't working for me
In what way is it not working? The same vector issue, or just basic I/O stream issues? |
|
|
|
|
 |
Justin_
|
Posted: Sun Aug 20, 2006 11:22 pm Post subject: (No subject) |
|
|
It was adding extra bytes throughout the file, it took a long time to figure out that it was because, while I had specified binary mode for the read file, I had forgotten to do the same for the write file. It was unfortunate, but now it works.
Another thing is that when you close a write file it loses track of the properties you specify with the constructor, so when you use the same instance of an fstream to open a new file you have to specify what mode again.
My next task is to attach a textfile to my executable so it doesn't need to be included in the directory. I'm thinking I'll have to make it binary and attach it to the executable, then extract it at run time. What are your thoughts? |
|
|
|
|
 |
wtd
|
Posted: Mon Aug 21, 2006 11:18 am Post subject: (No subject) |
|
|
Just keep it as a separate file. KISS. |
|
|
|
|
 |
Justin_
|
Posted: Mon Aug 21, 2006 3:07 pm Post subject: (No subject) |
|
|
lol, why you kissing me? |
|
|
|
|
 |
wtd
|
Posted: Mon Aug 21, 2006 3:49 pm Post subject: (No subject) |
|
|
It's an acronym.
Keep It Simple. And an optional "Stupid" on the end. |
|
|
|
|
 |
|