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.

Heap align setting using new/delete keyword in c++

Other Parts Discussed in Thread: SYSBIOS

Hi, I'd like to know what is the align setting for heap allocation when using new/delete keyword in C++? I assume it can be controled via the setting of sytem heap instance from which it is allocated. But it seems the pointer returned from 'new' does not agree with the preset 'minBlockAlign' value. Why is that??? Anyone have any tips for that issue? I've run through all the available documents, but so far it still remains to be open question. Regards z. f. yang

  • Zhongfan,

    Can you specify what SYSIOS and XDCTOOLS version you are using?

    Also, do you know which heap module you are using?  Have you change the default heap instance?

    Judah

  • Hi, Judah: Thanks for your fast reponse. The version of SYSBIOS is bios_6_34_02_18, and XDCTOOLS is xdctools_3_24_05_48. I'v change the default heap instance under BIOS memory management\ memory-module setting menu, the default heap instance now points to my specially designated heap which by the way is defined in HeapMem module and the its minimum block alignment has been explicitly specified. Hope that'll help make clear our discussion. Regards Z. F. Yang
  • ZF Yang,

    Okay, I figured out what's going on.  The issue is that malloc requires a header at the front of the buffer and so the buffer we return to you will always be the "alignment you specify + header".

    Note sure what device you are on, but on like the C6000 devices, the header is 8 bytes.  So if the buffer requires a particular alignment you could pad your structure at the beginning to be "alignment specify - 8".

    Judah

  • Thanks for your input, Judah. I can verify your claim under most circumstances. Because curiously, I discover something contradictory to your remark. Here is the test code. I run it over C6748 platform, but prinft output seems not add up no matter which way you look at it. For this test case, you can try align at 256 byte.

     

     


    #include <vector>
    #include <complex>

    using namespace std; /* Because of the library: "complex" */

    typedef float     _REAL;
    typedef complex<_REAL>  _COMPLEX;

    int main()
    {
     int i;

     _COMPLEX* ppComplex[8];


     System_printf("\r\n sizeof  _COMPLEX = %d;\n", sizeof(_COMPLEX));
     
     for(i = 0; i < 8; i++)
     {

     
      ppComplex[i] = new _COMPLEX[i + 3];


      System_printf("\r\n heap Pointer[%d] = 0x%0x, 0x%x;\n",i, ppComplex[i], (unsigned)ppComplex[i] - 8);

     
     }

     for(i = 0; i < 8; i++)
     {
      delete [] ppComplex[i];
     

     }


     return 0;

    }

  • Hi,

    Just tried the code you posted above...I don't see the problem you are talking about.  They aligned correctly for me.  I didn't even look at the System_printf(), I just look at the variable in memory.  See screeen shot below:

     

    Judah

  • Hi, Judah,Thanks for your patience. The moment I saw your result I'm just speechless!!!Following is the screen shot of my result and relevant setting. Might  you please point out the issues I may have missed? Thanks in advance.

     

  • ZF,

    Could you attach your test project here?  Just do a "file->export->archive file" in CCS and attach the zip file here.

    I don't know why its not working for you.

    Judah

  • Hi, Judah, this is the zip file requested. Feel free to ask any relevant info if required, thanks again.
    Test.zip
  • Zhongfan,

    Sorry for the delay.  I took your example and step through the code.  I see that HeapMem_alloc() returns the correct value...namely a ptr that is aligned to 256KB.  I see that malloc is also doing the right thing by adding 0x8 to the value.  What weird is the function "operator new" - addr begins at 0xc080e920 is the one that adds 0x8 to what is returned from malloc.  This is why you see the additional 0x10 instead of expected 0x8 from malloc.  Unfortunately, I don't know what this function is or what is does.

    Judah

  •  

    Judah, thanks so much for the efforts you put in pursuit of an answer. It seems the question can only be resolved by the person who write the 'new' operator code. I don't know whether it belongs to C++ compiler or SYSBIOS and documents regarding the implementation of this C++ 'new' key word in TI DSP platform is few and far between.

    Anyway, I'm glad this problem is not due to my fault. Hopefully, someone behind the CodeGen or SYSBIOS notice this discrepancy and correct it in later release or at least make it consistent??

    Regards

    Z. F. Yang