具有指针的结构 [英] Structure having pointers

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

问题描述




我有以下结构,


结构样本

{

int i;

char * p;

};


int main()

{

样本a;

ap = malloc(10);

样本b;

b = a;

}


现在我觉得浅色的副本已经完成,如果我只在对象上des

那就会有一个悬空的指针。

我如何克服这个问题,因为C结构不支持

函数?


谢谢你提前!!!

Hi,

I have the following Struct,

Struct Sample
{
int i;
char *p;
};

int main()
{
Sample a;
a.p = malloc(10);
Sample b;
b = a;
}

Now i think a shallow copy is done and if i destry only on the object
there would be a dangling pointer.
How do i overcome this problem as C structures don''t support
functions?

Thanks in advance!!!

推荐答案



当你写b = a你复制的内存b中的struct实例a。

这样就可以复制b中a的指针p。现在ap和bp指向

相同的内存。

要制作结构的深度副本,你必须编写一个函数:


struct Sample * SampleCopy(struct Sample * src)

{

struct Sample * b = NULL;

b =(struct Sample * )malloc(sizeof(struct Sample));

bi = src.i;

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

memcpy(bp,src.p);

返回b;

}


int main()

{

样本a;

ap =(char *)malloc(10);

样本* b;

b = SampleCopy(& a);


免费(ap);

free(a);

printf("%s",b-> p); //应该工作!!

}


我想通过这种方式你可以解决你的问题......如果我已经理解了。

可能我在代码中犯了一些错误..但我现在还没有

参考!


再见

Gio

On 29 Lug,12:20,sam _... @ yahoo.co.in写道:
Hi,
when you write b=a you copy the memory of the struct instance a in b.
In this way you copy the pointer p of a in b. Now a.p and b.p point to
the same memory.
To make a depth copy of the struct you have to write a function:

struct Sample* SampleCopy(struct Sample* src)
{
struct Sample* b=NULL;
b=(struct Sample*)malloc(sizeof(struct Sample));
b.i=src.i;
b.p=(char*)malloc(sizeof(char)*(strlen(src.p)+1));
memcpy(b.p,src.p);
return b;
}

int main()
{
Sample a;
a.p = (char*)malloc(10);
Sample *b;
b = SampleCopy(&a);

free(a.p);
free(a);
printf("%s",b->p); //should work!!
}

I think in this way you may solve your problem... if I''ve understood.
Probably I''ve committed some errors in the code.. but I''ve not a
reference now!

Bye
Gio
On 29 Lug, 12:20, sam_...@yahoo.co.in wrote:




我有以下结构,


结构样本

{

int i;

char * p;

};


int main()

{

样品a;

ap = malloc(10);

样品b;

b = a ;


}


现在我认为浅色副本已经完成,如果我仅在对象上进行destry

会有一个悬垂的指针。

我如何克服这个问题,因为C结构不支持

函数?


提前致谢!!!
Hi,

I have the following Struct,

Struct Sample
{
int i;
char *p;
};

int main()
{
Sample a;
a.p = malloc(10);
Sample b;
b = a;

}

Now i think a shallow copy is done and if i destry only on the object
there would be a dangling pointer.
How do i overcome this problem as C structures don''t support
functions?

Thanks in advance!!!






sa * ****@yahoo.co.in 写道:
sa*****@yahoo.co.in wrote:




我有以下结构,

结构样本
Hi,

I have the following Struct,

Struct Sample



关键字是​​struct,而不是Struct。 C区分大小写。

The keyword is struct, not Struct. C is case sensitive.


{

int i;

char * p;

};


int main()

{

样本a;
{
int i;
char *p;
};

int main()
{
Sample a;



声明应该是:


struct Sample a;

The declaration should be:

struct Sample a;


ap = malloc(10);

样本b;
a.p = malloc(10);
Sample b;



如上所述。

As above.


b = a;

}


现在我认为浅拷贝已完成
b = a;
}

Now i think a shallow copy is done



由于ai具有不确定的值,因此副本会调用未定义的行为。

Since a.i has an indeterminate value, the copy invokes undefined behaviour.


如果我只在对象上徘徊

会有一个悬空指针。

如何克服这个问题C结构不支持

功能?
and if i destry only on the object
there would be a dangling pointer.
How do i overcome this problem as C structures don''t support
functions?



通过在ap或bp上调用free释放内存,并将它们设置为

为NULL。

Deallocate the memory by calling free on either a.p or b.p, and set them
both to NULL.


AnticitizenOne写道:


[修正后的帖子]
AnticitizenOne wrote:

[top-post corrected]

On 29 Lug,12:20,sam _... @ yahoo.co.in写道:
On 29 Lug, 12:20, sam_...@yahoo.co.in wrote:

>

我有以下结构,

结构样本
{
int i;
char * p;
};

int main( )
{
样本a;
ap = malloc(10);
样本b;
b = a;

}

现在我认为浅色副本已经完成,如果我只在对象上进行描述,那么会有一个悬空指针。
我如何克服这个问题,因为C结构不会支持
功能?
>Hi,

I have the following Struct,

Struct Sample
{
int i;
char *p;
};

int main()
{
Sample a;
a.p = malloc(10);
Sample b;
b = a;

}

Now i think a shallow copy is done and if i destry only on the object
there would be a dangling pointer.
How do i overcome this problem as C structures don''t support
functions?





当你写b = a时你在b中复制结构实例a的内存。

这样就可以复制b中a的指针p。现在ap和bp指向

相同的内存。

要制作结构的深度副本,你必须编写一个函数:


struct Sample * SampleCopy(struct Sample * src)

{

struct Sample * b = NULL;

b =(struct Sample * )malloc(sizeof(struct Sample));


Hi,
when you write b=a you copy the memory of the struct instance a in b.
In this way you copy the pointer p of a in b. Now a.p and b.p point to
the same memory.
To make a depth copy of the struct you have to write a function:

struct Sample* SampleCopy(struct Sample* src)
{
struct Sample* b=NULL;
b=(struct Sample*)malloc(sizeof(struct Sample));



C中不推荐演员。

The cast isn''t recommended in C.


bi = src.i;

bp =(char *)malloc(sizeof(char)*(strlen(src.p)+1));
b.i=src.i;
b.p=(char*)malloc(sizeof(char)*(strlen(src.p)+1));



而且sizeof(char)在C中总是一个。

And sizeof(char) is always one in C.


memcpy(bp,src.p );

返回b;

}


int main()

{

样本a;
memcpy(b.p,src.p);
return b;
}

int main()
{
Sample a;



它应该是struct Sample a;

It should be struct Sample a;


ap =(char *)malloc(10);

样本* b;
a.p = (char*)malloc(10);
Sample *b;



C89中不允许使用混合代码和声明,但在
C99中允许它们。

Mixed code and declarations are not allowed in C89 though they''re allowed in
C99.


b = SampleCopy(& a);


免费(ap);

免费(a);

printf("%s",b-> p); //应该管用!!
b = SampleCopy(&a);

free(a.p);
free(a);
printf("%s",b->p); //should work!!



返回0;

return 0;


}
}


这篇关于具有指针的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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