CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Jan 2005
    Posts
    63

    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:
    Code:
    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:
    Code:
    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:
    Code:
    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
    Last edited by axr0284; June 18th, 2006 at 09:42 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured