Corrupted heap on 64 bit platforms using Ipp32f** and ippsMalloc_32f to allocate memory

Corrupted heap on 64 bit platforms using Ipp32f** and ippsMalloc_32f to allocate memory

I have a very simple problem. A program like folows works on 32 bit platforms Windows and linux. On 64 bi platforms the program results in a corrupted heap. Example for the program:

************************************************

Ipp32f** x = (Ipp32f**)ippsMalloc_32f(19);

for( int i = 0; i < 19; i++ ) {
  x[i] = (Ipp32f*)ippsMalloc_32f(100);
  for( int k = 0; k < 100; k++ ) {
    x[i][k] = 0.0;
  }
}

for( int j = 0; j < 19; j++ ) {
  if( x[j] != NULL ) {
    ippsFree(x[j]);
    x[j] = NULL;
  }
}
ippsFree(x);
x = NULL;

************************************************

In the reference manual I found something like
...
Ipp32f* ippsMalloc_32f(int len);
...

Example
The following example shows how to use the function ippsMalloc_8u:
void func_malloc(void) {
Ipp8u* pBuf = ippsMalloc_8u(8*sizeof(Ipp8u));
if(NULL == pBuf)
   // not enough memory
ippsFree(pBuf);
}

I can not really believe that also the sizeof(IppXYZ) has to be multiplied when calling the ippsMalloc_XYZ(...) function.

I guess the corrupted heap in my small programm is the result of the large adressroom
- on 64 bit platforms: sizeof(Ipp32f*) = 8 , sizeof(Ipp32f) = 4
- on 32 bit platforms: sizeof(Ipp32f*) = 4 , sizeof(Ipp32f) = 4

If I use
Ipp32f** x = (Ipp32f**)ippsMalloc_32f(19 * sizeof(Ipp32f*)/sizeof(Ipp32f));
instead of
Ipp32f** x = (Ipp32f**)ippsMalloc_32f(19);
the program works perfect on all platforms (32 and 64 bit).

Has anybody an idea what's going wrong?

publicaciones de 4 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

Hello,

In the 64 bit platform, the size of point is 64 bits( 8 bytes). so it should be:
Ipp32f** x = (Ipp32f**)ippsMalloc_64f(19);

Thanks,
Chao

Hi Peter,

The first line of your sample is not clear.

Ipp32f** x = (Ipp32f**)ippsMalloc_32f(19);

If your are going to allocate room for 19 pointers to Ipp32f (19 * Ipp32f*), you should ask for at least 19*sizeof(Ipp32f*) bytes. On 32-bit system it will be 76 bytes, on 64-bit - 152 bytes.
In any case the required space depends on size of pointer in you particular system.
What if you got 128-bit system? ))

Assumption that sizeof(Ipp32f*) is equal to sizeof(Ipp32f) is wrong. ippsMalloc_32f allocates memory for 32-bit variables only. I wouldn't use IPP allocation functions for that. Or, you can use ippsMalloc_8u(19 * sizeof(Ipp32f*)). Here, Malloc_8u is similar to conventional "malloc" function.

Regards,
Sergey

Regards,
Sergey

Thanks for the comments.

I discussed the problem again with a friend of mine. The correct way to access *PointerArrays* should be:

Ipp32f** x = (Ipp32f**)malloc(19 * sizeof(Ipp32f*));
for( int i = 0; i < 19; i++ ) {
x[i] = (Ipp32f*)ippsMalloc_32f(100);
for( int k = 0; k < 100; k++ ) {
x[i][k] = 0.0;
}
}
...
for( int j = 0; j < 19; j++ ) {
if( x[j] != NULL ) {
ippsFree(x[j]);
x[j] = NULL;
}
}
free(x);
x = NULL;

Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya