strdup

strdup or _strdup?

六眼飞鱼酱① 提交于 2019-11-28 04:49:53
When I use strdup in Microsoft Visual C++, it warns me: warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details. Thus it seems _strdup is correct. But when I use _strdup in GCC (Fedora Linux OS), the compiler shows an error: error: ‘_strdup’ was not declared in this scope With GCC and Linux, compiler does not show any error for strdup . Which is correct - strdup or _strdup ? Note: I include <string.h> in my code. Cheers and hth. - Alf strdup is not a standard C++ function. but it is apparently a Posix

strdup error on g++ with c++0x

余生颓废 提交于 2019-11-28 01:02:05
问题 I have some C++0x code. I was able to reproduce it below. The code below works fine without -std=c++0x however i need it for my real code. How do i include strdup in C++0x? with gcc 4.5.2 note i am using mingw. i tried including cstdlib, cstring, string.h and tried using std::. No luck. >g++ -std=c++0x a.cpp a.cpp: In function 'int main()': a.cpp:4:11: error: 'strdup' was not declared in this scope code: #include <string.h> int main() { strdup(""); return 0; } 回答1: -std=gnu++0x (instead of

freeing the string allocated in strdup() from flex/bison

时间秒杀一切 提交于 2019-11-28 00:49:51
问题 I have flex code that copies a string lexeme using strdup() . %{ #include "json.tab.h" #define YY_DECL extern "C" int yylex() %} %option noyywrap %% [ \t\n]+ ; \"[a-zA-Z]+\" {yylval.sval = strdup(yytext); return STRING; } [0-9]+ {yylval.ival = atoi(yytext); return NUMBER; } . {return yytext[0];} ; %% strdup() allocates memory and copies the input string into it and return (strdup() - what does it do in C?), so I guess I need to free it up when I don't need it anymore. From this post:When is

strdup() function

此生再无相见时 提交于 2019-11-27 09:25:43
I recently became aware that the strdup() function I've enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don't want to rewrite all my code, so I think I'm just going to write my own strdup() function. It's not that hard, really, it's just a malloc() and a strcpy() . Anyway, I have the function, but what am I doing if I write this function and link it to my code, and it already exists in the libc? Will my linker or compiler allow me to basically define my own version of the function, or do I have to give it another name? It would be terribly convenient if there was a

Obj-C: Memory Leak of Malloc 48 bytes in strdup frame

别来无恙 提交于 2019-11-27 05:40:17
问题 In my app, I have am receiving multiple memory leaks. The object is Malloc 48 bytes, and it always originates from the responsible caller strdup. The history of the object only shows it being Malloced, and no other retains or releases. The stacktrace doesn't show any of my code. The only bits of relevance that I can pick out are: 10 UIKit _UIGestureRecognizerSendActions 9 UIKit -[UIScrollView handlePan:] 8 UIKit -[UIScrollView _endPanWithEvent:] 7 UIKit -[UIScrollView(Static) _startTimer:] 6

strdup or _strdup?

筅森魡賤 提交于 2019-11-27 00:33:55
问题 When I use strdup in Microsoft Visual C++, it warns me: warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details. Thus it seems _strdup is correct. But when I use _strdup in GCC (Fedora Linux OS), the compiler shows an error: error: ‘_strdup’ was not declared in this scope With GCC and Linux, compiler does not show any error for strdup . Which is correct - strdup or _strdup ? Note: I include <string.h>

strcpy vs strdup

六月ゝ 毕业季﹏ 提交于 2019-11-26 23:54:50
I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string. Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup ? Abdul Muheedh strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++) where as strdup is equivalent to ptr2 = malloc(strlen(ptr1)+1); strcpy(ptr2,ptr1); ( memcpy version might be more efficient) So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough. The functions strcpy

strdup() function

∥☆過路亽.° 提交于 2019-11-26 17:50:01
问题 I recently became aware that the strdup() function I've enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don't want to rewrite all my code, so I think I'm just going to write my own strdup() function. It's not that hard, really, it's just a malloc() and a strcpy() . Anyway, I have the function, but what am I doing if I write this function and link it to my code, and it already exists in the libc? Will my linker or compiler allow me to basically define my own version

strcpy vs strdup

风流意气都作罢 提交于 2019-11-26 09:17:51
问题 I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string. Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup ? 回答1: strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++) where as strdup is equivalent to ptr2 = malloc(strlen(ptr1)+1); strcpy(ptr2,ptr1); (memcpy version might be more efficient) So if you want the string which you have copied to be used in another function (as

strdup() - what does it do in C?

吃可爱长大的小学妹 提交于 2019-11-25 21:57:49
问题 What is the purpose of the strdup() function in C? 回答1: Exactly what it sounds like, assuming you're used to the abbreviated way in which C and UNIX assigns words, it duplicates strings :-) Keeping in mind it's actually not part of the ISO C standard itself (a) (it's a POSIX thing), it's effectively doing the same as the following code: char *strdup(const char *src) { char *dst = malloc(strlen (src) + 1); // Space for length plus nul if (dst == NULL) return NULL; // No memory strcpy(dst, src)