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
2
votes
3 answers

Advantages and disadvantages of using strdup on a string literal

I want to be clear about all the advantages/disadvantages of the following code: { char *str1 = strdup("some string"); char *str2 = "some string"; free(str1); } str1: You can modify the contents of the string str2: You don't have to…
ComtriS
  • 67
  • 2
  • 8
2
votes
2 answers

strdup invalid read of size 4 when string literal is ending with newline \n

I am getting an invalid read error when the src string ends with \n, the error disappear when i remove \n: #include #include #include int main (void) { char *txt = strdup ("this is a not socket terminated…
razz
  • 9,770
  • 7
  • 50
  • 68
2
votes
2 answers

Is there a way to free all members of a struct which have been strdup'd all at once?

For example I have the following struct: struct Student{ char *studentName; int studentAge; struct Student *next; }; I have many instances of the Student struct (for many different students) in a linked list. Each variable within…
Brandon
  • 401
  • 5
  • 20
2
votes
2 answers

How do you best utilize wcsdup?

I'm writing code and a good portion of it requires returning wchar arrays. Returning wstrings aren't really an option (although I can use them) and I know I can pass a pointer as an argument and populate that, but I'm looking specifically to return…
TMenninger
  • 33
  • 1
  • 5
2
votes
3 answers

What is the diffrence between char *str="this is a string" from char *str = strdup("this is a string") in C

What's the difference between the following code : char* str = "this is a string" From this one : char* str = strdup("this is a string") The usage scenarios ?
林开盛
  • 41
  • 6
2
votes
1 answer

Valgrind Memory Leak in strdup

I am doing a small Project. I am checking about memory leaks using the tool Valgrind. When I use this tool, I got the bellow information. > 584 bytes in 74 blocks are definitely lost in loss record 103 of 104 > ==4628== at 0x402BE68: malloc (in…
Smith Dwayne
  • 2,675
  • 8
  • 46
  • 75
2
votes
3 answers

Whether const char * and strdup serve the same function when used with getopt in C?

In the below code snippet can i replace char * to const char * and remove the strdup() function call and directly take the optarg value set by getopt()? I am advised to use const char * to skip the strdup function usage. Appreciate the help in…
Liju Mathew
  • 871
  • 1
  • 18
  • 31
2
votes
2 answers

mem-leak freeing g_strdup

I'm trying to free g_strdup but I'm not sure what I'm doing wrong. Using valgrind --tool=memcheck --leak-check=yes ./a.out I keep getting: ==4506== 40 bytes in 10 blocks are definitely lost in loss record 2 of 9 ==4506== at 0x4024C1C: malloc…
Mike
  • 33
  • 4
2
votes
2 answers

error: conflicting types for 'strdup'!

This is program in book “The c programming language”. There is an error:conflicting types for 'strdup'! When encounter function 'strdup'.But if you change 'strdup' to other name, for example 'strdu', the error will disappear. I don't know WHY? By…
Sparks_Fly
  • 429
  • 4
  • 15
2
votes
1 answer

Does malloc+strdup leak memory?

With: char *x = malloc(1024); strcpy(x, "asdf"); x = strdup(x); free(x); // OK free(x); // Segfault If I just free it once, will I still be leaking? And if so, how to avoid it?
Matoe
  • 2,742
  • 6
  • 33
  • 52
2
votes
1 answer

Which macro is being used with strdup on Linux?

I have seen strdup used in code samples on StackOverflow and have just tried to use it on Linux (3.0.0-21-generic x86_64). The compiler (clang) knew it was in string.h, but still complained about not having a prototype even with string.h included.…
Scooter
  • 6,802
  • 8
  • 41
  • 64
2
votes
1 answer

multiple calls to strdup() with the same lvalue

Throughout the programs I inherited from my predecessors, there are functions of the following format: somefunc(some_type some_parameter, char ** msg) In other words, the last parameter is a char **, which is used to return messages. That is:…
JochenVdB
  • 31
  • 3
1
vote
0 answers

mmap brokes after strdup

I tried the following configuration with mmap: open file (file is over 2 kB) request statistics from file *f_file* map file (file is smaller than a page, offset page 0, size is expected size) verify values of *f_footer* in the map *f_fpage* usage…
user1108078
  • 31
  • 1
  • 4
1
vote
3 answers

Typecasting string and strdup

If an input const string is being modified in some way (which is resulting in C compiler warning), what is the best way to handle it - typecasting it to a new variable and then using it OR duplicating it and using it and then freeing it. Or is there…
user32262
  • 8,660
  • 21
  • 64
  • 77
1
vote
1 answer

How to use strdup with user input?

#include #include int main() { char cipher[5][5] = { {'a', 'b', 'c', 'd', 'e'}, {'f', 'g', 'h', 'i', 'k'}, {'l', 'm', 'n', 'o', 'p'}, …
1 2
3
8 9