vectors of structures

Hi,
I have been trying to get this to work for sometime now but without success. I am trying to use a vector of structures in the following class:

class aodv_rit {

public:

/*This structure forms an RREQ PACKET.
* RREQ|SRC|DST|BID|SEQNO|TTL|FROM|TO
* 1 |1 |1 |4 |4 |1 |1 |1 bytes
*/
struct RREQ_STRUCT {
unsigned char rreq_code; /* The destination ID*/
unsigned char source; /* The source ID*/
unsigned char destination; /* The destination ID*/
int broadcast_ID; /*The ID of the broadcasted RREQ. This uniquely identifies each RREQ*/
int sequenceNumber; /*The latest sequence number for this source*/
unsigned char TTL; /*The Time To Live*/
unsigned char FROM; /* which node sent it*/
unsigned char TO; /* To which node was it sent*/
};

std::vector<RREQ_STRUCT> rreq_in_Buffer; /*This is a buffer used by the callback function to store all incoming rreqs*/
}

The vector is then used in a function as follows:

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
int returnValue = 0;

RREQ_STRUCT rreq_object; /*The structure defining an rreq*/

/*Create the RREQ structure object*/
rreq_object.rreq_code = RREQ;
rreq_object.source = packet[1];
rreq_object.destination = packet[2];

rreq_object.broadcast_ID = 0;
for(int i=0; i<sizeof(int);i++) {

rreq_object.broadcast_ID |= (unsigned char) packet[i+3];

if(i != sizeof(int)-1) {
rreq_object.broadcast_ID <<= 8;
}
}

rreq_object.sequenceNumber = 0;
for(int i=0; i<sizeof(int);i++) {

rreq_object.sequenceNumber |= (unsigned char) packet[i+3+sizeof(int)];

if(i != sizeof(int)-1) {
rreq_object.sequenceNumber <<= 8;
}
}

rreq_object.TTL = packet[3+2*sizeof(int)];
rreq_object.FROM = packet[3+2*sizeof(int)+1];
rreq_object.TO = packet[3+2*sizeof(int)+2];

/*Store in rreq_in_Buffer*/
rreq_in_Buffer.push_back(rreq_object); <<<<<<<<<<<<<<<<<

...
...
...

return returnValue;
}

The problem is everytime it reaches the <<<<<<<<< push_back function, I get an exception error. I traced it to inside the vector class:

void push_back(const _Ty& _Val)
{ // insert element at end
if (size() < capacity())
...
...
...

size_type size() const
{ // return length of sequence
return (_Myfirst == 0 ? 0 : _Mylast - _Myfirst);
}

It seems _Myfirst does not exists. I get an address violation exception on the return line for size(). I am at a lost why that would happen. Did I not initialize the vector properly? Thanks for any help.
Amish
[3014 byte] By [axr0284] at [2007-11-19 22:49:50]
# 1 Re: vectors of structures
You have an awful lot of code that has nothing to do with vector, and we don't know what side effects/undefined behavior/bugs it could contain that may effect how vector operates.

Why not post a small, compilable, but complete example that duplicates the error? We don't know what context this piece of code you posted is used, where it's used, how it's called, etc. etc. For example:

packet[i+3+sizeof(int)]

You pass packet as a char*. How do we (or you) know that packet[i+3+sizeof(int)] is out of bounds, and you have a memory overwrite? A memory overwrite can cause other parts of your program to not behave correctly.

If you can't create a duplicable, complete example, then this is more of a reason why you should inspect the other code, not vector.

#include <vector>

struct RREQ_STRUCT {
unsigned char rreq_code;
unsigned char source;
unsigned char destination;
int broadcast_ID;
int sequenceNumber;
unsigned char TTL;
unsigned char FROM;
unsigned char TO;
};

std::vector<RREQ_STRUCT> rreq_in_Buffer;

int main()
{
RREQ_STRUCT r;
rreq_In_Buffer.push_back( r );
}

Does this crash? If not, then the problem is not push_back().

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:04:14 >
# 2 Re: vectors of structures
I tried it with an int too just to be sure:

class aodv_rit
{
public:
std::vector<int> intest;
int recvPacket(unsigned char *packet, int size);
}

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
...

intest.push_back(1);
...
}

same thing happened

although I tried it like this and it worked:

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
...
std::vector<int> test2;

test2.push_back(1);

std::vector<RREQ_STRUCT> rreq_test;

...
...
...
rreq_test.push_back(rreq_object);
...
}

