问题描述
大家好,
我在我的c程序中使用strdup()..但是我在使用free()时有一些
pr0blem在我的c代码中。我粘贴了我的
代码。
#include< stdio.h>
#include" string.h"
int checking(char * a [20]);
void main()
{
int i;
char * bin3;
char * bin [20];
char * var [20];
var [0] =" hello";
var [1] =" world";
for(i = 0; i< = 1; i ++)
{
bin3 = strdup(var [i]);
bin [i] = bin3;
printf(" \\\
bin3值是%s",bin3);
printf(\ n bin值为%s,bin [i]);
免费(bin3);
}
check(bin);
}
int checking(char * a [20])
{
printf(" \ n valus是%s%s& ,a [0],a [1]);
返回0;
}
我的输出是
bin3值是你好
bin值是世界
价值是 - -------
我没有得到检查功能的最终价值...
但是我我在评论免费(bin3)方法时得到了最终输出。
//免费(bin3)
我找不到问题。如果有的话知道解决方案,请告诉我
知道。
谢谢......
strdup仅在unix中可用。所以不便携也。
问候,
manoj。
Hi all,
I am using strdup() in my c program..But I am having some
pr0blem while using the free() in my c code.here I am pasting the my
code.
#include <stdio.h>
#include "string.h"
int checking(char *a[20]);
void main()
{
int i;
char *bin3;
char *bin[20];
char *var[20];
var[0]="hello";
var[1]="world";
for( i=0;i<=1;i++)
{
bin3= strdup(var[i]);
bin[i]=bin3;
printf("\n the bin3 value is %s",bin3);
printf("\n the bin value is %s",bin[i]);
free(bin3);
}
checking(bin);
}
int checking (char *a[20])
{
printf("\n the valus is %s %s ",a[0],a[1]);
return 0;
}
My output is
the bin3 value is hello
the bin value is world
the valus is ---------
I am not getting the final value inside checking function...
But i am getting the final output when comment the free(bin3) method.
//free(bin3)
I could not find the problem.if any one know the solution,plz let me
know.
Thanks...
strdup is available only in unix.so not portable also.
regards,
manoj.
....which checking() attempts to access; this is a Bad Thing. Once a
pointer has been passed to free(), the memory it pointed to is off
limits.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
这篇关于Strdup中的问题()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!