This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Pointers problem

Hello,

I run into a problem using pointers on an array. In my application I try to burn SPI FLASH with program I downloaded into array in SDRAM (this application is strictly for testing). This is a pseudo code of a program:

int FLASH_Buffer[40000];

int *ReadPtr, *WritePtr;

 WritePtr = FLASH_Buffer;

for loop {

  write to FLASH;

  read from FLASH;

  validate;

  WritePtr +=64;
  ReadPtr +=64;
}

The problem is that the pointers advanced in steps of 256 instead of 64, as defined in program. I could change the number from 64 to 16, but I need to understand why does this happens. Both buffer and ponter to budder are integers.

Thank You

 

  • Hello,

    you have pointers to int type which size is 4 bytes.

    consider this:

    WritePtr = FLASH_Buffer;

    WritePtr += 1; //this now points to next int

    (WritePtr == &(Flash_Buffer[1]))  which is next int  ->  4 bytes further in the memory

    If you have pointers don't use them as table indexes

    this is WRONG:

    something = FLASH_Buffer[ReadPtr]; this is WRONG

    You should use pointers like this:

    something = *ReadPtr;

    *WritePtr = something;

    or declare indexes to table

    int ReadIndex, WriteIndex; //NOT pointers and use them like this

    something = FLASH_Buffer[ReadIndex];

    FLASH_Buffer[WriteIndex] = something;

    ReadIndex and WriteIndex would be incremented as you've expected by one for each int.

    Pointers on the other side are addresses and each int takes 4bytes hence inrement by 4.

     

     

     

     

     

     

     

     

     

  • Hi,

    I am aware of those rules, that is actually what I was counting on.

    instead of using buffer with index e.g: buffer[index], i declare a pointer to buffer of same type e.g:

    int * pointer;

    int buffer[40000];

    pointer=buffer;

    and use pointer to perform read/write e.g. value = *pointer. That suppose to save execution time for CPU.

    The problem occurs when I declare pointer of type integer, that is 4 bytes for this particular format, and trying to advance it by 64 integer values i.e. pointer+=64.

    New pointer value suppose to point to the 64-th integer in array since, as You said, pointer is an integer, instead it it points to 256-th integer in array.

    I suspect that it has somthing to do with DATA ALIGNMENT or the way data is packed.

    I am using a big arrays - 40000 integers, maybe this causing a problem.

     

    Regards

    Arye lerner

     

  • Hi Arye,

    Arye Lerner said:

    The problem occurs when I declare pointer of type integer, that is 4 bytes for this particular format, and trying to advance it by 64 integer values i.e. pointer+=64.

    New pointervalue suppose to point to the 64-th integer in array since, as You said, pointer is an integer, instead it it points to 256-th integer in array.

    I suspect that it has somthing to do with DATA ALIGNMENT or the way data is packed.

    Whenever you increment a pointer type "int", it will increment 4 bytes in the memory -sizeof(int). It should not increment 32 bytes like you said - I never saw that happen. If your big array is local, make sure to move it to global and re-test it - you could have a stack problem, or even a broken pointer, etc...

     

  • Thank You, Mariana!

  • Arye Lerner said:
    That suppose to save execution time for CPU.

     

    If you use optimization option 3 you get the same assambly code exectly

    Mark