realloc() won't work 
Author Message
 realloc() won't work

Hi C gurus,

On my system realloc() always fails when the first argument is NULL.
Since I am desperate to get my program working (it calls realloc() a
lot), I added the following line to the header file stdlib.h:

        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

But this has the potential problem of evaluating X twice if X != NULL.
I can set up a static variable in stdlib.h, perhaps something like this:

        static void *ttttttttttttttttttttttttttttttttttttttttt;
        #define realloc(X,Y) \
                ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
                malloc(Y):realloc(t,Y))

But I don't quite like the idea of a possible variable name conflict.
Can someone suggest a better way?  Thanks.

Larry



Sat, 22 May 1999 03:00:00 GMT  
 realloc() won't work

Quote:

> Hi C gurus,

> On my system realloc() always fails when the first argument is NULL.
> Since I am desperate to get my program working (it calls realloc() a
> lot), I added the following line to the header file stdlib.h:

>         #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

> But this has the potential problem of evaluating X twice if X != NULL.
> I can set up a static variable in stdlib.h, perhaps something like this:

>         static void *ttttttttttttttttttttttttttttttttttttttttt;
>         #define realloc(X,Y) \
>                 ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
>                 malloc(Y):realloc(t,Y))

> But I don't quite like the idea of a possible variable name conflict.
> Can someone suggest a better way?  Thanks.

i always just define a local variable in the macro:

#define realloc(X,Y) \
{ void *l_X = (X); \
  (l_X == 0 ? malloc(Y) : realloc(l_X,(Y)); \

Quote:
}

.bri.

Quote:
> Larry

--
`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'

RS/6000 Division                           VNET:     BRIANH at AUSTIN
11400 Burnet Rd, 4449/045-2L007            internal:

Austin, TX  78758                   Phone: (512) 838-2958  t/l 678-2958
`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'


Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work

|On my system realloc() always fails when the first argument is NULL.
|Since I am desperate to get my program working (it calls realloc() a
|lot), I added the following line to the header file stdlib.h:
|
|        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))
|
|But this has the potential problem of evaluating X twice if X != NULL.
|I can set up a static variable in stdlib.h, perhaps something like this:
|
|        static void *ttttttttttttttttttttttttttttttttttttttttt;
|        #define realloc(X,Y) \
|                ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
|                malloc(Y):realloc(t,Y))
|
|But I don't quite like the idea of a possible variable name conflict.
|Can someone suggest a better way?  Thanks.

A pedantic answer would be: your realloc() is broken, start beating
your vendor on his head until he starts to like it and then stop.

A more practical answer would be: add a small stub like this and include
the last #define everywhere where needed:

#ifdef realloc
#undef realloc
#endif

void* my_realloc(void* x, size_t y) {

        return x?realloc(x, y):malloc(y);

Quote:
}

#define realloc(x, y) my_realloc(x, y)

I hope this helps you out,

kind regards,


--
Qgi nicws nt swaj ri rgw eufgr>



Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work


Quote:
>On my system realloc() always fails when the first argument is NULL.

Then, your implementation is broken (non-standard conforming).

Quote:
>Since I am desperate to get my program working (it calls realloc() a
>lot), I added the following line to the header file stdlib.h:

>        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

>But this has the potential problem of evaluating X twice if X != NULL.

If you never call realloc with X being an expression containing side
effects (it would be completely insane to do such a thing, anyway) this
is not a problem at all.

Quote:
>I can set up a static variable in stdlib.h, perhaps something like this:

>        static void *ttttttttttttttttttttttttttttttttttttttttt;
>        #define realloc(X,Y) \
>            ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
>            malloc(Y):realloc(t,Y))

>But I don't quite like the idea of a possible variable name conflict.
>Can someone suggest a better way?  Thanks.

Use a proper function instead of a macro:

    void *safe_realloc(void *ptr, size_t size)
    {
        if (ptr) return realloc(ptr, size);
        return malloc(size);
    }

    #ifdef BROKEN_realloc
    #define realloc safe_realloc
    #endif

