uint32_t variable is strange changing

223 views Asked by At

I have simple code:

#define NUM_UA_SOCK     100

typedef struct _UA_REQUEST
{
    string full_url;
    uint32_t        url_adler32 ;
    TCPsocket   sock_ua ;
    uint32_t    status_flag ;               // ref Enum STATUS
} UA_REQUEST ;

UA_REQUEST  GLB_ARRAY__UA_REQ[ NUM_UA_SOCK ] ;

void handle_ua_sock_ready( uint32_t ii )
{
    string _req_mstr;

    byte*request    = (byte*) _req_mstr.c_str() ;
    byte*pcrlf  = NULL ;

    UA_REQUEST*ar = GLB_ARRAY__UA_REQ ;

    // Get request from UA
    int32_t nrcv ;

    printf("index =  %lu\n", ii);
    TCPsocket sock = ar[ii].sock_ua;
    nrcv = SDLNet_TCP_Recv( sock , request , MAXLEN ) ;
    printf("After index =  %lu\n", ii);
}

The ii variable in begin of handle_ua_sock_ready() func has the 0 value. After invoking nrcv = SDLNet_TCP_Recv( sock , request , MAXLEN ) ; line it becomes to have something very big value for instance 1852397344. It is single-threaded app. I'm using VS 2010, SDL, SDL_net libraries. PS: When I compiled it under Linux, it works fine.

1

There are 1 answers

0
Mike Vine On BEST ANSWER

You're passing in request to the SDLNet_TCP_Recv function and telling that function that request points to a buffer of size MAXLEN. As request comes from casting away the constness from the buffer of an empty std::string this is clearly wrong.

You want a vector

std::vector<unsigned char> request;
request.resize(MAXLEN);

...

nrcv = SDLNet_TCP_Recv( sock , request.data() , request.size() ) ;