It seems if I declare the vector in the same function where I use it it works fine. Any suggestions anybody. I am really at a loss here. Thanks a lot
Amish
axr0284 at 2007-11-9 1:05:16 >
# 3 Re: vectors of structures
no it's not Like that . you declare vector as a class member of your class so in any member Function of that class can have access to your vector. here is a Small example have a look on this. Not Calling your Function any where in my Code but this will work proper just check it out


class MyClass
{
private :
int i,j;
public :
vector <int> c1;
vector <int>::iterator c1_Iter;
int recvPacket(unsigned char *packet, int size);
};
int MyClass::recvPacket(unsigned char * packet, int size)
{
c1.push_back(1);
return 0;
}
int main(int argc, char* argv[])
{
MyClass my;
my.c1.push_back( 1 );
my.c1.push_back( 2 );
my.c1_Iter = my.c1.begin( );
cout << "The first element of c1 is "<< *my.c1_Iter << endl;
return 0;
}


thanx
humptydumpty at 2007-11-9 1:06:10 >
# 4 Re: vectors of structures
Isn't that what I did here:

class aodv_rit
{
public:
std::vector<int> intest;
int recvPacket(unsigned char *packet, int size);
}

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
...

intest.push_back(1);
...
}

both intest and recvPacket are part of the aodv_rit class so it should work but I get memory violation exception everytime. I really appreciate the help. If somebody would be willing to compile the code, maybe I can send the whole files. There's only three. Thanks
Amish
axr0284 at 2007-11-9 1:07:12 >
# 5 Re: vectors of structures
Okay that's 5n just simple post your zip file.so can check it out
wat's the error . Second in your Both Code after the End of your Class semicolon is missing have a look on that also.Second thing your are using char* did u Properly allocate it memory with new and doing free it with delete .if still problem Just post your zip file

Thanx
humptydumpty at 2007-11-9 1:08:20 >
# 6 Re: vectors of structures
You've made the common mistake of assuming that crashes occur due to a bug in the lines of code around where the crash happens. In practice, the bug can be in any code that has been executed in the entire program prior to the crash - commonly bugs can corrupt the stack or heap, leading to strange behaviour later on. Here, I think you are suffering from heap corruption.

The code posted looks fine, so the problem is in some other code. Look for buffer overruns, double deletes, uses of deleted pointers, etc.
TomWidmer at 2007-11-9 1:09:13 >
# 7 Re: vectors of structures
I located the error. It was not the vectors after all. I am using callback functions in this class and I tries to do the following:

int main(int argc, char* argv[])
{
aodv_rit *aodv = NULL;
aodv = new aodv_rit();

pt2Object = (void*) &aodv; /*Set a global pointer to the class*/
...
...
}

static void* pt2Object; /*This variable is used to point to the class for callback purposes*/

void aodv_rit::recvPacket_callback(unsigned char *packet, int size) {

// explicitly cast to a pointer to TClassA
aodv_rit* mySelf = (aodv_rit*) pt2Object;

// call member
mySelf->recvPacket(packet, size);

}

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
...
...
}

pt2Object is declared outside the aodv_rit class in the aodv_rit.h file. It seems though that although an address is assigned to pt2Object properly in the main class, it does not show up in the aodv_rit class since when debuffing in function recvPacket_callback, it's all 0. That's why it was getting this memory violation stuff. Now if I can only fix this. I also included the files with this post if anybody care to take a look. It uses a bunch of dll to work but including the dll makes the filesize too big. Thanks for the help
Amish
axr0284 at 2007-11-9 1:10:19 >
# 8 Re: vectors of structures
I located the error. It was not the vectors after all.That is what I was saying from the beginning. Also, you didn't try the code that I posted previously. Instead, you still tried the code that no one knew about, but just changed the type that you're storing in a vector to int. That didn't get you anywhere, and led up to others basically repeating what I mentioned earlier

Next time, please verify that *only* push_back() doesn't work, as my example shows. Then if that works, then don't look at vector as the problem.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:11:14 >
# 9 Re: vectors of structures
I tried it with an int too just to be sure:
Please read the following guidelines when posting a problem with code, especially item 1:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Your code that you posted is riddled with ellipses. No one knows what you're doing in there. And as you've found out, it is this mystery code in these ellipses that was causing the problem.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:12:14 >
# 10 Re: vectors of structures
my bad. I'll be more careful next time when posting code. Thanks
Amish
axr0284 at 2007-11-9 1:13:22 >
# 11 Re: vectors of structures
beware of callback functions. Are you taking a class member function then casting it? I'm not sure exactly what you are doing there.

I would advise you don't use callback functions at all - use an abstract base class and pass a (smart) pointer or a reference to one.

Also make your code const correct. And also so many of your class member are public.
NMTop40 at 2007-11-9 1:14:17 >