EEVblog® Electronics Community Forum

Electronics => Microcontrollers => Topic started by: IuriC on November 20, 2014, 04:39:31 pm

Title: Weird pointer issue
Post by: IuriC on November 20, 2014, 04:39:31 pm
Hi,

I'm having a little weird issue in my code running on a ARM Cortex M0 (LPC1114), probably is just a ridiculous mistake bypassing my view. I'm using LPCXpresso IDE, coding in C.

Let me break it down for you guys:

I have one typedef struct in my code that defines my circular buffer nodes. Exactly the same below.

Code: [Select]
typedef struct {
uint8_t addr;
uint8_t offset;
uint8_t rw;
uint8_t size;
uint8_t status;
uint8_t data[8];
} EEPROM_MemoryPageNode;

My read function receives one pointer of that structure above as a parameter to retrieve data. Function declaration below.

Code: [Select]
uint8_t _EEPROM_readBytes(uint8_t address, uint8_t size, EEPROM_MemoryPageNode* mem);
Inside the function I catch the memory address of my last node in my circular buffer.

Code: [Select]
...
mem = (EEPROM_MemoryPageNode)&_pageNodes[_stopNode];
...

Here comes the issue. I call the function as below:

Code: [Select]
...
EEPROM_MemoryPageNode* mem;
_EEPROM_readBytes(0x00, 0x01, mem );
...

It compiles ok, but for some reason the variable "mem" doesn't catch the address of my node! I did some debugging and could not fix it.

As I said earlier, probably a small detail escaping through my hands. Any help is welcome.

Thank you.
Title: Re: Weird pointer issue
Post by: DJohn on November 20, 2014, 04:55:12 pm
If you want it to change the mem variable in the caller, you need to pass in a pointer to that variable.  As it is, you're passing the value of mem, which is a pointer to EEPROM_MemoryPageNode.

You want
Code: [Select]
uint8_t _EEPROM_readBytes(..., EEPROM_MemoryPageNode** mem )
{
  ...
  *mem = whatever;
  ...
}

_EEPROM_readBytes( ..., &mem );
Title: Re: Weird pointer issue
Post by: IuriC on November 20, 2014, 05:41:11 pm
Didn't work. First time I got this kind of issue with pointers, may be some limitation on compiler?
Title: Re: Weird pointer issue
Post by: IuriC on November 20, 2014, 06:07:08 pm
Didn't work. First time I got this kind of issue with pointers, may be some limitation on compiler?

oh man... found it! I was forgetting to allocate memory! I can't believe it  :palm: :palm: :palm:

Thank you DJohn.