Mahesh B. Mane

Hi Friends.....

Is there any function to know the number of bytes allocated to a pointer.

Ex:

foo()

{

int *intList = NULL;

intLIst = (int *)malloc(sizeof(int) * 10);

freeList(intList);

}

freeList(int *intList)

{

/*How to collect the total number of bytes allocated for intList*/

free(intList);

}

Thanks....



Re: Visual C++ General How to know the Number of Bytes allocated to a Pointer.

Marius Bancila

No, there isn't. When you pass a pointer to an array of characters to a function, also pass the number of elements.

void foo(int* array, int len)

{

  for(int i = 0; i < len; i++)

  {

     // do something with array[ i ]

  }
}

int main()

{

   int buffer[200];

   foo(&buffer[0], 200);
}






Re: Visual C++ General How to know the Number of Bytes allocated to a Pointer.

Viorel.

I case of Visual Studio 2005 and memory blocks allocated with malloc, calloc or realloc functions (but probably not by new), for debug purpose I think you can try _msize function [http://msdn2.microsoft.com/en-us/library/z2s077bc(VS.80).aspx].

 

I hope this helps.

 





Re: Visual C++ General How to know the Number of Bytes allocated to a Pointer.

Mahesh B. Mane

Thanks..

The _msize works fine in VisualStudio. Is there any standard function for the same functionality; I mean ANSI standard. Because _msize may not be supported on different compilers.