The number 16384 ?!

Oct 18, 2008 at 1:39pm
Hey,

I am currently studying this code a I 've got some difficulties to undestand the following lines:

if (++D->nP > num) {
num += 16384;
if ((tmp = realloc(D->P, num * sizeof *D->P)) == 0) {
fprintf(stderr, "realloc error\n");
return 0;

Could you please tell me waht the 16384 stands for and what this code ist exactly trying to do?
PS: P stands for packet

Thanks in advance!!
Oct 18, 2008 at 2:00pm
16834 is 16K exactly, but I think from the looks of this code that the number was probably arbitrarily chosen.

What this code is doing, it would seem -- the variable names are not well chosen so the code isn't very self-dois equal tcumenting -- is attempting to add an element to a dynamically allocated array whose size, in elements, is stored in D->nP. If the number of elements in the array now exceeds the capacity of the array (num), it attempts to reallocate the array by calling realloc. If there isn't memory to do the realloc, the code gives up, prints an error, and returns.
Topic archived. No new replies allowed.