我如何使用此代码破坏内存?
How am I corrupting memory with this code?
编辑:问题是我没有为“\0”字符分配内存。
基本上,我在尝试 运行 此代码时遇到了核心转储和损坏的最大尺寸错误,不用于 malloc 和动态内存。我的目标是连接两个字符串并使用分配的内存来执行此操作。第一个 'if' 将仅连接非 NULL 字符串,如果两者都是常规字符串,它将测量长度并为其分配内存。
代码编译通过,但执行时出现错误
int str_length(const char *str){
int i=0;
if(str == NULL) return (-1);
while( *(str+i) != '[=10=]') i++;
return i;
}
我用这个函数来测量数组的大小。我不能使用公共库
char* str_concatenate(const char *stra, const char *strb){
char *concatenate;
int i=0;
if(stra==NULL || strb == NULL){
if(stra==NULL && strb == NULL) return (concatenate = NULL);
if(strb==NULL){
concatenate = (char*) malloc( sizeof(char)* (str_length(stra) + 1) ); //se a segunda e NULL, copia a primeira
do{
concatenate[i]=stra[i];
i++;
}while(stra[i]!='[=11=]');
return concatenate;
}
else{
concatenate = (char*) malloc( sizeof(char)* (str_length(strb) + 1) ); //primeira NULL copia a segunda
do{
concatenate[i]=strb[i];
i++;
}while(strb[i]!='[=11=]');
return concatenate;
}
}
int size_a = str_length(stra);
int size_b = str_length(strb);
int total = size_a + size_b;
concatenate = (char*) malloc( sizeof(char)*(total + 1) );
while(strb[i]!='[=11=]'){
concatenate[size_b+i] = strb[size_b+i];
i++;
}
i=0;
while(stra[i]!='[=11=]'){
concatenate[i] = stra[i];
i++;
}
return concatenate;
}
int main(){
char stra[] "first string"
char strb[] "second string"
str_concatenate(stra, strb);
return 0;
}
请注意,您正在重置 i
,因此实际上您在第二个 while
中覆盖了 concatenate
,您应该尝试使用不同的变量来跟踪 [=] 的索引12=],例如:
//...
// size of char is always 1 byte, no need to include in malloc
concatenate = malloc(total + 1); // don't forget the null byte
i = 0;
while (strb[i] != '[=10=]')
{
concatenate[i] = strb[i];
i++;
}
int j = 0;
while (stra[j] != '[=10=]')
{
concatenate[i] = stra[j];
i++;
j++;
}
concatenate[i] = 0; // null terminate the string, the same goes for the others
//...
编辑:问题是我没有为“\0”字符分配内存。
基本上,我在尝试 运行 此代码时遇到了核心转储和损坏的最大尺寸错误,不用于 malloc 和动态内存。我的目标是连接两个字符串并使用分配的内存来执行此操作。第一个 'if' 将仅连接非 NULL 字符串,如果两者都是常规字符串,它将测量长度并为其分配内存。 代码编译通过,但执行时出现错误
int str_length(const char *str){
int i=0;
if(str == NULL) return (-1);
while( *(str+i) != '[=10=]') i++;
return i;
}
我用这个函数来测量数组的大小。我不能使用公共库
char* str_concatenate(const char *stra, const char *strb){
char *concatenate;
int i=0;
if(stra==NULL || strb == NULL){
if(stra==NULL && strb == NULL) return (concatenate = NULL);
if(strb==NULL){
concatenate = (char*) malloc( sizeof(char)* (str_length(stra) + 1) ); //se a segunda e NULL, copia a primeira
do{
concatenate[i]=stra[i];
i++;
}while(stra[i]!='[=11=]');
return concatenate;
}
else{
concatenate = (char*) malloc( sizeof(char)* (str_length(strb) + 1) ); //primeira NULL copia a segunda
do{
concatenate[i]=strb[i];
i++;
}while(strb[i]!='[=11=]');
return concatenate;
}
}
int size_a = str_length(stra);
int size_b = str_length(strb);
int total = size_a + size_b;
concatenate = (char*) malloc( sizeof(char)*(total + 1) );
while(strb[i]!='[=11=]'){
concatenate[size_b+i] = strb[size_b+i];
i++;
}
i=0;
while(stra[i]!='[=11=]'){
concatenate[i] = stra[i];
i++;
}
return concatenate;
}
int main(){
char stra[] "first string"
char strb[] "second string"
str_concatenate(stra, strb);
return 0;
}
请注意,您正在重置 i
,因此实际上您在第二个 while
中覆盖了 concatenate
,您应该尝试使用不同的变量来跟踪 [=] 的索引12=],例如:
//...
// size of char is always 1 byte, no need to include in malloc
concatenate = malloc(total + 1); // don't forget the null byte
i = 0;
while (strb[i] != '[=10=]')
{
concatenate[i] = strb[i];
i++;
}
int j = 0;
while (stra[j] != '[=10=]')
{
concatenate[i] = stra[j];
i++;
j++;
}
concatenate[i] = 0; // null terminate the string, the same goes for the others
//...