libstdc++ versus -Wcast-align

Gerald Pfeifer pfeifer@dbai.tuwien.ac.at
Fri May 2 18:48:00 GMT 2003


[ gcc -> gcc-patches ]

On Mon, 28 Apr 2003, Benjamin Kosnik wrote:
> You can try adding
>
> #pragma GCC system_header
>
> to bits/stl_alloc.h, which might hide this error.

Would you accept such a patch for mainline (and later the 3.3 branch)?

> You could try using a different allocator than the default __pool_alloc.
>
>>  /sw/gcc-current/include/c++/3.4/bits/stl_alloc.h:516: warning: cast from `char*
>>    ' to `std::__pool_alloc<true, 0>::_Obj*' increases required alignment of
>>    target type
>
> The other option would be to properly align allocated memory.

I suppose that memory _is_ correctly aligned, else we'd get actual
run-time errors, not just warnings with -Wcast-align?

And if that is the case, isn't the problem that internally we use
char* in cases where we should use void*, at least when not doing
arithmetics?

> So
>
> 516:
>           __next_obj = (_Obj*)((char*)__next_obj + __n);
>
> probably comes from:
>
> 456:
>           _S_start_free = (char*) __new_alloc::allocate(__bytes_to_get);
>
> You'll need to figure out how to align this.

I'm afraid I'd need to enter areas of code quite unknown to me if I'm
going to try and fix this myself, but <some time later>:


The following hack really seems to work. and removes all warnings I got
for -Wcast-qual on sparc-sun-solaris2.9.  If I brush this up with a proper
ChangeLog and full testruns, would something like this be acceptable?

Gerald

--- stl_alloc.h.orig	Fri May  2 20:31:51 2003
+++ stl_alloc.h	Fri May  2 20:33:54 2003
@@ -450,6 +450,6 @@ namespace std
                 _S_free_list + _S_freelist_index(__bytes_left);

-              ((_Obj*)_S_start_free) -> _M_free_list_link = *__my_free_list;
-              *__my_free_list = (_Obj*)_S_start_free;
+              ((_Obj*)(void*)_S_start_free) -> _M_free_list_link = *__my_free_list;
+              *__my_free_list = (_Obj*)(void*)_S_start_free;
             }
           _S_start_free = (char*) __new_alloc::allocate(__bytes_to_get);
@@ -509,10 +509,10 @@ namespace std

       // Build free list in chunk.
-      __result = (_Obj*)__chunk;
-      *__my_free_list = __next_obj = (_Obj*)(__chunk + __n);
+      __result = (_Obj*)(void*)__chunk;
+      *__my_free_list = __next_obj = (_Obj*)(void*)(__chunk + __n);
       for (__i = 1; ; __i++)
         {
 	  __current_obj = __next_obj;
-          __next_obj = (_Obj*)((char*)__next_obj + __n);
+          __next_obj = (_Obj*)(void*)((char*)__next_obj + __n);
 	  if (__nobjs - 1 == __i)
 	    {



More information about the Libstdc++ mailing list