Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Realloc destroys?

Reply
Thread Tools

Realloc destroys?

 
 
ifmusic@gmail.com
Guest
Posts: n/a
 
      10-10-2005
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-

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-10-2005
wrote:

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


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.
 
Reply With Quote
 
 
 
 
ifmusic@gmail.com
Guest
Posts: n/a
 
      10-10-2005
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.

 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      10-10-2005
In article < .com>,
wrote:

> 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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-11-2005
On 10 Oct 2005 14:06:47 -0700, wrote:

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


Is *tm a count of how may structures your currently have space for?

>{
>int offset= (*tm)++;


offset is the original value of *tm.

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


What did you think the +1 does. What you have done, if realloc
succeeds, is allocate space for the same number of structures plus one
more byte. You did not allocate space for an extra struct. Did you
really mean
... = realloc(lista, (offset+1) * sizeof(peers));

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


As it stands now, this invokes undefined behavior. lista points to an
allocated area capable of holding offset structures. The structures
are numbered 0, 1, ..., offset-1. You are now writing beyond the end
of your allocated area.

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



<<Remove the del for email>>
 
Reply With Quote
 
ifmusic@gmail.com
Guest
Posts: n/a
 
      10-11-2005
Thank you all for all the feedback,
now i have a pointer to pointer structure.

typedef struct{
int socket;
char id[16];
}reqs;

reqs** ingresaridsock(reqs** lista,int sock, char id[16],int *tm){
int i;
reqs* aux;
reqs **temp;
int offset= (*tm)++;
aux = malloc(sizeof(reqs));
if (offset > 0 ){
if ((temp = realloc(lista, (offset+1) * sizeof(*lista)))==NULL)
exit(1);
lista=temp;}
strcpy(aux->id, id);
aux->socket= sock;
lista[offset]= aux;
return lista;

Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth
item,
i get Segmentation Fault! Knowing that i'm not freeing memory as i
should
shouldn't it be possible to store more than 4 miserable items without
freeing?!.

thanks again.

Barry Schwarz ha escrito:

> On 10 Oct 2005 14:06:47 -0700, wrote:
>
> >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)

>
> Is *tm a count of how may structures your currently have space for?
>
> >{
> >int offset= (*tm)++;

>
> offset is the original value of *tm.
>
> >
> >if (offset>0) lista = realloc(lista,offset * sizeof(peers)+1);

>
> What did you think the +1 does. What you have done, if realloc
> succeeds, is allocate space for the same number of structures plus one
> more byte. You did not allocate space for an extra struct. Did you
> really mean
> ... = realloc(lista, (offset+1) * sizeof(peers));
>
> >
> >strcpy((lista + offset)->ip, ip);

>
> As it stands now, this invokes undefined behavior. lista points to an
> allocated area capable of holding offset structures. The structures
> are numbered 0, 1, ..., offset-1. You are now writing beyond the end
> of your allocated area.
>
> >(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-

>
>
> <<Remove the del for email>>


 
Reply With Quote
 
ifmusic@gmail.com
Guest
Posts: n/a
 
      10-11-2005
Here is something new,
if the first time i malloc(15*sizeof(*Reqs)) it works fine. So i
commented the Realloc part in the function i tried it. But Something i
did not expect ,happened, it didn't just stored 15 items but it seemed
to be "endless", i stored tons of items as if there was no need for a
Realloc. WHY?!.

ifmu...@gmail.com ha escrito:

> Thank you all for all the feedback,
> now i have a pointer to pointer structure.
>
> typedef struct{
> int socket;
> char id[16];
> }reqs;
>
> reqs** ingresaridsock(reqs** lista,int sock, char id[16],int *tm){
> int i;
> reqs* aux;
> reqs **temp;
> int offset= (*tm)++;
> aux = malloc(sizeof(reqs));
> if (offset > 0 ){
> if ((temp = realloc(lista, (offset+1) * sizeof(*lista)))==NULL)
> exit(1);
> lista=temp;}
> strcpy(aux->id, id);
> aux->socket= sock;
> lista[offset]= aux;
> return lista;
>
> Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth
> item,
> i get Segmentation Fault! Knowing that i'm not freeing memory as i
> should
> shouldn't it be possible to store more than 4 miserable items without
> freeing?!.
>
> thanks again.
>
> Barry Schwarz ha escrito:
>
> > On 10 Oct 2005 14:06:47 -0700, wrote:
> >
> > >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)

> >
> > Is *tm a count of how may structures your currently have space for?
> >
> > >{
> > >int offset= (*tm)++;

> >
> > offset is the original value of *tm.
> >
> > >
> > >if (offset>0) lista = realloc(lista,offset * sizeof(peers)+1);

> >
> > What did you think the +1 does. What you have done, if realloc
> > succeeds, is allocate space for the same number of structures plus one
> > more byte. You did not allocate space for an extra struct. Did you
> > really mean
> > ... = realloc(lista, (offset+1) * sizeof(peers));
> >
> > >
> > >strcpy((lista + offset)->ip, ip);

> >
> > As it stands now, this invokes undefined behavior. lista points to an
> > allocated area capable of holding offset structures. The structures
> > are numbered 0, 1, ..., offset-1. You are now writing beyond the end
> > of your allocated area.
> >
> > >(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-

> >
> >
> > <<Remove the del for email>>


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      10-11-2005
wrote:
>
> typedef struct{
> int socket;
> char id[16];
> }reqs;
>
> reqs** ingresaridsock(reqs** lista,int sock, char id[16],int *tm){
> int i;
> reqs* aux;
> reqs **temp;
> int offset= (*tm)++;
> aux = malloc(sizeof(reqs));
> if (offset > 0 ){
> if ((temp = realloc(lista, (offset+1) * sizeof(*lista)))==NULL)
> exit(1);
> lista=temp;}


You never allocate anything when *tm was 0.
If lista were NULL (or an array of size 0), then you would
cause undefined behaviour.

> strcpy(aux->id, id);
> aux->socket= sock;
> lista[offset]= aux;
> return lista;
>
> Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the
> fifth item, i get Segmentation Fault!


If the above was not the problem, then perhaps you have a buffer
overflow in the strcpy, or some other heap corruption. Can you
post a complete, compilable program that demonstrates the problem?

BTW, please don't top-post ( http://en.wikipedia.org/wiki/Top-posting )

 
Reply With Quote
 
ifmusic@gmail.com
Guest
Posts: n/a
 
      10-11-2005
There's no problem about the tm being 0.

the thing with posting the whole thing is that i don't know how you're
gonna test it. I'm testing it with 3 vmwares as clients and 1 vmware as
server which is storing the info.

Again, something that's still bugging me is the "endless" list i'm
getting...

if you want i can mail you the code.

Thanks for the help-

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-11-2005
On 10 Oct 2005 18:11:39 -0700, wrote:

>Thank you all for all the feedback,
>now i have a pointer to pointer structure.
>
>typedef struct{
>int socket;
>char id[16];
>}reqs;
>
>reqs** ingresaridsock(reqs** lista,int sock, char id[16],int *tm){
>int i;
>reqs* aux;
>reqs **temp;
>int offset= (*tm)++;
>aux = malloc(sizeof(reqs));
>if (offset > 0 ){
> if ((temp = realloc(lista, (offset+1) * sizeof(*lista)))==NULL)
>exit(1);
> lista=temp;}
>strcpy(aux->id, id);
>aux->socket= sock;
>lista[offset]= aux;
>return lista;
>
>Now, it seems to work just fine. BUT!, i add 1,2,3,4 and on the fifth
>item,
>i get Segmentation Fault! Knowing that i'm not freeing memory as i
>should
>shouldn't it be possible to store more than 4 miserable items without
>freeing?!.


On which statement does the seg fault occur?

How are you calling this function? Show us main.


<<Remove the del for email>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI:Solaris 9-Signal 11 in realloc() Mr T Java 0 11-19-2003 10:22 AM
Realloc a struct ** Henrik J C++ 1 11-10-2003 08:54 AM
Problem with malloc, realloc, _msize and memcpy Bren C++ 8 09-03-2003 11:01 PM
g++ 3.2.2 realloc bug? Jonathan.Bailleul C++ 3 08-08-2003 06:16 PM
Impossible Leak realloc Eitan Michaelson C++ 11 06-30-2003 10:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57