我有这行代码:
strKey &= Strings.StrDup(intKeySize - intLength, chrKeyFill)
Run Code Online (Sandbox Code Playgroud)
C#中这段代码的等价物是什么?我似乎找不到它.
这是"C编程语言"一书中的程序.
有一个错误:'strdup'的冲突类型!当遇到函数'strdup'时.但是如果你将'strdup'改为其他名称,例如'strdu',错误就会消失.
我不知道为什么?顺便说一下,我使用code :: blocks作为我的IDE.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORD 100
struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
struct tnode *addtree(struct tnode *, char *);
struct tnode *talloc(void);
void treeprint(struct tnode *);
int getword(char *, int);
char *strdup(char *);
/* word frequency count */
int main()
{
struct tnode *root;
char word[MAXWORD];
root = NULL;
while (getword(word, MAXWORD) != EOF)
if (isalpha(word[0]))
root = addtree(root, word);
treeprint(root); …Run Code Online (Sandbox Code Playgroud) 我用C编写程序.我读到了strdup()函数.从我所知道的,该strdup()函数分配空间而strcpy()不是.但问题strdup()是它分配空间但不释放空间.strdupa()分配和释放空间.但在某些地方我读到这个strdupa()功能很危险.如果有人能告诉我为什么strdupa()危险会很有帮助.此外,当我试图在我的Open Suse 12.1 32位系统中运行程序时,gcc发出一个错误,告诉它strdupa()不存在.如果strdupa()是危险函数,有人可以告诉我strdupa()使用该函数时要使用的副本和标题.
当src字符串结束时\n,我收到无效的读取错误,当我删除时错误消失\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;
}
Run Code Online (Sandbox Code Playgroud)
valgrind输出:
==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 …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
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);
Run Code Online (Sandbox Code Playgroud)
我希望这输出"string1 \nstring2 \n",但str1和str2保持为null.我究竟做错了什么?
如果以某种方式修改输入const字符串(导致C编译器警告),处理它的最佳方法是什么 - 将其转换为新变量然后使用它或复制它并使用它然后释放它.或者有没有其他方法来处理这种情况.请建议.任何帮助,将不胜感激.
//铸字
const char * s1;
char * s2 = (char *)s1;
Run Code Online (Sandbox Code Playgroud)
//重复且免费
const char * s1;
char * s2 = strdup( s1 );
free(s2)
Run Code Online (Sandbox Code Playgroud)
编辑:这是一个C编译器; 不是C++.我不确定是否在类型转换中,s2将是字符串s1的新副本还是指向原始字符串s1?
谢谢你的回答.我还有一个疑问 -
const char * c1;
const char * c2 = c1;
Run Code Online (Sandbox Code Playgroud)
上述作业是否有效?
我想知道为什么将字符串转换为char*似乎使新的char*不等于它来自的字符串.
如果我有:
//raw versions of the string:
string s = "fun";
char* c = "fun";
char* s_convert = strdup(s.c_str()); //converting the string to char*
printf("(string) == 'fun' -> %d\n", (s == "fun"));
printf("(char*) == 'fun' -> %d\n", (c == "fun"));
printf("(char* convert) == 'fun' -> %d\n", (s_convert == "fun"));
printf("(string) == (char*) -> %d\n", (s == c)); //does new char* equal original string
Run Code Online (Sandbox Code Playgroud)
生产:
(string) == 'fun' -> 1 //true
(char*) == 'fun' -> 1 //true
(char* convert) == 'fun' …Run Code Online (Sandbox Code Playgroud) 当我运行 Valgrind 时出现以下错误,我尝试释放所有使用的函数,但仍然有相同的错误消息
==303912== HEAP SUMMARY:
==303912== in use at exit: 348 bytes in 2 blocks
==303912== total heap usage: 1,192 allocs, 1,190 frees, 153,918 bytes allocated
==303912==
==303912== 348 bytes in 2 blocks are still reachable in loss record 1 of 1
==303912== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==303912== by 0x490050E: strdup (strdup.c:42)
==303912== by 0x109B8E: main (minishell.c:178)
==303912==
==303912== LEAK SUMMARY:
==303912== definitely lost: 0 bytes in 0 blocks
==303912== indirectly lost: 0 bytes in 0 blocks
==303912== …Run Code Online (Sandbox Code Playgroud) 我使用a std::vector来存储一些字符串,稍后我尝试std::find它们但是通过strdup,如示例代码中所示,它不起作用,std::find返回last,这意味着它没有找到字符串,但我可以看到它是在那里,当我通过该std::vector::at功能访问它时,它正确显示.问题是什么?
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdint.h>
#include <string.h>
int main()
{
std::vector<char*> signal_list;
std::vector<char*>::iterator it;
char *temp;
char *temp2;
signal_list.push_back("DDF_LTEsyn__CALLER");
signal_list.push_back("DDF_LTEsyn__FFT_ctrl");
signal_list.push_back("DDF_LTEsyn__IFFT_ctrl");
signal_list.push_back("DDF_LTEsyn__ae_ctrl");
signal_list.push_back("DDF_LTEsyn__cwp_ctrl");
signal_list.push_back("DDF_LTEsyn__decision_ctrl");
signal_list.push_back("DDF_LTEsyn__ovelap_ctrl");
signal_list.push_back("DDF_LTEsyn__pilots_ctrl");
signal_list.push_back("DDF_LTEsyn__pre_ctrl");
signal_list.push_back("DDF_LTEsyn__rep_ctrl");
temp2 = strdup(signal_list.at(3));
printf("There is %s at position %d\n",temp2, 3);
it = find(signal_list.begin(), signal_list.end(), temp2);
printf("i found %s at position %d ",temp2, it - signal_list.begin());
}
Run Code Online (Sandbox Code Playgroud) 我必须创建一个非常便宜的算法(处理器和内存)来char从C中删除第一个字符串(char数组).
我目前正在使用:
char *newvalue = strdup(value+1);
free(value);
value = newvalue;
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一些较便宜的方法可以做到这一点.字符串value是动态分配的.
我实现了一个返回字符串的函数。它使用整数作为参数(age),并返回格式化的字符串。
一切运行良好,除了我有一些疯狂的内存泄漏的事实。我知道strdup()是造成这种情况的原因,但是我试图研究一些修复措施但无济于事。
我的代码是:
const char * returnName(int age) {
char string[30];
sprintf( string, "You are %d years old", age);
return strdup(string);
}
Run Code Online (Sandbox Code Playgroud)
Valgrind的输出是:
==15414== LEAK SUMMARY:
==15414== definitely lost: 6,192 bytes in 516 blocks
==15414== indirectly lost: 0 bytes in 0 blocks
==15414== possibly lost: 0 bytes in 0 blocks
==15414== still reachable: 0 bytes in 0 blocks
==15414== suppressed: 0 bytes in 0 blocks
Run Code Online (Sandbox Code Playgroud)
非常感谢您对解决此内存泄漏问题的任何帮助。
之间有什么区别
char *key_str="kiwi";
Run Code Online (Sandbox Code Playgroud)
和
char *key_str = strdup("kiwi");
Run Code Online (Sandbox Code Playgroud)
例如:
int strCmp(void *vp1, void *vp2) {
char * s1 = (char *) vp1;
char * s2 = (char *) vp2;
return strcmp(s1, s2);
}
Run Code Online (Sandbox Code Playgroud)
为什么这些*key_str在函数中使用时表现不同strCmp()?
源代码:https : //github.com/alexparkjw/typing/blob/master/pa2.c