: This article mainly introduces the invalid memsetsizeof function when PHP calls the C ++ extension. if you are interested in the PHP Tutorial, refer to it. Problem: In C ++, I used to use the memset function to initialize arrays, for example:
Int sz= 100; int * a = (int *) malloc (sizeof (int) * sz); memset (a, 0, sizeof ());
The code above dynamically opens up an int array a with the size of sz, and assigns the opened memory to the ASCII code 0.
When PHP calls this C ++ code (using the url to call the PHP function), it is found that the memset function does not work, and the memory of array a is not assigned a value, resulting in many program errors, found this problem
Cause: The root cause is that when PHP calls C ++, sizeof (a) does not return the size of a pointing to the continuous memory space, but only the size of the pointer itself, therefore, memset does not play its due role.
Solution: in the third parameter of memeset, use sizeof (int) * sz to replace sizeof (a) and pass in the actual size of the array as a parameter so that the memset function can take effect.
When PHP calls the C ++ extension, many library functions related to pointers in C ++ may encounter problems and sometimes have to write them by themselves, the reason may be the memory management mechanism problem when PHP calls the extension.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above describes the invalid memset/sizeof function when PHP calls the C ++ extension, including some content, and hope to be helpful to PHP tutorials.