Why does php RETURN_STRINGL use a static string with Segmentationfault?

Source: Internet
Author: User
My c basics are not very good. Please know for details. Thank you. If I directly use code similar to the following, A segfault error will occur {code ...} no matter whether ret is writing a string directly or initializing it to char [100], it will not work, but you only need to slightly improve the program and use dynamic allocation... my c basics are not very good. Please know more. Thank you.

If I use code similar to the following, A segfault error occurs.

char* ret = "hello world";RETURN_STRINGL(ret, strlen(ret), 0);

No matter whether ret directly writes a string or first initializes it to char [100 ],

But as long as the program is slightly improved, it will be okay to use dynamic memory allocation:

char* hello = "hello world";int   len = strlen(hello);char* ret = (char*)emalloc(len);memcpy(ret, hello, len);RETURN_STRINGL(ret, len, 0);

I found that the third parameter of RETURN_STRINGL is changed to 1 and there will be no cross-border access errors.

Reply content:

My c basics are not very good. Please know more. Thank you.

If I use code similar to the following, A segfault error occurs.

char* ret = "hello world";RETURN_STRINGL(ret, strlen(ret), 0);

No matter whether ret directly writes a string or first initializes it to char [100 ],

But as long as the program is slightly improved, it will be okay to use dynamic memory allocation:

char* hello = "hello world";int   len = strlen(hello);char* ret = (char*)emalloc(len);memcpy(ret, hello, len);RETURN_STRINGL(ret, len, 0);

I found that the third parameter of RETURN_STRINGL is changed to 1 and there will be no cross-border access errors.

@ Pipiru's answer is totally wrong. It is purely misleading. I didn't expect it to be accepted yet. "Hello world" is a static string, not in the stack. Char * hello only points to it.

The essence of this problem is:
Php itself is a type-safe scripting language. For strings returned by RETURN_STRINGL or RETURN_STRING, php will be free when appropriate. Therefore, programmers must ensure that the returned strings are free in the heap, this is why dynamic allocation is okay. And:

Char * ret = "hello world ";
RETURN_STRINGL (ret, strlen (ret), 0 );

This directly returns a static string, causing an error when php returns the free string.

RETURN_STRINGL and RETURN_STRING are the last parameters. If the value is 1, a copy of the string in the first parameter is copied and returned in the heap. This is why the program is normal when the last parameter is equal to 1.

Obviously, this is a problem of C language understanding. All local variables in the function are allocated in the memory.StackOn
It is uncontrollable, And the compiler controls the released memory area. It is usually released after the function is completed.
If you want to allocate a block of memory for out-of-function calls, you must usemallocTo explicitly allocate memory, of course, you need to rememberfreeDrop.

That's it. The third parameter is whether to copy

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.