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.

Memory allocation problems in Zon2

I wanna allocate a piece of memory for a pointer in Zone2, but it failed. Actually to make things easier, I downloaded Example_281xCodeRunFromXintf from TI website and modified a little bit. But it seemed that it didn't work.

The following is a piece of code from my project.

.Cmd file:

   zone2data           : > ZONE2       PAGE = 1

.c file:

#pragma DATA_SECTION(Zone2test,"zone2data");
#pragma DATA_SECTION(pzone2,"zone2data");


#define max 32
float *pzone2;
int i=0;
float Zone2test[max];

main()

{

.......................

    pzone2=(float *)malloc(10*sizeof(float));
    for(i=0;i<max;i++)
        Zone2test[i]=101;

.........................

}

The problem is, from the watch window, i can see that,

The address of Zone2test is,0x00080040, which means this array has got memory from zone2. But for the pointer pzone2,I got the message, memory map prevented read of target memory at 0xFFFF9002@Data which means it hasn't successfully get memory from zone2.

It's really weird, why does the code and allocation succeed for arrays not for pointers?

Anything wrong with my program?

Really appreciate your help!

  • Haibin Wang said:

    #pragma DATA_SECTION(pzone2,"zone2data");


    #define max 32
    float *pzone2;

    Haibin,

    This will allocate space for the variable*pzone2.  That is the pointer itself  (32-bits and contains the address of what you are pointing to) in the data section called zone2data.

    Haibin Wang said:
        pzone2=(float *)malloc(10*sizeof(float));

    This initializes the pointer contents (in zone2data) to a an address where the array resides.  A malloc will pull from memory from the heap which is in the .esysmem compiler section.  So what the pointer points to - the array - will be in this memory section.

    Cheers

    Lori

     

     

     

     

     

  • Dear Lori,

    You are so great!

    In your reply, you said malloc. Do you mean far_malloc? In order to use Zone2 or 6, I tried far_malloc which allocates memory for section .esysmem. I did this before.

    But the thing is, section .esysmem can only correspond to one zone, let's say zone6?

       .esysmem            : > ZONE6       PAGE = 1

    Is it possible for me to map .esysmem to both zone2 and 6? Because I have too many data to deal with, only zone is not enough.[:)]

    Many thanks!

  • Haibin,

    I think the heap (like the stack) needs to be continuous memory.  The C2000 compiler guide may talk more about this.  Also for your reference, linker directives are in the C2000 assembler ref guide.

    Unless you need dynamic memory allocation (ie you don't know the size of arrays before compile time) I suggest avoiding malloc operations anyway.  You can just create the variable/array/structure/whatever and map it to the particular zone you want using the DATA_SECTION pragma.  

    -Lori