strdup

C语言中的神兽strdup

混江龙づ霸主 提交于 2020-03-11 16:24:21
  C语言的确博大精深,在C语言的世界中遨游了那么多年,发现自己仍是菜鸟一枚,很多利器没有能够驾驭,今天介绍一个神兽,威力无比,但是却很少人能用得好。 函数原型: #include <string.h> char *strdup(const char *s); 函数介绍:   strdup()函数是c语言中常用的一种字符串拷贝库函数,一般和free()函数成对出现。 strdup()在内部调用了malloc()为变量分配内存,不需要使用返回的字符串时,需要用free()释放相应的内存空间,否则会造成内存泄漏。该函数的返回值是返回一个指针,指向为复制字符串分配的空间;如果分配空间失败,则返回NULL值。 函数实现: char * __strdup(const char *s) { size_t len = strlen(s) +1; void *new = malloc(len); if (new == NULL) return NULL; return (char *)memecpy(new,s,len); } 函数实战: #include <syslib.h> #include<string.h> int main(void) { char *src =”This is the strdup test”; char *dest; dest = strdup(s); printf(

Vb.Net convert StrDup to C#.net

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 10:56:06
问题 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. 回答1: strKey += new String(chrKeyFill, intKeySize - intLength); or strKey = strKey.PadRight(intKeySize, chrKeyFill) 回答2: strKey += strKey.PadRight(strKey.Length + (intKeySize - intLength), chrKeyFill) 回答3: Personally I think the String constructor approach is clumsy and less readable than the VB.Net Strings library. using Microsoft

strcpy, strdup, strcat, strncpy, strndup

本小妞迷上赌 提交于 2020-01-10 12:32:26
http://hi.baidu.com/liuhuman/item/c862c932b272d020b3c0c532 char* strcpy (char* dst, const char* src); //如果dst的长度 小于或者等于 strlen(src)时, src多余的字符串仍然被复制,将覆盖原先存储于数组后面的内存空间的值。 char* strdup(const char* src); //这个函数包含了malloc和strcpy, 不用担心在strcpy中dst的长度问题 char* strcat(char* dst, cosnt char* src); // 需要保证dst的大小足至少是strlen(dst) + strlen(src) + 1,否则数组溢出。 char* strncpy(char* dst, const char* src, size_t len); // 它总是正好向dst写入len个字符。 如果strlen(src) 小于 len , dst数组就用额外的'\0'填充到len个长度。 如果strlen(src) 大于或者等于 len, 那么只有len个字符被复制到dst中。注意: 它的结果将不会以'\0'结束 char* strndup(const char* src, size_t len); //复制len个字符串,它比strcpy好在:

C array of structure (exception thrown)

我只是一个虾纸丫 提交于 2020-01-05 05:39:05
问题 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 works perfectly, but my problem starts when i use it like this: void gen_Humans(Human array[MAX], int n){ //n == max; for (int i = 0; i<n; i++){ char *name = gen_name_function_used_before_WORKING(); array[i] = *createHuman(*name); } … } As I said, if I generate

strdup invalid read of size 4 when string literal is ending with newline \n

拜拜、爱过 提交于 2019-12-30 11:06:23
问题 I am getting an invalid read error when the src string ends with \n , the error disappear when i remove \n : #include <stdio.h> #include <stdlib.h> #include <string.h> int main (void) { char *txt = strdup ("this is a not socket terminated message\n"); printf ("%d: %s\n", strlen (txt), txt); free (txt); return 0; } valgrind output: ==18929== HEAP SUMMARY: ==18929== in use at exit: 0 bytes in 0 blocks ==18929== total heap usage: 2 allocs, 2 frees, 84 bytes allocated ==18929== ==18929== All heap

Typecasting string and strdup

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 12:01:20
问题 If an input const string is being modified in some way (which is resulting in C compiler warning), what is the best way to handle it - typecasting it to a new variable and then using it OR duplicating it and using it and then freeing it. Or is there any other way to handle this type of scenario. please suggest. Any help would be appreciated. //Typecasting const char * s1; char * s2 = (char *)s1; //Duplicate and free const char * s1; char * s2 = strdup( s1 ); free(s2) EDIT: It is a C compiler;

strdupa() in C - Dangers and Duplicates

随声附和 提交于 2019-12-23 22:07:45
问题 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 frees space. But at some places I read that the strdupa() function is dangerous. It would be helpful if someone could tell me why strdupa() is dangerous. Also, when I tried to run a program in my Open Suse 12.1 32 bit system, gcc , gave an error

转帖关于new/delete的运算符和malloc()/free()的标准库函数

随声附和 提交于 2019-12-23 09:48:08
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> new--------delete malloc--------free 问题: 我又一个对象类,里面有一个指针链表,动态分配空间,在析构的时候释放。开始对对象进行new操作,但是执行delete对象操作的时候出错,提示在析构的时候内存有问题。可是这时候成员一个比特的内存都没有分配啊。所以析构的时候应该什么内存操作都不执行。 更奇怪的是采用free()函数就可以释放这种对象,但是内存却不见减少,整个程序内存占用节节升高?这是为什么? 回复1: 你在析构函数当中没有正确的释放你申请的内存,比如,这个对象当中有一个指针,是采用动态申请内存的,在构造函数当中应该把它的值设置为NULL,然后在某个方法当中会申请内存,是采用new方法进行申请的,在析构函当中,应该先判断该指针是否为空,如果不为空,则使用delete释放内存,然后再把该指针设置为NULL。这样就可以了,如果你在外面是采用new申请这个对象,则在使用完成后使用delete释放就可以了。 回复2: 补充一点:new和malloc虽然都是申请内存,但申请的位置不同,new的内存从free store 分配,而malloc的内存从heap分配(详情请看ISO14882的内存管理部分), free store和heap很相似,都是动态内存,但是位置不同

strdup() causing memory leaks?

会有一股神秘感。 提交于 2019-12-13 20:29:36
问题 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 I've tried to research some fixes to no avail. My code is: const char * returnName(int age) { char string[30]; sprintf( string, "You are %d years old", age); return strdup(string); } Valgrind's output is: ==15414== LEAK SUMMARY: ==15414== definitely

strdup(): Confused about warnings ('implicit declaration', 'makes pointer…without a cast', memory leak)

Deadly 提交于 2019-12-12 14:23:40
问题 When I compile the short piece of code below (in which we define a string and then use strdup to make a copy), I get 3 warnings: 2 compiler warnings from GCC and 1 run-time warning/error from valgrind. I suspect the memory leak error (reported by valgrind) is also related to my use of strdup, which is why I'm including the relevant output below. What am I doing wrong? (I'm working my way through a C book and this is how strdup is used by the author.) The code: #include <stdio.h> #include