here is what I want to do: I want to read 104-character strings from a text file, one per line. I want to chop those strings in two, the first 32 characters being a string called schecksum and the last 72 characters being a string called sencdata. Then I want to convert these strings to binary using the getHash function, and store them in vectors (ts_checksum and enc_data, respectively).

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef unsigned char uint8_t;

size_t getHash(char szHash[], unsigned char hash[])
{
       unsigned int number;
       size_t nHashLen;

       if(((nHashLen = strlen(szHash)) % 2) != 0)
            return(0);

       for(size_t i = 0;i < nHashLen;i++) {
           if(isxdigit(szHash[i]) == 0)
              return(0);
       }

       nHashLen /= 2;

       for(size_t i = 0;i < nHashLen;i++) {
           sscanf(&szHash[i*2],"%2x",&number);
           hash[i] = (unsigned char)number;
       }
       return(nHashLen);
}

void dump(uint8_t *digest)
{
     for(int i = 0;i < 16;i++)
			printf("%02X",digest[i]);
}

int main (int argc, char *argv[])
{
	vector<uint8_t*> vec_ts_checksum;
	vector<uint8_t*> vec_enc_data;
	//open the hash list...
	ifstream hash_list(argv[4]);

	//checksum of timestamp
	uint8_t ts_checksum[16];
	//encrypted timestamp
	uint8_t enc_data[36];
	char schecksum[]="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
	char sencdata[]="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

	char shash[105];
	while(!hash_list.eof())
	{
		hash_list.getline(shash,105);//get each line of hash file
		cout << shash << " loaded\n\n";
		if(strlen(shash)!=104)
		{
			cout << "encrypted timestamp is a 104-character hex string\n";
			exit(1);
		}
		//chop string into two different strings...
		for(int i=0;i<32;i++)
			schecksum[i]=shash[i];
		for(int i=0;i<72;i++)
			sencdata[i]=shash[i+32];
		//convert strings to binary and store in vector...
		getHash(schecksum,ts_checksum);

		cout << "ts_checksum= ";
		dump(ts_checksum);
		cout << "\n";

		vec_ts_checksum.push_back(ts_checksum);

		getHash(sencdata,enc_data);

		cout << "enc_data= ";
		dump(enc_data);
		cout << "\n";

		vec_enc_data.push_back(enc_data);

		cout << "\nvec_ts_checksum[0] = \n";
		dump(vec_ts_checksum[0]);
		cout << "\n\n";
	}

Everything seems to work alright except for storing the binary in the vectors. When I look at the vector, all the elements are the same! And they are all equal to the last element that was inserted into the vector!

for example, the text file contains 3 104-character strings a, b, and c. The vector ends up being filled with c, c, c rather than a, b, c. I want vector[0] to be a, vector[1] to be b, and vector[2] to be c. But vector[0] is c, vector[1] is c, and vector[2] is c! I find this to be very frustrating.

What am I doing wrong? I have tried everything! Please help. Thanks!

Recommended Answers

All 3 Replies

This is the essence of what you're doing:

#include <iostream>

using namespace std;

int main()
{
    int *p[10];
    int val = 5;

    for (int x = 0; x < 10; ++x) p[x] = &val;
    for (int x = 0; x < 10; ++x) cout << *p[x] << '\n';
    val = 10;
    for (int x = 0; x < 10; ++x) cout << *p[x] << '\n';
}

The pointers in the vector all point to the same array because the pointer is copied into the vector and not the array itself. A way to fix the problem is to have a vector of vectors and then create a new vector from the array when you want to push_back.

>>vec_enc_data.push_back(enc_data);
I think the problem is that enc_data is the same for every row in the vec_enc_data vector. What you probably need to do is allocate a new array for each row

uint8_t* ts_checksum;

...
ts_checksum = new uint8_t[32];
getHash(sencdata,enc_data);
...
vec_enc_data.push_back(enc_data);

Ah, I see. Storing the pointers instead of the actual array. I fixed it using a two dimensional array of unsigned char. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.