#192 _aligned_malloc() prototype not always available

open
nobody
header (101)
5
2014-05-15
2010-09-15
Ozkan Sezer
No

Consider the following code:

[headers headers headers]
void *foo () {
return _aligned_malloc(sizeof(void *), 16);
}

If te headers section looks like:

#include <stdlib.h>

... or like:

#include <stdlib.h>
#include <windows.h>

... the code gets the protoypes of _aligned_malloc()
and _aligned_free() and it compiles without warnings.

However, if the headers section looks like:

#include <malloc.h>

... or like:

#include <intrin.h>
#include <stdlib.h>

... or like:

#include <windows.h> /* causes intrin.h to be included */
#include <stdlib.h>

... the code does NOT get the protoypes of _aligned_malloc()
and _aligned_free() and warnings are emmitted for C or an error
for C++.

The issue is due to the x86intrin.h workarounds in stdlib.h and
malloc.h. Those workarounds break existing code. At present,
the only way to get the prototypes of _aligned_malloc() and
_aligned_free() is to make sure that stdlib.h is included before
other mentioned headers.

Discussion

  • GEN

    GEN - 2010-09-16

    [Avatar]
    2010-09-16 14:56:41 IST
    I am getting same error for following code also #include <iostream> #include <fstream> #include <iomanip> #include <sstream> #include <string> #include <ctime> #include <cmath> #include <time.h> #include <stdlib.h> #include <string.h> #include <vector> #include <malloc.h> #include <CL/opencl.h> #ifdef _WIN32 #include <windows.h> #else #include <sys/time.h> #include <linux/limits.h> #endif #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) #define _aligned_malloc __mingw_aligned_malloc #define _aligned_free __mingw_aligned_free #endif //MINGW #if defined (_WIN32) output = (cl_uint *)_aligned_malloc(sizeof(cl_uint4), 16); #else output = (cl_uint *)memalign(16, sizeof(cl_uint4)); #endif

     
  • Ozkan Sezer

    Ozkan Sezer - 2010-09-16

    genaganna: I don' have the OpenCL stuff, so I tried with the following:

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    #include <ctime>
    #include <cmath>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <vector>
    #include <malloc.h>
    /*#include <CL/opencl.h>*/
    #ifdef _WIN32
    #include <windows.h>
    #else
    #include <sys/time.h>
    #include <linux/limits.h>
    #endif
    #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
    #define _aligned_malloc __mingw_aligned_malloc
    #define _aligned_free __mingw_aligned_free
    #endif /* MINGW */
    /*
    #if defined (_WIN32)
    output = (cl_uint *)_aligned_malloc(sizeof(cl_uint4), 16);
    #else
    output = (cl_uint*)memalign(16, sizeof(cl_uint4));
    #endif
    */
    void *foo () {
    return _aligned_malloc(sizeof(void *), 16);
    }

    /opt/cross_win64/bin/x86_64-w64-mingw32-g++ -Wall -W -O2 -c test.cc

    ... and I don't see any warnings at all.

     
  • GEN

    GEN - 2010-09-16

    You can find OpenCL headers here : http://www.khronos.org/registry/cl/

    It looks like OpenCL headers are including some headers which creates this problem again.

     
  • Ozkan Sezer

    Ozkan Sezer - 2010-09-16

    Well, still no errors even with the opencl headers:

    $ ls -l
    total 8
    drwxrwxr-x 2 ozzie ozzie 4096 2010-09-16 17:16 CL
    -rw-rw-r-- 1 ozzie ozzie 727 2010-09-16 17:13 test.cc
    $ cat test.cc
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    #include <ctime>
    #include <cmath>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <vector>
    #include <malloc.h>
    #include <CL/opencl.h>
    #ifdef _WIN32
    #include <windows.h>
    #else
    #include <sys/time.h>
    #include <linux/limits.h>
    #endif
    #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
    #define _aligned_malloc __mingw_aligned_malloc
    #define _aligned_free __mingw_aligned_free
    #endif /* MINGW */
    /*
    #if defined (_WIN32)
    output = (cl_uint *)_aligned_malloc(sizeof(cl_uint4), 16);
    #else
    output = (cl_uint*)memalign(16, sizeof(cl_uint4));
    #endif
    */
    void *foo () {
    return _aligned_malloc(sizeof(void *), 16);
    }
    $ /opt/cross_win64/bin/x86_64-w64-mingw32-g++ -Wall -W -O2 -I. -c test.cc
    In file included from ./CL/opencl.h:44,
    from test.cc:13:
    ./CL/cl_gl_ext.h:44:4: warning: "/*" within comment
    $

     
  • GEN

    GEN - 2010-09-16

    Try following to reproduce the issue. I just added CL/cl.h header on the top of your code.

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #include <CL/cl.h>

    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    #include <ctime>
    #include <cmath>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>
    #include <vector>
    #include <malloc.h>
    #include <CL/opencl.h>
    #ifdef _WIN32
    #include <windows.h>
    #else
    #include <sys/time.h>
    #include <linux/limits.h>
    #endif
    #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
    #define _aligned_malloc __mingw_aligned_malloc
    #define _aligned_free __mingw_aligned_free
    #endif /* MINGW */

    /*
    #if defined (_WIN32)
    output = (cl_uint *)_aligned_malloc(sizeof(cl_uint4), 16);
    #else
    output = (cl_uint*)memalign(16, sizeof(cl_uint4));
    #endif
    */
    void *foo () {
    return _aligned_malloc(sizeof(void *), 16);
    }
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Following error i am getting

    BinarySearch.cpp: In function 'void* foo()':
    BinarySearch.cpp:35: error: '_aligned_malloc' was not declared in this scope

     
  • Ozkan Sezer

    Ozkan Sezer - 2010-09-16

    > Try following to reproduce the issue. I just added CL/cl.h header on the
    > top of your code.
    >
    > #include <CL/cl.h>
    >

    Well, that would obviously fail. That much is evident and this bug
    entry already reports that. Workaround for now is, as recommended,
    including stdlib.h before other headers that would include the intrinsic
    headers in some way. If you are trying to compile a code and not
    trying to make it fail on purpose, then please move that <CL/cl.h>
    include down, just above the other <CL/opencl.h> include.

     
  • GEN

    GEN - 2010-09-16

    Ozkan,

    I fixed my code already as you suggested. I have provided this test case to verify after fixing this bug. Thank you very much for helping me to fix all my issues.

     
  • Ozkan Sezer

    Ozkan Sezer - 2010-09-16

    > Ozkan,
    >
    > I fixed my code already as you suggested. I have provided this
    > test case to verify after fixing this bug. Thank you very much for helping
    > me to fix all my issues.

    Glad to know that your issues are fixed. Now we have to fix our stuff
    so that include order wouldn't matter for this.

     
  • NightStrike

    NightStrike - 2010-10-10

    Is our stuff fixed?

     
  • Ozkan Sezer

    Ozkan Sezer - 2010-10-10

    No, nothing is fixed so far. Fix would probably involve revisions in MS intrinsics, so this is best suited for Kai.

     
  • Jose Fonseca

    Jose Fonseca - 2011-10-12

    Please see the example I provided in https://sourceforge.net/tracker/?func=detail&atid=983354&aid=3422509&group_id=202880 .

    The issue seems to be related to _MM_MALLOC_H_INCLUDED being defined in intrin.h which is included indirectly by windows.h.

    But I'm not sure what's the best way to fix this....

    .. it looks to me that nothing ever defines a macro named "_aligned_malloc", so it should be safe to always declare it (numerous times even).

     

Log in to post a comment.