malloc and realloc are likely to be cpu-intensive functions, so the
overhead of an additional function call is insignificant.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work

 >Hi C gurus,
 >
 >On my system realloc() always fails when the first argument is NULL.
 >Since I am desperate to get my program working (it calls realloc() a
 >lot), I added the following line to the header file stdlib.h:
 >
 >        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))
 >
 >But this has the potential problem of evaluating X twice if X != NULL.

Is that really a problem?  Do you ever use side-effects on the first
argument to realloc() in your program?  I don't remember ever having
done this myself.  On the other hand, if the first parameter is a
function call, this could be problem.  But usually realloc() is used
on a variable, not a function result.  This is a very program- and
style-dependent consideration.

 >I can set up a static variable in stdlib.h, perhaps something like this:
 >
 >        static void *ttttttttttttttttttttttttttttttttttttttttt;
 >        #define realloc(X,Y) \
 >           ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
 >           malloc(Y):realloc(t,Y))
 >
 >But I don't quite like the idea of a possible variable name conflict.

I think that's unlikely, especially considering your choice of
variable names.  Besides, you wrote the program, right?  And thus you
know that there isn't any other identifier by the name of
ttttttttttttttttttttttttttttttttttttttttt in the program, right?

 >Can someone suggest a better way?  Thanks.

In my first draft I was going to suggest a different sort of macro to
replace realloc(), then I realized that the size of the block being
realloc()'d can't be portably determined.

In GNU C, you can write this:

        #define realloc(x,y) ({ void *p = x; (p ? realloc(p,y) \
                                                : malloc(y); )}

'Course, that's not portable, so I wouldn't use it.
--



Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work

 >On my system realloc() always fails when the first argument is NULL.
 >Since I am desperate to get my program working (it calls realloc() a
 >lot), I added the following line to the header file stdlib.h:

 >        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

 >But this has the potential problem of evaluating X twice if X != NULL.
 >I can set up a static variable in stdlib.h, perhaps something like this:

 >        static void *ttttttttttttttttttttttttttttttttttttttttt;
 >        #define realloc(X,Y) \
 >           ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
 >           malloc(Y):realloc(t,Y))

 >But I don't quite like the idea of a possible variable name conflict.
 >Can someone suggest a better way?  Thanks.

 >Larry

My approach is to add the following lines, preferably to a local header
but possibly to stdlib.h:
        #define realloc(X,Y)    fix_realloc(X,Y)
        extern void *fix_realloc(void*, size_t);

and write fix_realloc in a separate file as something like:
        #undef  realloc
        void *fix_realloc(void *ptr, size_t size)
        {
                if (ptr == NULL)
                        return malloc(size)
                else
                        return realloc(ptr, size);
        }

#defining realloc is not standard-conforming, but then if it works, it is
fixing a system that is not standard-conforming anyway.

--
Roger Wells (speaking only for myself)



Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work


 > On my system realloc() always fails when the first argument is NULL.
 > Since I am desperate to get my program working (it calls realloc() a
 > lot), I added the following line to the header file stdlib.h:
 >
 >         #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))
 >
 > But this has the potential problem of evaluating X twice if X != NULL.
 > I can set up a static variable in stdlib.h, perhaps something like this:
 >
 >         static void *ttttttttttttttttttttttttttttttttttttttttt;
 >         #define realloc(X,Y) \
 >           ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
 >           malloc(Y):realloc(t,Y))
 >
 > But I don't quite like the idea of a possible variable name conflict.
 > Can someone suggest a better way?  Thanks.

Well, you could always make a different subroutine, and then define
realloc to call the other subroutine.  The change-only-a-header way:

myheader.h:
---------------
static void *Xrealloc(char *p, size_t n)
{
    return (p == NULL) ? malloc(n) : realloc(p, n);

Quote:
}

#define realloc Xrealloc
----------------

Or of course you can make one external Xrealloc if you wanted an extra
module, and then only define realloc to Xrealloc in the header file.

--
==========================================================================

| Air-Shields Information Systems  | http://www.cerfnet.com/~timb (New!) |
==========================================================================



Sun, 23 May 1999 03:00:00 GMT  
 realloc() won't work

