Strdup中的问题() [英] Problem in Strdup()

查看:50
本文介绍了Strdup中的问题()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在我的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)

我找不到问题。如果有的话知道解决方案,请告诉我

知道。

谢谢......

解决方案

> #include< stdio.h>

#include" string.h"


为什么" string.h"? < string.h中>足够。 int checking(char * a [20]);
void main()


make it int main(void){

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 bin3值为%s",bin3) ;

printf(\ n bin值为%s,bin [i]);

free(bin3);
strdup分配内存并返回一个指针

你刚刚释放了指针。
}
check(bin);
bin [0]和bin [1]中的内容是什么?

两个释放的指针。

通过释放的内存访问内存就像卖房子一样

后来试着留在那所房子里。

加一个回报0;这里。 } int /> int checking(char * a [20])
{/ / printf(" \ n valus is%s%s",a [0],a [1] );
返回0;
}
我的输出是

bin3值是你好

bin值是世界

价值是---------

我没有得到检查功能的最终价值......


但我在评论免费(bin3)方法时得到最终输出。

//免费(bin3)

我找不到问题。如果任何人都知道解决方案,PLZ让我知道。

谢谢......



strdup仅在unix中可用。所以不便携也。

问候,

manoj。


> strdup仅在unix中可用。也不是便携式的。


是的,它不是便携式的,但是如果

,则可以在其他系统上使用strdup实现提供它。例如,微软的编译器

支持它。当然,编写它是微不足道的,所以整个可移植性问题是没有问题的:


char * dupstr(const char * src)

{

char * p = malloc(strlen(src)+ 1);


if(p == NULL)

返回NULL;


strcpy(p,src);


返回p;

}


priya< pr ************* @ gmail.com>写道:

void main()


main()返回int: http://www.eskimo.com/~scs/C-faq/q11.12.html


首选


int main(无效)

char * var [20];
var [0] =" hello";
var [1] =" world" ;;


考虑


const char * var [] = {" hello"," world" (b =(i = 0; i< = 1; i ++)
{
bin3 = strdup(var [i]);


bin3指向一个新分配的字符串,其内容为var [i]。

bin [i] = bin3;


现在bin [i]指向*相同*新分配的字符串。

printf(\ n bin3值是%s, bin3);
printf(\ n bin值为%s,bin [i]);
free(bin3);


bin3和bin [i]现在指向已释放的内存...

}
check(bin);



.... check()尝试访问;这是一件坏事。一旦

指针传递给free(),它指向的内存关闭

限制。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


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...

解决方案

> #include <stdio.h>

#include "string.h"
why "string.h"? <string.h> is enough. int checking(char *a[20]);
void main()
make it int main(void) {

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); strdup allocate memory and returns a pointer
you just freed the pointer.
}
checking(bin); what will be in bin[0] and bin[1]?
two freed pointers.
accessing memory through a freed memory is like selling a house and
later try to stay in that house.
add a return 0; here. }
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.


> strdup is available only in unix.so not portable also.

True, it''s not portable, but strdup is available on other systems if
the implementation provides it. For example, Microsoft''s compiler
supports it. Of course, it''s trivial to write, so the whole portability
issue is moot:

char *dupstr(const char *src)
{
char *p = malloc (strlen(src) + 1);

if (p == NULL)
return NULL;

strcpy(p, src);

return p;
}


priya <pr*************@gmail.com> wrote:

void main()
main() returns int: http://www.eskimo.com/~scs/C-faq/q11.12.html

Prefer

int main( void )
char *var[20];
var[0]="hello";
var[1]="world";
Consider

const char *var[]={ "hello", "world" };
for( i=0;i<=1;i++)
{
bin3= strdup(var[i]);
bin3 points to a newly allocated string with the contents of var[i].
bin[i]=bin3;
Now bin[i] points to the *same* newly allocated string.
printf("\n the bin3 value is %s",bin3);
printf("\n the bin value is %s",bin[i]);
free(bin3);
bin3 and bin[i] now point to memory that has been freed...
}
checking(bin);



....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中的问题()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