Realloc摧毁了吗? [英] Realloc destroys?

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

问题描述

我曾经认为我知道用C语言编程但是这个问题正在制作

我的事情。

我想做一些微不足道的事,我假设。

i有这个结构:


typedef struct {

int socket;

char ip [16];

}同行;


在main()i malloc中这样:

Peers = malloc(sizeof (同行));

然后,我的程序最终会想要保存更多的同行。所以

我说,为什么我不Realloc这个东西x2 x3 x4 x5等等......因为

需要。所以我创建了这个函数:


peers * ingresaripsock(peers * lista,int sock,char ip [16],int * tm)

{

int offset =(* tm)++;


if(offset> 0)lista = realloc(lista,offset * sizeof(peers)+1);


strcpy((lista + offset) - > ip,ip);

(lista + offset) - > socket = sock;


返回lista;

}


i打印结果:


void imprimirpeers(peers * lista,int tm){

int i = 0;


for(i = 0; i< ; tm; i ++){

printf(" Ip:%s - 袜子:%d \ n",(lista + i) - > ip,(lista + i)

- >套接字);

}

}

但是当我打印结果时,这一切都很奇怪数据。我相信

问题是在Realloc周围,因为我尝试了这段代码而不是

使用realloc我malloc''d从第一次开始,空间就像20

位置(在函数中注释了realloc部分)并且它工作了

就好了。但我想用我真正需要的东西,不想做malloc

(20000000)并使用几个字节...

请我真的需要一些帮助 -

I used to think that i knew to program in C but this problem is making
me thing otherwise.
i''m trying to do something trivial, i suppose.
i have this struct:

typedef struct{
int socket;
char ip[16];
}peers;

in a main() i malloc this way:
Peers=malloc(sizeof(peers));
Then, my program would eventually want to save more "peers" so
i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as
needed. So i created this function:

peers* ingresaripsock(peers* lista,int sock, char ip[16],int *tm)
{
int offset= (*tm)++;

if (offset>0) lista = realloc(lista,offset * sizeof(peers)+1);

strcpy((lista + offset)->ip, ip);
(lista + offset)-> socket=sock;

return lista;
}

i print the results with:

void imprimirpeers(peers* lista, int tm){
int i=0;

for(i=0;i<tm;i++){
printf("Ip: %s - Sock: %d\n",(lista + i) ->ip , (lista + i)
->socket);
}
}
but when i printf the Results, it''s all weird data. I believe the
problem is around the Realloc becuase i tried this code but instead of
using realloc i malloc''d from the first time with space for like 20
positions (commented the realloc part in the function) and it worked
just fine. But i want to use what i Really need, dont want to do malloc
( 20000000) and use a couple of bytes...
Please i really need some help-

推荐答案

如果***** @ gmail.com 写道:
if*****@gmail.com wrote:
if(offset> 0)lista = realloc(lista,offset * sizeof(peers)+1);
if (offset>0) lista = realloc(lista,offset * sizeof(peers)+1);




这是一个严重的错误。永远不要使用表格

ptr = realloc(ptr,size);

而是将realloc()的结果分配给临时的

如果realloc失败,至少可以给你做什么选项。



This is a severe mistake. Never use the form
ptr = realloc(ptr, size);
Instead have the results of the realloc() assigned to a temporary
variable to give you at least the option of what to do if realloc fails.


是的,写完帖后,我读了一些,我转到a

指向指针数据结构的指针。希望它的工作方式。

谢谢。

yes, after writing the post, i read some of that, i''m switchin to a
pointer to pointer data structure. hope it works that way.
Thanks.


文章< 11 ********** ************@o13g2000cwo.googlegroups .com> ;,
如果***** @ gmail.com 写道:
In article <11**********************@o13g2000cwo.googlegroups .com>,
if*****@gmail.com wrote:
我曾经认为我知道用C语言编程但这个问题正在制作
我的事情。
我我试图做一些微不足道的事。我有这个结构:

typedef struct {
int socket;
char ip [16];
}同行;

在main()i malloc中这样:
Peers = malloc(sizeof(peers));
然后,我的程序最终想要保存更多同行所以
我说,为什么我不Realloc这个东西x2 x3 x4 x5等等......如需要。所以我创建了这个函数:

peer * ingresaripsock(peers * lista,int sock,char ip [16],int * tm)
{
int offset =(* tm )++;

if(offset> 0)lista = realloc(lista,offset * sizeof(peers)+1);
I used to think that i knew to program in C but this problem is making
me thing otherwise.
i''m trying to do something trivial, i suppose.
i have this struct:

typedef struct{
int socket;
char ip[16];
}peers;

in a main() i malloc this way:
Peers=malloc(sizeof(peers));
Then, my program would eventually want to save more "peers" so
i said, why dont i Realloc this thing x2 x3 x4 x5 and so on.. as
needed. So i created this function:

peers* ingresaripsock(peers* lista,int sock, char ip[16],int *tm)
{
int offset= (*tm)++;

if (offset>0) lista = realloc(lista,offset * sizeof(peers)+1);




您计算需要多少字节看起来很有趣。



Your calculation how many bytes are needed looks interesting.


这篇关于Realloc摧毁了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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