324: does it sound strange?
Author Message
324: does it sound strange?

look at this code:

net=(struct s_net *) realloc(net, new_dimension  * sizeof(struct
s_net));
block=(struct s_block *) realloc(block, new_dimension * sizeof(struct
s_block));

s_net is defined as:
struct s_net {char *name; int num_pins; int *blocks; int *blk_pin; };

s_block is defined as:
struct s_block {char *name; enum e_block_types type; int *nets; int x;
int y; boolean combinatorial;};

after the realloc the element number 324 of the two arrays is lost.

can somebody explain what happens?

thanks

Fri, 04 Feb 2005 12:44:01 GMT
324: does it sound strange?
Submitted by "Giancarlo" to comp.lang.c:

Quote:
> look at this code:

> net=(struct s_net *) realloc(net, new_dimension  * sizeof(struct
> s_net));
> block=(struct s_block *) realloc(block, new_dimension * sizeof(struct
> s_block));

Proper call to realloc:

newptr = realloc(oldptr, newsize);
if (newptr != NULL)
oldptr = newptr;
else
/* handle alloc error */

No cast is neccesary.

Quote:

> s_net is defined as:
> struct s_net {char *name; int num_pins; int *blocks; int *blk_pin; };

> s_block is defined as:
> struct s_block {char *name; enum e_block_types type; int *nets; int x;
> int y; boolean combinatorial;};

> after the realloc the element number 324 of the two arrays is lost.

I don't see any evidence to support the theory that element 324
was ever allocated...

Quote:

> can somebody explain what happens?

> thanks

--
Andreas K?h?ri
--------------------------------------------------------------
Stable, secure, clean, free:  www.netbsd.org

Fri, 04 Feb 2005 13:13:50 GMT
324: does it sound strange?

Quote:

> Submitted by "Giancarlo" to comp.lang.c:
> > look at this code:

> > net=(struct s_net *) realloc(net, new_dimension  * sizeof(struct
> > s_net));
> > block=(struct s_block *) realloc(block, new_dimension * sizeof(struct
> > s_block));

> Proper call to realloc:

> newptr = realloc(oldptr, newsize);
> if (newptr != NULL)
>     oldptr = newptr;
> else
>     /* handle alloc error */

> No cast is neccesary.

Quite so. Also, we can simplify the sizeof call, thus turning:

net=(struct s_net *) realloc(net, new_dimension  * sizeof(struct
s_net));
block=(struct s_block *) realloc(block, new_dimension * sizeof(struct
s_block));

into:

newnet = realloc(net, new_dimension * sizeof *net);
if(newnet != NULL)
{
net = newnet;

Quote:
}

else
{
/* handle or report the failure */

Quote:
}

newblock = realloc(block, new_dimension * sizeof *block);
if(newblock != NULL)
{
block = newblock;
Quote:
}

else
{
/* handle or report the failure */

Quote:
}

Removing the casts and type names doesn't *just* remove a possible
current bug and a possible future maintenance problem. It *also* makes
the code look so much prettier. :-)

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Fri, 04 Feb 2005 19:26:36 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages