strdup

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

Hi, 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 advance. /* Code Snippet */ char *dir = NULL; char *bld = NULL; int chr; while ( ( chr ...

strdup() - what does it do in C?

What is the purpose of the strdup() function in C? ...

strdup() function

I recently became aware that the strdup() function I've enjoyed using so much on OS X is not part of ANSI C, but part of POSIX. I don't want to rewrite all my code, so I think I'm just going to write my own strdup() function. It's not that hard, really, it's just a malloc() and a strcpy(). Anyway, I have the function, but what am I doing...

Passing a structure by reference and manipulating it

typedef struct unit_class_struct { char *name; char *last_name; } person; int setName(person *array) { array[0].name = strdup("Bob"); array[1].name = strdup("Dick"); return 1; } int setLastName(person *array) { array->last_name = strdup("Sanchez"); array++; array->last_name = strdup("Clark"); re...

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 to the number of lines reserved with malloc(). I dont know if I have to do it like reser...

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 (vg_replace_malloc.c:195) ==4506== by 0x40782E3: g_malloc (in /lib/libglib-2.0.so.0.2200...

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, ptr2); I want this to output "string1\nstring2\n" however str1 and str2 remain null. What am ...

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 use free() Faster Any other differences? ...

Issue with threads: value stored in heap

Hello, 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 a new thread: void *status; pthread_t t; pthread_create(&t, NULL, executeTh...