Questions tagged [strdup]

The strdup() function duplicates a string

The strdup() function, available in various languages, returns a pointer to a new string which is a duplicate of the original string.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb759969(v=vs.85).aspx
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/strdup.3.html
http://linux.die.net/man/3/strdup

122 questions
330
votes
11 answers

strdup() - what does it do in C?

What is the purpose of the strdup() function in C?
Manoj Doubts
  • 13,579
  • 15
  • 42
  • 45
90
votes
6 answers

strcpy vs strdup

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?
johan
  • 1,943
  • 10
  • 31
  • 43
70
votes
7 answers

strdup or _strdup?

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…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
20
votes
4 answers

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

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…
dark_perfect
  • 1,458
  • 1
  • 23
  • 41
17
votes
4 answers

strdup(): Confused about warnings ('implicit declaration', 'makes pointer...without a cast', memory leak)

When I compile the short piece of code below (in which we define a string and then use strdup to make a copy), I get 3 warnings: 2 compiler warnings from GCC and 1 run-time warning/error from valgrind. I suspect the memory leak error (reported by…
iceman
  • 2,020
  • 2
  • 17
  • 24
15
votes
7 answers

strdup() function

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,…
Chris Lutz
  • 657
  • 2
  • 6
  • 8
15
votes
6 answers

'memdup' function in C?

In C, you can use strdup to succinctly allocate a buffer and copy a string into it. As far as I'm aware, however, there is no similar function for general memory. For example, I can't say struct myStruct *foo = malloc(sizeof(struct…
Dan
  • 2,952
  • 4
  • 23
  • 29
13
votes
4 answers

How do I use strdup?

I am calling strdup and have to allocate space for the variable before calling strdup. char *variable; variable = (char*) malloc(sizeof(char*)); variable = strdup(word); Am I doing this right? Or is there something wrong here?
Ivan Zhang
  • 177
  • 1
  • 1
  • 7
12
votes
3 answers

How to avoid "null argument where non-null required" compiler warning

Compiling the following code: #include #define FOO (NULL) int main(int argc, char *argv[]) { char *foo; if (FOO) foo = strdup(FOO); return 0; } results in the following compiler warning: foo.c: In function…
profzoom
  • 297
  • 1
  • 2
  • 10
11
votes
1 answer

Should I free strdup pointer after basename/dirname in C?

I want to use POSIX's basename function (as opposed to GNU's). From the man page: Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions. These functions may…
pakman
  • 1,676
  • 3
  • 23
  • 41
10
votes
3 answers

strdup and memory leaking

Does strdup allocate another memory zone and create another pointer every time? For example: does the following code result in a memory leak? void x(char** d, char* s){ *d = strdup(s); } int main(){ char* test = NULL; x(&test, "abcd"); …
Zack
  • 385
  • 2
  • 3
  • 21
8
votes
1 answer

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

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…
prosseek
  • 182,215
  • 215
  • 566
  • 871
7
votes
4 answers

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

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,…
user34537
7
votes
5 answers

alternative to strdup

I'm writing a C++ class for a book that contains a name: class Book { private: char* nm; .......... ............ .......... ........... }; I am not allowed to use std::string in this assignment. So here I am using strdup to copy the value of…
aherlambang
  • 14,290
  • 50
  • 150
  • 253
5
votes
10 answers

How could this C fragment be written more safely?

I have the following C code fragment and have to identify the error and suggest a way of writing it more safely: char somestring[] = "Send money!\n"; char *copy; copy = (char *) malloc(strlen(somestring)); strcpy(copy,…
Adam Taylor
  • 7,534
  • 8
  • 44
  • 54
1
2 3
8 9