The memset/sizeof function is invalid when PHP calls the C ++ extension.

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.