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

Why we didn't use * during dereferencing the pointer?

In the below code, we get a pointer from strdup(source) and we store it in a pointer named target. Now, when we print the string using pointer, we don't add * at the beginning of the pointer: why is it so? As I studied whenever we want to…
user17804723
1
vote
1 answer

Freeing a strdup inside of a linked list

I have a linked list in which I am trying to free a char* which I used strdup for. These are the methods I have for adding a node into my list: node* add_first(char* name, node* head) { node* new_node; new_node =…
SteamedBun
  • 91
  • 9
1
vote
1 answer

How to free this strdup?

I am using strdup to duplicated the value inside command. I also free it at the end of my loop to make sure I don't have anything leaking but valgrind seems to disagree with me and says that what is allocated with this strdup is leaking. Any…
Yohan
  • 145
  • 1
  • 10
1
vote
2 answers

C array of structure (exception thrown)

I have created an array of structure Human which consists of char *name. I use function like this: Human *createHuman(char *name){ Human *h = malloc(sizeof(Human)); h->name = strdup(name); return h; } I have tested this function, it…
MirEso
  • 27
  • 4
1
vote
2 answers

C programming - Strdup not capturing and storing filenames in array correctly

I'm trying to store file names in an array. The array is in a struct, and I want to store the names of files found in a directory in the array. However, the process that I'm using to store the names seems to be corrupting 2 or 3 of the names during…
N. Adams
  • 37
  • 6
1
vote
2 answers

how to save a string token , save its content to an array, then use those contents for further comparison

/*I am unsure if my code for saving the tokens in an array is accurate. This is so because been whenever I run my program, the code to compare token[0] with my variable doesn't give an output nor perform assigned function. Hence I am sure there is…
Atinuke
  • 191
  • 2
  • 4
  • 11
1
vote
0 answers

string in bison bison flex strdup

im have a flex-bison project working on ubuntu linux vmPlayer. Problem is, I'm trying to use map of string and int in my bison file for int-string casting, and it gives me error in file extra.y: %{ #include #include
mooly
  • 113
  • 1
  • 8
1
vote
1 answer

A templated 'strdup()'?

template static T *anydup(const T *src, size_t len) { T *ptr = malloc(len * sizeof(T)); memcpy(ptr, src, (len * sizeof(T))); return ptr; } Is this proper? Can I expect any errors from this when using an int, long, etc.? I'm…
Phobos D'thorga
  • 439
  • 5
  • 17
1
vote
2 answers

Issue with threads: value stored in heap

I have an issue with threads. I am defining a global variable, a char * that I initialize to NULL, and a mutex. pthread_mutex_t mutex; char *minURLTime; minURLTime = NULL; Then I initialize my mutex: pthread_mutex_init(&mutex, NULL); I then create…
Jary
  • 11
  • 2
1
vote
1 answer

Use of strdup() in flex

I want to send duplicate string to my bison file. In my flex file I use it like this "<" {return strdup(tOPEN);} ">" {return strdup(tCLOSE);} Is this right things to do? Or should I use it like below the code. "<" {…
M.J.Watson
  • 470
  • 7
  • 18
1
vote
1 answer

How to free strdup'd char* flexible array members stored within a struct?

I have char* flexible array member stored within a stuct. Each of the values within the flexible array member have been strdup'd. I am unsure as how to free each of the strdup'ed values since I do not know exactly how many elements I have stored in…
John
  • 105
  • 1
  • 8
1
vote
5 answers

strdup() causing memory leaks?

I've implemented a function that returns a string. It takes an integer as a parameter (age), and returns a formatted string. All is working well, except from the fact that I have some crazy memory leaks. I know strdup() is the cause of this, but…
user3186023
1
vote
1 answer

Problems grabbing file names using SDL_strdup and similar

I'm trying to create a program with SDL2. In a certain part of the code, I'm writing functions to grab names of all present files in a given directory path (and keep them in memory) so that, in another function, I can check if a specified file was…
user4103007
1
vote
6 answers

What is wrong with this c strdup code?

Consider this code: char *strs[] = { "string1", "string2", NULL }; char *ptr1 = NULL, *ptr2 = NULL, *tmp; short iter = 0; tmp = ptr1; while (iter < 2) { tmp = strdup(strs[iter]); tmp = ptr2; iter++; } printf("1: %s\n2: %s\n", ptr1,…
user318747
  • 1,418
  • 2
  • 16
  • 29
1
vote
1 answer

list[idx] = strdup(tok[1] +tok[2]) or initial malloc to size of list -C

I have a code that has an invalid write in valgrind. What I tried to do was strcat the idx I malloc'd. I would like to know what size to strdup() so I can put spaces in between the number of tokens, which are the in the char *argv[]. Here is a small…
user4139475
1 2 3
8 9