6 Replies - 4219 Views - Last Post: 06 January 2013 - 03:05 AM Rate Topic: -----

#1 lamle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-January 13

Pointer to a local variable

Posted 03 January 2013 - 04:30 AM

Hi all,
I have a coding guideline that I need to avoid using dynamic memory allocation. In stead of it, I do things like this:

typedef struct
{
u32 id;
....
} A;

void test()
{
A instance;
A* pinstance = &instance;
pinstance->id = 0xa000111;
(do everything with pinstance)
....
 -> pinstance->id has different value (0x111) even though it is not set any where else!
}


Can any one pls tell me:
1. If this is a good way to do.
2. What can be possible cause of the change of the value of pinstance->id. Can it be because of stack memory full? the thread is still running normally, only the local variable value is changed. It seems that the problem occured when optimization is on (-O3).
Thanks,Lam

This post has been edited by JackOfAllTrades: 03 January 2013 - 04:58 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to a local variable

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Pointer to a local variable

Posted 03 January 2013 - 05:00 AM

What happens is likely the result of something in here:

Quote

(do everything with pinstance)


You could be stomping your memory in there.

Why bother with a pointer at all?

A instance;
instance.id = 0xa000111;

Was This Post Helpful? 2
  • +
  • -

#3 lamle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-January 13

Re: Pointer to a local variable

Posted 03 January 2013 - 06:08 AM

Hi JackOfAllTrades,
Thank you for quick reply.

[code](do everything with pinstance)[code]

this part just basically sets value for members of struct A (excluding id), nothing more.

I used pointer to the local variable for convenience reason with other part of the code. Now it is like that and I am wondering what was the cause of the problem that the value of pinstance->id has changed.

Can it be that the address of instance is changed at some point and pinstance keeps pointing to the old address?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,236
  • Joined: 21-June 11

Re: Pointer to a local variable

Posted 03 January 2013 - 07:15 AM

No, the addresses of a function's local variables can't change during the run of that function. It is perfectly safe to use pointers to local variables as long as those pointers aren't returned from the function or otherwise still exist after the function returns.

Your problem must lie elsewhere. It would help if you could show us a short, compilable piece of code that we can run to reproduce the behavior you're seeing (not least because such a code sample would be guaranteed to actually contain the code that's causing the problem).
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Pointer to a local variable

Posted 03 January 2013 - 10:52 AM

I'd throw all the pointer play into a function. The would make is easier to make sure you aren't clobbering your local pointer. e.g.
void testRun(A *p) {
	p->id = 0xa000111;
	// ...
}

void test() {
	A instance;
	
	testRun(&instance);
}



If "do everything" is a lot of code, you should have it broken up into functions, anyway.
Was This Post Helpful? 1
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Pointer to a local variable

Posted 04 January 2013 - 12:00 PM

This is a good place to use a debugger. Watch the point pinstance and put a break point where you noticed the error.

Somethings to check: #1 Scope. Make sure that instance is still in scope everywhere you are using the object. Remember you CAN NOT do this:
A* myfunc() {
    A instance;
    A* pinstance = &instance;
    return pinstance; //error! memory pointed to will no longer be available!
}


#2 Make sure that pinstance still points to instance. Maybe somewhere you did pinstance = newValue instead of *pinstance = *newValue etc.

#3 Casts. Pointers are magical things that can be cast to any other type compatible or not. If you do any casting remember that structures in memory don't always look exactly like they do on paper. For example padding my be added in places, order might be re-arranged etc.


The code you gave us didn't really give us enough to spot an error.
Was This Post Helpful? 3
  • +
  • -

#7 lamle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-January 13

Re: Pointer to a local variable

Posted 06 January 2013 - 03:05 AM

Thank guys for helping.
I suspected the wrong cause of the problem, there was no problem doing the mentioned way except that it uses additional unnecessary memory (the pointer).
The problem was because of another thread writing to the same memory location and overriding its value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1