Who modified my local variable?

Michael L Torrie torriem at chem.byu.edu
Mon Jun 12 10:17:09 MDT 2006


On Mon, 2006-06-12 at 23:41 +0800, Laurent R wrote:
> Hi, Michael:
>   I made a mistake on the example. It should be 'ucLen' instead of 'unLen'.
>   There is no specific reason to use the pointer of local variable 
> (stack-based) to call another function. I just do not want to allocate 
> memory and free it again.

Umm, there is no allocation and freeing of memory.  This is the stack
we're talking about here. Use the built-in scoping rules to your
benefit.  

There's no need to do all kinds of bizarre malloc and free (new and
delete) calls when normal C and C++ scoping rules would accomplish the
same things and be safer.  Unless copying is a huge concern, my rule of
thumb is to always use static allocations (local, on the stack of
course) whenever possible.  Additionally, passing by value will always
have fewer side effect.  Methinks Java's implicit pass by reference, and
apparent lack of statically allocated objects, has poisoned a lot of
programmers thinking.  And the best C tip I was ever given was the now-
standard alloca() function.  Anytime I think I might need malloc for
something short-lived (as in no more than the scope of the function), I
almost always find alloca to be more appropriate

In your case, passing ucLen by pointer requires the same amount of
memory and copying (4 extra bytes) than to pass by value.  

>   Multi-process means there are several processes running on the system. No 
> pre-empt process means other process can not pre-empt the process which 
> calls FunctionB.

Okay.

> 
>   I guess there is a mistake in the system. But I can't find any clue. There 
> are many libs for me to include and reference. I need some clue to find a 
> way to explain and investigate this problem.

gdb (and say, ddd) is your friend.

> 
> Br/Laurent
> 
> 
> 
> >From: Michael Torrie <torriem at chem.byu.edu>
> >Reply-To: Provo Linux Users Group Mailing List <plug at plug.org>
> >To: Provo Linux Users Group Mailing List <plug at plug.org>
> >Subject: Re: Who modified my local variable?
> >Date: Mon, 12 Jun 2006 06:42:55 -0600
> >
> >On Mon, 2006-06-12 at 15:58 +0800, Laurent R wrote:
> > > Hi,
> > >   I met a problem as show below:
> > >
> > > void FunctionA(unsigned char *pLen)
> > > {
> > >     *pLen = 16;
> > >     printf("T2 %d\r\n", *pLen);
> > >     return;
> > > }
> > >
> > > void FunctionB(void)
> > > {
> > >     unsigned char ucLen = 0;
> > >     printf("T1 %d\r\n", unLen);
> > >     FunctionA(&ucLen);
> > >     printf("T3 %d\r\n", unLen);
> > >     return;
> > > }
> > >
> > >   It is a multi-process project. One of the process will call FunctionB, 
> >but
> > > the result is not 0->16->16. It is 0->16->0. There are several other
> > > processes running in the backgroud. But there is no pre-empt process 
> >related
> > > to this function, or re-entry call of this function. I don't know who 
> >modify
> > > my local variable?
> > >   Who can help me to think out other reason to modify my local variable 
> >in
> > > this example?
> >
> >What does the debugger say?
> >
> >When you say there are multiple processes, does that mean that these
> >functions are called simultaneously by different processes?  I don't
> >know what you mean by "no pre-empt process."
> >
> >Step through your program with a debugger and watch the variable.  when
> >it changes examine the code.  My gut feeling is you have memory
> >corruption going on in another part of your program or these functions
> >that's clobbering this variable somehow.  Is there a reason you need to
> >pass a pointer to a local (stack-based) variable to another function?
> >
> >Trying to compile this fragment says that unLen is undeclared.  Did you
> >mean to say "ucLen?"  When I change it to ucLen, the fragment works just
> >as you would expect (0,16,16), so the problem is definitely in your
> >surrounding code.
> >
> > > Br/Laurent
> > >
> > > _________________________________________________________________
> > > Express yourself instantly with MSN Messenger! Download today it's FREE!
> > > http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> > >
> > >
> > > /*
> > > PLUG: http://plug.org, #utah on irc.freenode.net
> > > Unsubscribe: http://plug.org/mailman/options/plug
> > > Don't fear the penguin.
> > > */
> > >
> >
> >/*
> >PLUG: http://plug.org, #utah on irc.freenode.net
> >Unsubscribe: http://plug.org/mailman/options/plug
> >Don't fear the penguin.
> >*/
> 
> _________________________________________________________________
> Don't just search. Find. Check out the new MSN Search! 
> http://search.msn.com/
> 
> 
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
> 




More information about the PLUG mailing list