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

using array of chars and strdup, getting segmentation fault

Suppose i write, char **p; p[0] = strdup("hello"); Strdup creates a duplicate string in heap with ending character '\0'; As p is pointer to pointer of char, p[0] = strdup("hello") seems perfectly fine for me. But why am i getting segmentation…
0
votes
1 answer

Valgrind gives error memory "still reachable"

I am having the following error when I run Valgrind, I tried to free all the used functions, but still have the same error message ==303912== HEAP SUMMARY: ==303912== in use at exit: 348 bytes in 2 blocks ==303912== total heap usage: 1,192…
BigHero101
  • 11
  • 1
  • 3
0
votes
1 answer

Segmention Fault on the while in reviews.csv

When i try run with the reviews.csv file the code gives segmention fault don't know why!! Can someone HELP me with that... In guião1v2.h only are the structs made for this. In the code i add some comments for being much easier understand what i'm…
0
votes
0 answers

Using strdup pointer as map key causes to a memory leak

We are having a 'Entry' class like below and std map with set of Entries. at the end we clean the map but valgrind shows possible leaks of the memory allocated via strdup function. class Entry { public: char* m_key; t m_data; …
ShaL
  • 141
  • 6
0
votes
1 answer

strdup for converting const char* to char*

I have designed for Huffman tree convert binary code with shorter bin code. In main if you call a Binary tree.init(q), then the tree would come out with key: frequency and value: bin code. The problem is converting const char* with char*. I've…
user12451939
0
votes
2 answers

What happends if you use strdup on an already allocated memory

I'm having some issue with my stack implementation, my push function manipulate the value i send into the function and changes it. I have tried diffrent ways of constructing this but they either don't work or give me corrupted output. The base idea…
forsb
  • 123
  • 9
0
votes
0 answers

create a fake strdup

In this case I'm trying to understand the difference between this wrong strdup and the good one. char *wrong_strdup(char *str) { char *dest; dest = str; return (dest); }
Myno
  • 158
  • 2
  • 13
0
votes
3 answers

Reading each line from a text file, word by word and converting to int (Infinite loop or crashing?)

I'm trying to read in this text file: 8 4 4 6 1 8 4 4 6 2 8 4 4 6 3 8 4 4 6 4 8 4 4 6 5 8 4 4 6 6 8 4 4 6 7 8 4 4 6 8 11 4 4 6 3 15 11 13 7 2 1 4 4 9 4 3 9 9 8 2 1 5 4 10 1 2 3 4 6 1 6 1 1 2 5 3 2 13 1 1 2 10 3 8 11 2 11 10 7 And printing it…
AlexDong11
  • 11
  • 1
0
votes
0 answers

How do I dynamically allocate memory to read a .txt file using the strdup function in C

I have a function to dynamically allocate and read a .txt file in C. However according to my assignment instructions the reading of the file should use strdup and not malloc to handle the dynamic memory allocation. I have tried tinkering with the…
DegaClaw
  • 37
  • 4
0
votes
0 answers

I am stuck with this code, why doesn't it work?

Why does this code not give me the right output? I am expecting the output to be decomptxt = aa". I typed in 02a (RLEText)" "Function 'Compressdata' doesn't return a value on all code paths. A null reference exception could occur at run…
0
votes
3 answers

free'ing results in crash

What is the difference between: Case1: char* strings[100]; strings[0]=malloc(100); char str[100]="AAA"; strings[0]=strdup(str); free(strings[0]); Case2: char* strings[100]; strings[0]=malloc(100); strings[0]="AAA"; …
anurag86
  • 1,635
  • 1
  • 16
  • 31
0
votes
2 answers

String Copy in C

I have a function which I pass a character pointer to, like: funtion_name (char* string){ ... } I want to copy the string to a temporary string variable, then copy that into a struct. This is what I have done thus far: char* namecpy =…
user10497670
0
votes
2 answers

Free for consistent dynamically allocated char pointers?

I have a struct containing a bunch of char pointers whose values are used throughout the program's lifetime. Most are overwritten every iteration. Should these values be freed at any point besides program exit? Should the value in the char pointer…
Kagemand Andersen
  • 1,470
  • 2
  • 16
  • 30
0
votes
1 answer

free strdup output in a loop

I have a loop which tries to read data from a qml list object, here is my loop char * argvarry[(gcps.size() * 5) + 8]; argvarry[0] = "-of"; argvarry[1] = "GTiff"; argvarry[2] = "-a_nodata"; argvarry[3] = "'0 0 0'"; argvarry[4] =…
Majid Hojati
  • 1,740
  • 4
  • 29
  • 61
0
votes
1 answer

Pointer is increment to NULL till end of string as below code but while if check its prove to be wrong why?

Hi pointer is increment to NULL to the end of string as bellow code but while if check its prove to be wrong why? #include #include #include int main() { char *p="Hello,"; // after comma NULL is implicit here …
1 2 3
8 9