Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I've already fallen for that one, or rather, thought it up, found it didn't work and quickly realised why (it doesn't work for the same reason that [tt]sizeof(*a)[/tt] does work, right?).xwb said:Try
Note that this does not work if array is a pointer. It is a very common mistake.Code:sizeof (array) / sizeof (array[0])
char* pString = malloc( 80 + sizeof( int ) );
int* pSize = (int*)pString;
*pSize = 80;
pString += sizeof( int );
void SomeFunc( char* pString )
{
int size = *(pString - sizeof( int ));
memset( pString, '\0', size );
...
}
struct myStr {
size_t allocSize;
size_t usedSize;
char *string;
};
void* Malloc( size_t size )
{
void* ptr = malloc( size + sizeof( size_t ) );
size_t* pSize = (size_t*)ptr;
*pSize = size;
return (ptr + sizeof( size_t ));
}
size_t PtrLength( void* ptr )
{
return *(ptr - sizeof( size_t ));
}
void Free( void* ptr )
{
free( ptr - sizeof( size_t ) );
}