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?
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…
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…
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…
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,…
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…
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?
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…
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…
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");
…
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,…
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…
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,…