Skip to content

Assignment Makes Pointer From Integer Without A Cast Covers

The following sentence: "If the size is zero and the pointer is not null, then the pointer will be freed." along with second row in Table 2-2: "Not null 0 Original block is freed" is not entirely true in my opinion. The actual behaviour is implementation-defined. As by C11 7.22.3/p1 Memory management functions: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. and C11 7.22.3.5: 3) (...) If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged. 4) The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated. I made some basic example, that compiles with GCC 4.4.7 on GNU/Linux CentOS 6, that indicates that memory is not freed: #include <mcheck.h> #include <stdio.h> #include <stdlib.h> int main(void) { int a = 5; int *p, *q; mtrace(); p = malloc(sizeof(int)); q = &a; printf("%p\n", (void *) p); printf("%p\n", (void *) q); q = realloc(p, 0); printf("%p\n", (void *) p); printf("%p\n", (void *) q); return 0; } $ gcc -g check.c $ export MALLOC_TRACE=report $ ./a.out 0xd4e460 0x7fff5fa0ecdc 0xd4e460 (nil) $ mtrace a.out report Memory not freed: ----------------- Address Size Caller 0x0000000000d4e460 0x4 at /home/grzegorz/workspace/check.c:12 As you can see the q was set to NULL with realloc call. According to spec this clearly means that realloc failed to allocate block with zero, thus p is not changed. The mtrace tool also indicates that it happened in that way. I am marking it as question, because I am not sure how to rewrite the sentence. Maybe the simplest way is to put this as "implementation-defined" and that deallocation is not guaranteed. Curiously, man realloc (on my system) says that: If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). which seems to untrue according to above experiment.

Note from the Author or Editor:
Page 44 Last sentence "The function's behavior is summarized in Table 2-2." Add the following statement after the sentence: "Bear in mind that the actual behavior of the realloc function is implementation dependent. Always verify its behavior before use."

I'm getting:

/tmp/nginx-goodies-nginx-sticky-module-ng-1e96371de59f/ngx_http_sticky_module.c

/root/nginx-goodies-nginx-sticky-module-ng-1e96371de59f/ngx_http_sticky_module.c: In function ‘ngx_http_get_sticky_peer’: /tmp/nginx-goodies-nginx-sticky-module-ng-1e96371de59f/ngx_http_sticky_module.c:340:21: error: assignment makes pointer from integer without a cast [-Werror] iphp->rrp.current = iphp->selected_peer; ^ cc1: all warnings being treated as errors make[1]: [objs/addon/nginx-goodies-nginx-sticky-module-ng-1e96371de59f/ngx_http_sticky_module.o] Error 1 make[1]: Leaving directory `/tmp/nginx-1.9.0' make: [build] Error 2

My configure options are: ./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-file-aio \ --with-http_realip_module \ --with-http_geoip_module \ --with-google_perftools_module \ --without-http_scgi_module \ --without-http_uwsgi_module \ --without-http_fastcgi_module \ --add-module=/root/nginx-goodies-nginx-sticky-module-ng-1e96371de59f \ --with-http_spdy_module

This is on CentOS Linux release 7.1.1503