On 4 Dec 1996 16:16:51 -0600, Brian Horton wrote in
comp.lang.c.moderated:

Quote:
>i always just define a local variable in the macro:
>#define realloc(X,Y) \
>{ void *l_X = (X); \
>  (l_X == 0 ? malloc(Y) : realloc(l_X,(Y)); \
>}

You'll have a hard time getting this to work when the macro in
question needs to return a value (as this one does), i.e.,

   ptr = realloc(ptr, size);

will fail given your definition above.

Regards,

                                        -=Dave

I can barely speak for myself, so I certainly can't speak for B-Tree



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work


Quote:

> Hi C gurus,

> On my system realloc() always fails when the first argument is NULL.
> Since I am desperate to get my program working (it calls realloc() a
> lot), I added the following line to the header file stdlib.h:

>         #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

> But this has the potential problem of evaluating X twice if X != NULL.
> I can set up a static variable in stdlib.h, perhaps something like this:

>         static void *ttttttttttttttttttttttttttttttttttttttttt;
>         #define realloc(X,Y) \
>            ((ttttttttttttttttttttttttttttttttttttttttt=X)==0? \
>            malloc(Y):realloc(t,Y))

> But I don't quite like the idea of a possible variable name conflict.
> Can someone suggest a better way?  Thanks.

Three options:

1.  Find an ANSI compliant C compiler (some in this group would argue
that is the only solution acceptable to this group).

2.  Proceed as follows:
#define realloc myrealloc

and elsewhere have
void *myrealloc(void *X, size_t Y)
{
#undef realloc
     return X==0 ?  malloc(Y) : realloc(X,Y);

Quote:
}

3.  I believe Gnu C has an option to warn about using macros with
actual arguments that have side effects and are evaluated more than
once.  You could continue with your initial approach, and then use Gnu
C occasionally as a sort of Lint.  (Of course, if you can actually use
Gnu C as the compiler for your project, you are back to solution 1).

Be warned that your static variable doesn't work as well as you might
like.  If you do something like:
   f( realloc(p1,100), realloc(p2,200) );
You may not get what you expect (p1 AND p2 are both assigned to ttt
without an intervening sequence point).  This is a real issue, I once
tried a similar approach for the "abs" macro, however
    d = abs(x) + abs(y);
didn't work as we had hoped with the Vax compiler.  At the time we
complained this was a bug in the compiler, but the standard has now
made it clear the bug was in our code.
--
Martin Bonner
Pi Technology, Milton Hall, Church Lane, Milton, Cambridge, ENGLAND
+44 1223 203894



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work

On 4 Dec 1996 16:16:51 -0600, Brian Horton wrote in
comp.lang.c.moderated:

Quote:
>i always just define a local variable in the macro:
>#define realloc(X,Y) \
>{ void *l_X = (X); \
>  (l_X == 0 ? malloc(Y) : realloc(l_X,(Y)); \
>}

You'll have a hard time getting this to work when the macro in
question needs to return a value (as this one does), i.e.,

   ptr = realloc(ptr, size);

will fail given your definition above.

Regards,

                                        -=Dave

I can barely speak for myself, so I certainly can't speak for B-Tree



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work

Quote:

> On my system realloc() always fails when the first argument is NULL.
> Since I am desperate to get my program working (it calls realloc() a
> lot), I added the following line to the header file stdlib.h:

>         #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

> But this has the potential problem of evaluating X twice if X != NULL.
> I can set up a static variable in stdlib.h, perhaps something like this:

 I presume you just can't upgrade the version of malloc/free/realloc
etc.. ? apart from that the below should work.

#define realloc(x, y) (({ void *new_X = (X); (new_X) ? \
                         realloc(new_X, Y) : malloc(Y) }))

 but writting it as a function is prob. better is it really too hard
to do a search and replace ?

 ~J

--
/* James Antill **WWW**  http://users.essex.ac.uk/users/loser/ */
#include <stdio.h>
main(int c, char **v){char *a="";if(c==2)while((c=getchar())!=EOF){if(!
*a)a=v[1];c^=*a++;putchar(c);}else printf(" %s <key>\n",v[0]);exit(0);}



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work


