Never mind. It was fixed thanks to @Graham and @saifix.
free(length) wouldn't make sense because it's not a pointer.
You should be calling free(package) after every iteration of the loop - were you calling it only once?
You also change the package pointer during the loop. You need to free() the exact value returned by malloc(), so keep a copy of that around. Calling free() with a pointer to the middle of the memory region that was allocated won't work - it needs to be given a pointer to the start.
You're also missing an fclose() call at the end.
btw, there's no need to cast the result of malloc. malloc returns a void pointer, and void pointers can be implicitly cast to any pointer type - there's no need for an explicit cast.
"package" is no longer the pointer returned by malloc. It's the pointer returned by malloc after doing some pointer arithmetic on it. Calling free() on a pointer that wasn't returned by malloc() is undefined behavior (with the exception of NULL, which makes it a no-op).Code:
package += ipSize + tcpSize + etherSize;
So, you can either do "package + ipSize + tcpSize + etherSize" inline in the printf() call or you can create a new pointer to it (char *my_ptr = package + ipSize + tcpSize + etherSize) so you can still do free(package).
|« C Login System | Algebra II Homework »|
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)