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
5
votes
1 answer

strdupa() in C - Dangers and Duplicates

I make programs in C. I read about the strdup() function. From what I could tell, the strdup() function allocates space while the strcpy() does not. But the problem with strdup() is it allocates space but does not free it. strdupa() allocates and…
user5485048
5
votes
3 answers

Where to free memory in Bison/Flex?

I'm using Bison & Flex for 1 month more or less, so I'm sorry if I don't see something obvious (but I don't think it is). I have a problem about freeing memory with Flex Bison. Here is what my code looks like: parser.l {DATE} { yylval.str=…
blackmesa
  • 250
  • 4
  • 14
4
votes
2 answers

Why is strdup() used here?

I am reading "Operating System: Three Easy Pieces". In chapter 5, there is a piece of code that show the usage of exec() system call. 1 #include "common.h" 2 3 int main(int argc, char ** argv) { 4 printf("hello world (pid:…
wuzirui
  • 41
  • 3
4
votes
2 answers

Converting char* to int after using strdup()

Why after using strdup(value) (int)value returns you different output than before? How to get the same output? My short example went bad, please use the long one: Here the full code for tests: #include #include int main() { …
Rodion
  • 886
  • 10
  • 24
4
votes
1 answer

char* scope inside C++ containers

With the following: #include std::set global = std::set(); void x() { const char *c = "a"; const char *d = "b"; global.insert(c); global.insert(d); } int main() { x(); for (std::set
Matoe
  • 2,742
  • 6
  • 33
  • 52
3
votes
2 answers

Compiler flag to reveal functions like strdup

I've been given some starter code for a project I have to complete in a class I'm taking. The code compiles fine on the university computers however when I try to compile the code on my own computer I get errors due the function call strdup. From…
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
3
votes
7 answers

What's the difference between a string and a user entered string in C

I'm using with a smaller piece of code to test functionality for a larger (beginner) program, but I don't understand the difference between two strings. I found and used: #include #include int main() { char *string,…
Weaver
  • 145
  • 8
3
votes
4 answers

Cannot free memory after using strdup

gcc 4.5.1 c89 I am trying to free some memory. However, when I check with valgrind the memory hasn't been freed. I am wondering what I am doing wrong. I have the following structure: typedef struct tag_cand_results { char…
ant2009
  • 27,094
  • 154
  • 411
  • 609
3
votes
1 answer

memory leak with repeated strdup's

I am creating an array of filenames obtained from a linux_dirent structure (d). At each iteration of a loop a filename is obtained using d_entry = strdup(d->d_name); and a pointer to this is added to the array: srcList[aSz] = d_entry; As the array…
anita2R
  • 192
  • 1
  • 8
3
votes
2 answers

Duplicating strings passed to functions as parameters in C

I couldn't think of a proper title to my question so here it goes. I am trying to learn C and the following code is from the tutorial I am following. struct Person { char *name; int age; int height; int weight; }; struct Person…
MiJo
  • 569
  • 5
  • 19
3
votes
0 answers

Freeing memory allocated by strdup

How would I free the memory allocated by strdup? I've tried using free(linepos) at the end of this function, but that causes my program to fail. I don't have linepos outside of this function, so I'm unable to free it anywhere else. Thank you in…
Mickey
  • 117
  • 1
  • 10
2
votes
1 answer

How can I free all instances of dynamically allocated memory in this C code

I've written this C code. In the beginning, I used file handing to read a text file and insert every line as a string in a linked list. I need to free all cases of memory allocation in the program in a separate void function. How do I do that? I…
OmarAbzd
  • 19
  • 4
2
votes
3 answers

Vb.Net convert StrDup to C#.net

I have this line of code: strKey &= Strings.StrDup(intKeySize - intLength, chrKeyFill) What is the equivalent of this code in C#? I can't seem to find it.
ErocM
  • 4,505
  • 24
  • 94
  • 161
2
votes
3 answers

Is there a strdup equivalent in icu unicode?

The question is self-explanatory. I'm using the C API.
dan_waterworth
  • 6,261
  • 1
  • 30
  • 41
2
votes
2 answers

const char * to char * in my case

Although I know that converting const char * to char * is almost banned in C/C++ because of many problems, I am caught up in a situation where I think I have to convert const char * to char *. I get a string from a text file as string by using c_str…
wannaqc
  • 21
  • 3
1
2
3
8 9