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

How to use Malloc and StrDup together when StrDup is in a while loop?

I have a code here. I am using malloc to allocate memory to my struct. One member of this struct is assigned a string using StrDup inside a while loop though other members dont have to change their values. Now, as I am using StrDup, I have to clean…
Abhineet
  • 5,320
  • 1
  • 25
  • 43
-1
votes
2 answers

strdup error: 'Operation now in progress'

What does the error mean? I just need to return the value I get from the redis command. int getReply(char* result) { redisContext *c; redisReply *reply; c = redisConnect((char*)"127.0.0.2", 6379); reply = redisCommand(c,"GET %s",…
Blub
  • 13,014
  • 18
  • 75
  • 102
-1
votes
1 answer

Segmentation fault when strdupa void pointer

I'm fairly new to pointers, and void pointers is still black art to me. In the following code I get a segfault when tmp->item = strdup(item); I'm not sure how to fix. int springmap_add(SpringMap *sm, char *key, void *item) { SpringMap *tmp =…
oldfart
  • 13
  • 6
-1
votes
1 answer

C String spliting into array, bad behaviour when string is not supposed to be split

when input has "|" the string splits normally, when str does not have "|" it seg faults char **cmds; if (strchr(input, '|')) cmds = split(input,'|'); else cmds[0] = strdup(input);
-1
votes
1 answer

gcc 9.3.0 preprocessor under Cygwin: cmdline -Dname but name seems to be undefined

I'm trying to build OCRmyPDF under Cygwin and have run into a brick wall. While I've been a developer my entire career, I've worked mostly in Java and have little knowledge of Python internals and C++. The problem might be obvious to an expert in…
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
-1
votes
1 answer

what is different between initialization of string and using strdup()

What is the difference between char *key_str="kiwi"; and char *key_str = strdup("kiwi"); For example: int strCmp(void *vp1, void *vp2) { char * s1 = (char *) vp1; char * s2 = (char *) vp2; return strcmp(s1, s2); } Why do those…
alexparkjw
  • 113
  • 1
  • 6
-1
votes
3 answers

faster way than memcpy to copy 0-terminated string

I have a question about duplicating a 0-terminated string: const char * str = "Hello World !"; size_t getSize = strlen(str); char * temp = new char[getSize + 1]; ... i know i can use this function memcpy(temp, str, getSize); but i want to use my…
myOwnWays
  • 59
  • 4
-1
votes
1 answer

Dereferencing pointer to char pointer c++

I have the following code: char **ptr; *ptr=strdup("This is a pointer"); cout<<*ptr<
niko
  • 1,128
  • 1
  • 11
  • 25
-1
votes
2 answers

Is strdup adding a '\0' when duplicating the array of char?

i would like to know if strdup adds a '\0' at the end of the new array if the src array does not contain one ? let's assume we have an array containing "hello" allocated by this kind of malloc malloc(sizeof(char) * 5); so 5 bytes for 5…
Anthony
  • 185
  • 1
  • 14
-2
votes
3 answers

How to convert my malloc + strcpy to strdup in C?

I am trying to save csv data in an array for use in other functions. I understand that strdup is good for this, but am unsure how to make it work for my situation. Any help is appreciated! The data is stored in a struct: typedef struct current{ …
TinMan
  • 99
  • 2
  • 13
-2
votes
1 answer

Memory resource ( strdup )

I call strdup to duplicate the 'card' string in set_device( devname ) set_device( devname ) and then I use 'card' to open mixer: devname is in format hw:0/Mic static char *card, *channel; static snd_mixer_t *handle = NULL; static snd_mixer_elem_t…
user1840007
  • 615
  • 1
  • 10
  • 19
-2
votes
1 answer

pointer to pointer to structure with malloc and strdup

My main intention is to pass a pointer to a structure, to a function, which will allocate memory and fill all its values. After returning back i will print it on screen. Structure looks like this. struct isolock{ char *name; unsigned int…
-3
votes
1 answer

Calling free() on a variable initialised by strdup still results in memory leak

I have the following code: static const char * path[2]; int main(int argc, char *argv[]) { // validate argument char * temp = dirname(dirname(strdup(argv[optind]))); path[0] = temp path[1] = NULL; // do stuff …
User3952
  • 7
  • 2
-3
votes
1 answer

Value of type error in C++ function always NULL

i have to do my function always.I can not using standart library. My_cpy , my_len and my_strdup function in here. Please check it for me. I think it is easy but i have a problem about this function. I showed the error at the end of the page. I think…
Ugur Baki
  • 105
  • 4
-3
votes
2 answers

C++ release allocated char* into string

I have tried to find an answer but couldn't see anything straight forward. How do I free the allocated memory in the next snippet code: const char* attStr = strdup(OtherCharStr); string str(attStr, strlen(attStr)); delete str; //???
eladyanai
  • 1,063
  • 2
  • 15
  • 34
1 2 3
8
9