Quote:


> >Hi C gurus,

> >On my system realloc() always fails when the first argument is NULL.
> >Since I am desperate to get my program working (it calls realloc() a
> >lot), I added the following line to the header file stdlib.h:

> >        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

> >But this has the potential problem of evaluating X twice if X != NULL.

You could of course always wonder why you are trying to realloc a null pointer
in the first place!

This rather suggests that the malloc that created X in the first place must
have failed. You should really check this much earlier, such as just after the
malloc. If you couldn't create the original pointer then the realloc is
more-or-less doomed.

However, as others have said the sad answer if you don't want to check the
above, is to implement your own version of realloc() as a function which
checks for you.



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work

Quote:

> i always just define a local variable in the macro:

> #define realloc(X,Y) \
> { void *l_X = (X); \
>   (l_X == 0 ? malloc(Y) : realloc(l_X,(Y)); \
> }

You do, do you?  How extensively did you test this snippet? :-)
Your macro leaks memory nicely, but isn't very useful for allocating
reachable memory ...

---------------------------------------------------------------------------
Tim Hollebeek         | Disclaimer :=> Everything above is a true statement,
Electron Psychologist |                for sufficiently false values of true.

----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work


 > >

 > > >Hi C gurus,
 > > >
 > > >On my system realloc() always fails when the first argument is NULL.
 > > >Since I am desperate to get my program working (it calls realloc() a
 > > >lot), I added the following line to the header file stdlib.h:
 > > >
 > > >        #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))
 > > >
 > > >But this has the potential problem of evaluating X twice if X != NULL.
 > >
 >
 > You could of course always wonder why you are trying to realloc a null pointer
 > in the first place!
 >
 > This rather suggests that the malloc that created X in the first place must
 > have failed. You should really check this much earlier, such as just after the
 > malloc. If you couldn't create the original pointer then the realloc is
 > more-or-less doomed.
 >
 > However, as others have said the sad answer if you don't want to check the
 > above, is to implement your own version of realloc() as a function which
 > checks for you.

And then again, you might not wonder.  There's often no reason to do a
malloc() before realloc().  Consider code like:

        static char* buf = NULL;
        int lenbuf = 0;

        if (lenbuf < n)
        {
          char* p = realloc(buf, n);
          if (p == NULL)
            { /* out of memory */ }
          buf = p;
          lenbuf = n;
        }

        /* ... */

Michael M Rubenstein



Tue, 25 May 1999 03:00:00 GMT  
 realloc() won't work

Quote:


>> On my system realloc() always fails when the first argument is NULL.
>> Since I am desperate to get my program working (it calls realloc() a
>> lot), I added the following line to the header file stdlib.h:

>>         #define realloc(X,Y) (X==0?malloc(Y):realloc(X,Y))

>> But this has the potential problem of evaluating X twice if X != NULL.
>> I can set up a static variable in stdlib.h, perhaps something like this:

> I presume you just can't upgrade the version of malloc/free/realloc
>etc.. ? apart from that the below should work.

>#define realloc(x, y) (({ void *new_X = (X); (new_X) ? \
>                         realloc(new_X, Y) : malloc(Y) }))

Just exactly how is the value of new_X supposed to get outside of the {}'s?

[oops.  I forgot to bounce the above as a gcc-ism. -mod]

The macros posted are statements, not expressions.  They will not evaluate
to the new pointer as realloc() must.


Software Development Engineer     Check Solutions

#include <std.disclaimer>



Sat, 29 May 1999 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. TERMWAIT Win 3.1 sample doesn't work under '95

2. Win 2000 built OCX won't load on a WIn NT or 98 system

3. ActiveX built in VC7 won't work!

4. Web service calling unmanaged DLL won't work

5. Debugger won't work

6. code won't work

7. FileAttributes won't work!

8. fopen won't work running under NT

9. why won't it work?

10. why won't this work?

11. HELP! Multidimensional array won't work

12. PLEASE HELP - program won't work - Grades.c.txt [01/01]

 

 
Powered by phpBB® Forum Software