FireBall
asked on
variable
Hello ,
I got a code like the given below , there is no begin and end values but it enters the if statement different parts but i could not understand why or how , should somebody tell me how this values changes on incrementIp function
I got a code like the given below , there is no begin and end values but it enters the if statement different parts but i could not understand why or how , should somebody tell me how this values changes on incrementIp function
struct SharedData
{
bool done;
pthread_mutex_t mutex;
unsigned int ip_ring[100];
int begin;
int end;
void shared_data() { memset(this, 0, sizeof(SharedData));}
void incrementIp(unsigned int ip4){
pthread_mutex_lock(&mutex);
if ((end+1)%100 == begin)
{
pthread_mutex_unlock(&mutex);
incrementIp(ip4);
}
else
{
ip_ring[end] = ip4;
end = (++end)%100;
}
pthread_mutex_unlock(&mutex);
}
void synchronizeIps(std::map<IP4, int> & ip4map)
{
pthread_mutex_lock(&mutex);
for (int n = begin; n != (end+1)%100; n = (n+1)%100)
{
int ipaddress_decimal;
memcpy(&ipaddress_decimal, &ip_ring[n], sizeof(unsigned int));
struct in_addr dst_addr;
dst_addr.s_addr = __bswap_32(ipaddress_decimal);
char *dot_dst_ip = inet_ntoa(dst_addr);
ip4map[dot_dst_ip]++;
begin = n;
}
pthread_mutex_unlock(&mutex);
}
};
Given that you don't appear to initialise some of your variables before using them, they will have an arbitrary value and so the behaviour is undefined if you try to perform tests against them. Specifically, end is used before it's given and sensible value, at least in the code snippet you've provided.
ASKER
but it is working perfectly. That come to me so strange. C / C++ are very strict languages. How does it accept and run perfectly . Actually i got this code from somewhere and trying to still understand what is going on there
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for comments