Hi all,

I have a problem with part of my code, probably due to bad memory allocation.

short y;
char *sp2;

sp2 points onto a table of 4 char

The following bit of code is part of a network traffic generator program (hence the htons).
It is part of a multithreaded program.

It works fine for 1 - 150 threads, but over that I get an Access Violation at lines with strtol or strtoul and look like this :

Code Snippet

y = htons(strtol(sp2, NULL, 16));

It shouldn't normally be an issue, but somehow it is when used by over 150 ish threads.

Any advice


Re: Visual C++ Language htons, strtol, access violation

Chuck the Code Monkey

If sp2 points to a 4 char array, are you sure it's always null terminated, i.e. shouldn't it be a 5 char array with the last charachter '\0' strtol starts reading at sp2 but doesn't stop until it either hits a NULL or a non-hex character. If sp2 isn't null terminated then strtol will be reading off the edge of the allocated memory and eventually (at around 150 threads I'd guess) that other memory belongs to someone else.