strdup

A templated 'strdup()'?

可紊 提交于 2019-12-11 07:31:07
问题 template<typename T> static T *anydup(const T *src, size_t len) { T *ptr = malloc(len * sizeof(T)); memcpy(ptr, src, (len * sizeof(T))); return ptr; } Is this proper? Can I expect any errors from this when using an int, long, etc.? I'm very new to generic programming and am trying to learn more. 回答1: No this is not proper ! When you have a malloc() in C++ code, you should become very suspicious: malloc() allocates memory, but doesn't properly create objects. The only way to work with such

Where to free memory in Bison/Flex?

跟風遠走 提交于 2019-12-06 04:47:17
问题 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= strdup(yytext); pair<string,string> newpair = make_pair("DATE",yytext); myvector.push_back(newpair); return TOKEN_DATE ;} This is one of the example of my .l file. I copy the value of yytext into yylval.str. Then I create a new pair with that content (key

strdup and memory leaking

自作多情 提交于 2019-12-06 01:41:42
问题 Does strdup allocate another memory zone and create another pointer every time? For example: does the following code result in a memory leak? void x(char** d, char* s){ *d = strdup(s); } int main(){ char* test = NULL; x(&test, "abcd"); x(&test, "etc"); return 0; } 回答1: Yes, the program leaks memory because it allocates objects and then loses references to them. The first time this happens is in the line: x(&test, "etc"); The variable test holds the one and only copy of a pointer that was

Where to free memory in Bison/Flex?

时光怂恿深爱的人放手 提交于 2019-12-04 09:54:52
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= strdup(yytext); pair<string,string> newpair = make_pair("DATE",yytext); myvector.push_back(newpair); return TOKEN_DATE ;} This is one of the example of my .l file. I copy the value of yytext into yylval.str. Then I create a new pair with that content (key/value, actually), then I return the token date for bison. My parser .y is not more than yyparse; and

strdup and memory leaking

大城市里の小女人 提交于 2019-12-04 05:30:52
Does strdup allocate another memory zone and create another pointer every time? For example: does the following code result in a memory leak? void x(char** d, char* s){ *d = strdup(s); } int main(){ char* test = NULL; x(&test, "abcd"); x(&test, "etc"); return 0; } Yes, the program leaks memory because it allocates objects and then loses references to them. The first time this happens is in the line: x(&test, "etc"); The variable test holds the one and only copy of a pointer that was allocated in a previous call to x . The new call to x overwrites that pointer. At that point, the pointer leaks.

pwd函数实现

穿精又带淫゛_ 提交于 2019-12-03 14:41:25
/* * 文件名:mypwd.c * 描述: 实现简单的pwd命令 */ #include<stdio.h> #include<stdlib.h> #include<dirent.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<unistd.h> #define MAX_DIR_DEPTH (256) //根据文件名获取文件inode-number ino_t get_ino_byname(char *filename) { struct stat file_stat; if (0 != stat(filename, &file_stat)) { perror("stat"); exit(-1); } return file_stat.st_ino; } //根据inode-number,在当前目录下查找对应的文件名 char *find_name_byino(ino_t ino) { DIR *dp = NULL; struct dirent *dptr = NULL; char *filename = NULL; if (NULL == (dp = opendir("."))) { fprintf(stderr,"Can not open Current Directiory\n

strdup or _strdup?

匿名 (未验证) 提交于 2019-12-03 01:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When I use strdup in Microsoft Visual C++, it warns me: warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details. Thus it seems _strdup is correct. But when I use _strdup in GCC (Fedora Linux OS), the compiler shows an error: error: ‘_strdup’ was not declared in this scope With GCC and Linux, compiler does not show any error for strdup . Which is correct - strdup or _strdup ? Note: I include in my code. 回答1: strdup is not a standard C++ function. but

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

夙愿已清 提交于 2019-12-01 10:54:10
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 blocks were freed -- no leaks are possible ==18929== ==18929== ERROR SUMMARY: 1 errors from 1 contexts

freeing the string allocated in strdup() from flex/bison

℡╲_俬逩灬. 提交于 2019-11-29 07:29:21
I have flex code that copies a string lexeme using strdup() . %{ #include "json.tab.h" #define YY_DECL extern "C" int yylex() %} %option noyywrap %% [ \t\n]+ ; \"[a-zA-Z]+\" {yylval.sval = strdup(yytext); return STRING; } [0-9]+ {yylval.ival = atoi(yytext); return NUMBER; } . {return yytext[0];} ; %% strdup() allocates memory and copies the input string into it and return ( strdup() - what does it do in C? ), so I guess I need to free it up when I don't need it anymore. From this post: When is %destructor invoked in BISON? , I added %destructor { free($$); printf("free");} STRING in the yacc

Obj-C: Memory Leak of Malloc 48 bytes in strdup frame

孤者浪人 提交于 2019-11-28 06:25:00
In my app, I have am receiving multiple memory leaks. The object is Malloc 48 bytes, and it always originates from the responsible caller strdup. The history of the object only shows it being Malloced, and no other retains or releases. The stacktrace doesn't show any of my code. The only bits of relevance that I can pick out are: 10 UIKit _UIGestureRecognizerSendActions 9 UIKit -[UIScrollView handlePan:] 8 UIKit -[UIScrollView _endPanWithEvent:] 7 UIKit -[UIScrollView(Static) _startTimer:] 6 CoreFoundation CFNotificationCenterAddObserver 5 CoreFoundation _CFXNotificationRegisterObserver 4