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
1
vote
1 answer

operands of = have illegal types

I am very new to C (after many years). I am using lcc64 and have the following statement char *logMessage = strdup(argv[1]); I have no idea why I get the error "operands of = have illegal types 'pointer to char' and 'int" Any suggestions?
Carmelo
  • 11
  • 1
  • 2
1
vote
6 answers

c++ char* converted from a string using strdup doesn't equal original raw string

What I'm wondering is why converting a string to a char* seems to make the new char* not equal to the literal string it came from. If I have: //raw versions of the string: string s = "fun"; char* c = "fun"; char* s_convert = strdup(s.c_str());…
xgord
  • 4,606
  • 6
  • 30
  • 51
1
vote
3 answers

Free() inside a while loop

I'm trying to run this program where a character array is created and allocated memory dynamically. Later the array elements are populated with the string "hello" for 10 consecutive locations. The values are assigned to the string elements using…
Skanda
  • 77
  • 1
  • 3
  • 7
1
vote
2 answers

Using strdup into malloc reserved space

I've never used malloc to store more than values but I have to use strdup to order the lines of an input file and I dont get a way to make it work. I though using strdup() to get a pointer to each line and later, put each one into a space according…
sui
  • 120
  • 6
1
vote
4 answers

risk with returning strdup from a function with return type as char *?

If I want to return strdup from a function whose return type is char*, then what are the risks or chances of memory leak ? char* fun () { return strdup("hello"); } int main() { for(;;) printf("%s\n", fun()); }
anand
  • 267
  • 1
  • 3
  • 16
1
vote
2 answers

How to free the leak caused by strdup?

#include #include #include #include #include #include bool debugOpt=false; int main (int argc, char **argv) { (void) argc; char *progname = basename (argv[0]); char…
Gavin Z.
  • 423
  • 2
  • 6
  • 16
1
vote
3 answers

Odd error using strdup in C

I'm trying to use the strdup() function in C but I'm getting an odd error involving malloc. My condensed code is: void loadEntity(FILE *inFP, entity_t *ent, char *token) { char buffer[100] = "buffer"; if (strcmp(token, "name") == 0) { if…
Jaghancement
  • 151
  • 1
  • 5
  • 15
1
vote
1 answer

Crash in strdup() on Ubuntu

My C program gives segmentation fault when I try to run on Ubuntu.Here is the stack trace.Any help is appreciated 0 0x015383f1 in ?? () from /lib/tls/i686/cmov/libc.so.6 #1 0x01538075 in strdup () from /lib/tls/i686/cmov/libc.so.6 #2 0x00c0a4af…
G G
  • 1,049
  • 4
  • 17
  • 26
0
votes
3 answers

when strdup function fails?

i have following code which use strdup function #include #include #include char source[] = "The Source String "; int main() { char *dest; if ((dest = _strdup(source)) == NULL) { fprintf(stderr, " Error…
user466534
0
votes
2 answers

std::find does not work when using strdup

I am using a std::vector to store some strings, later I try to std::find them but passing through strdup, as shown in the sample code, It does not work, std::find returns last, which means it did not find the string, but I can see that it is there,…
0
votes
3 answers

Will the following use of strdup() cause a memory leak in C ?

char* XX (char* str) { // CONCAT an existing string with str , and return to user } And i call this program by: XX ( strdup("CHCHCH") ); Will this cause a leak while not releasing what strdup() generates ? It's unlikely that…
daisy
  • 22,498
  • 29
  • 129
  • 265
0
votes
1 answer

Seeking Help to Identify Memory Leaks in my C Function

I'm currently trying to implement the setenv function in C that sets the value of an environment variable, and I'm concerned about memory leaks in my code flagged by Valgrind due to strdup. I would greatly appreciate your insights in helping me…
Crezcenz
  • 5
  • 2
0
votes
0 answers

Review assembly strdup reimplementation

I am posting a message here because I am new to assembly programming. My goal today was to recode a strdup in assembler, so to save my first parameter which is a string (const char*), I have doubts when handling the RSP to reserve space for the…
user18129922
0
votes
0 answers

How to use strdup and strcat in C

I am trying to concatenate two string in C Here is my program, server->vhost.root_dir is char * initialized with strdup. char *path = strdup(server->vhost.root_dir); if(path == NULL) …
Nicolas
  • 31
  • 5
0
votes
1 answer

Use pointer to add value to linked list without using strdup() in C

How can I replace strdup() with strcpy() and malloc() in linked list in c?
1 2 3
8 9