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

How to avoid including "-D_BSD_SOURCE" in compile statement every time the code is compiled?

To avoid a whole bunch of strdup errors, I have to always include -D_BSD_SOURCE in my compile statement. Is there a way I can somehow include this in my .c file and never include it in my compile statement again.
Brandon
  • 401
  • 5
  • 20
0
votes
2 answers

How to move memory that is already allocated to another struct

I have the following struct: typedef struct{ char* name; int score; }Student; typedef struct{ char* name; int score; }TeachingAssistant; I have already strdup'd a string into the name variable in the Student struct. I wanted to…
John
  • 105
  • 1
  • 8
0
votes
1 answer

strdup pointer version needs a temporary pointer

I'm implementing a strdup function as an exercise. char* strdupPtr(const char* str) { const size_t sz{strlen(str)+1}; char *save, *temp; save = temp = (char*)malloc(sz); while((*temp++ = *str++)); // compiler warning with only 1…
user3477273
0
votes
1 answer

C - segfault when trying to use strdup

I've used strdup() in the past in the same way that I am using it here. I am passing token2 into strdup which is of type char * with a valid pointer in it, yet when I try to run the line "name = strdup(token2);" my program segfaults and I am quite…
Tom Hickey
  • 1
  • 1
  • 2
0
votes
1 answer

Replace _strdup by _strdup

I got the following compiling error on VS2013: error C4996: '_strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details. So it asks me to replace "_strdup" by the same…
azerty
  • 698
  • 7
  • 28
0
votes
1 answer

Disappearing strings

This is my first post here so I thank everyone in advance for any and all help. I'm having an issue with disappearing information. The program is supposed to read in book titles and library ID numbers from a file, create a hash table of linked lists…
NHP
  • 3
  • 2
0
votes
3 answers

What is the less expensive way to remove a char from the start of a string in C?

I have to create a very inexpensive algorithm (processor and memory) to remove the first char from a string (char array) in C. I'm currently using: char *newvalue = strdup(value+1); free(value); value = newvalue; But I want to know if there is some…
h0m3
  • 104
  • 9
0
votes
1 answer

Returning a pointer through the function argument, but losing the data that was assigned to it. (In C)

I am new to this site, so I greatly apologize if I do anything wrong with this first post. The way I've written my code (including codes made for reusability), I must use char* arrays. I am converting the passed in char* into all lower-case…
user3040968
0
votes
3 answers

strdup and free usage

I'm trying this code: imei=Found_imei(pClient->GetBuffer()); printf("6. CODICE IMEI %s \n",imei); pClient->SetImei(imei); printf("6.1 CODICE IMEI %s \n",pClient->GetImei()); free(imei); <<<<<<<<<<<<<< printf("6.2 CODICE IMEI %s…
Ros
  • 37
  • 1
  • 6
0
votes
1 answer

crash with SIGSEGV in __GI___strdup()

I write code to read and open myapp configuration from xml file. Code attempt to parse the file for key elements and create them if they don't exist: static xmlDocPtr configsave_open( const char *config_filename ) { xmlDocPtr doc; xmlNodePtr…
user1840007
  • 615
  • 1
  • 10
  • 19
0
votes
4 answers

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) { …
ZPS
  • 1,566
  • 4
  • 16
  • 20
0
votes
3 answers

free() char with assigned value before call strdup()

I want to set mixer device from gtk_entry with this form "/dev/mixer:line" or "/dev/mixer:cd". User must to entry in this format mixer device settings: /dev/mixer:line or: /dev/mixer:cd For this I write code to setup mixer and have same dilemma…
user1840007
  • 615
  • 1
  • 10
  • 19
0
votes
3 answers

error: conflicting types for 'removeSpaces'

I want to write a program which will verify whether the string is palindrome or not. But there is an error when I try to pass strings[0] to removeSpaces function which will remove spaces. Why does 'comflicting types error' occurs? What is wrong? The…
yulian
  • 1,601
  • 3
  • 21
  • 49
0
votes
1 answer

Strdup() not working

Probably it is very basic and everyone will shout at me, but I've been trying to fix that for hours and can't take it anymore. I have this structure struct node { Key_Type element; tree_ptr left, right; }; And I am trying to put a word into element…
maxim fedotov
  • 87
  • 3
  • 6
0
votes
1 answer

'strdup' leak - unknown reason

Checking my code for leaks using Instruments, i'm getting one show up that I can't figure out the solution for. It's this: Malloc 48 bytes Responsible library - libsystem_c.dylib Responsible frame - strdup I've googled this and a few suggestions…
Andrew
  • 15,935
  • 28
  • 121
  • 203
1 2 3
8 9