0

I want to compile some of my cpp-functions with the avr-g++ compiler & linker. My experience from former projects tells me that it definitely works with new and delete. But somehow this function compiles without errors:

void usart_controller::send_data(uint32_t * data32, size_t data32_size)
{
    size_t data_size = 4 * data32_size;
    //uint8_t * data = new uint8_t[data_size];
    uint8_t data[data_size];
    uint8_t *data_ptr = &data[0];
    for(unsigned int i = 0; i < data32_size; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            data[i*j+j] = (data32[i] << (j*8));
        }
    }
    /*usart_serial_write_packet(this->usart, *data_ptr, (size_t)(data_size * sizeof(uint8_t)));*/
    size_t len = sizeof(uint8_t)*data_size;
    while (len) {
        usart_serial_putchar(this->usart, *data_ptr);
        len--;
        data_ptr++;
    }
    //delete[] data;//Highly discouraged, because of memory leak!//Works as a charme because of C, but I don't care at the moment
}

but the same function with new does not work:

void usart_controller::send_data(uint32_t * data32, size_t data32_size)
{
    size_t data_size = 4 * data32_size;
    uint8_t * data = new uint8_t[data_size];
    //uint8_t data[data_size];
    //uint8_t *data_ptr = &data[0];
    for(unsigned int i = 0; i < data32_size; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            data[i*j+j] = (data32[i] << (j*8));
        }
    }
    /*usart_serial_write_packet(this->usart, *data_ptr, (size_t)(data_size * sizeof(uint8_t)));*/
    size_t len = sizeof(uint8_t)*data_size;
    while (len) {
        usart_serial_putchar(this->usart, *data);
        len--;
        data++;
    }
    delete[] data;
}

Here I get the following errors:

error: undefined reference to `operator new[](unsigned int)'
error: undefined reference to `operator delete[](void*)'

The compiling and linking command is (shorted):

"C:\Program Files (x86)\Atmel\Atmel Toolchain\AVR8 GCC\Native\3.4.1061\avr8-gnu-toolchain\bin\avr-g++.exe" -o PreAmp.elf <...> usart_controller.o <...> -Wl,-Map="PreAmp.map" -Wl,--start-group -Wl,-lm  -Wl,--end-group -Wl,--gc-sections -mmcu=atxmega16a4u

so I am assuming that I am using the g++-compiler and not the gcc-compiler. But in cpp it is impossible to declare a variable-length array as done above. Where is my mistake here?

7
  • variable-length arrays are a GNU extension. g++ is the C++ frontend of the GCC compiler suite. In modern C++ code, you hardly ever use new or delete, but library functionality that takes care of memory management for you (via containers and smart pointers). Commented Mar 9, 2015 at 15:49
  • Your second code should be fine when compiled with the right settings, but use vectors instead of raw new. Commented Mar 9, 2015 at 15:51
  • @NeilKirk: Which settings should be corrected? And unfortunately I can not use stuff like std::vector, the include file is missing (and afaik for avr-µCs not suitable) Commented Mar 9, 2015 at 15:52
  • If you don't have other standard C++ stuff, why do you expect new/delete to work? Commented Mar 9, 2015 at 15:52
  • @CarlNorum: What do you mean with that? I simply reused code from another project, and there it worked. Commented Mar 9, 2015 at 15:54

1 Answer 1

1

I did not see any information on controller used, IDE (if any). But if you are using Atmel studio/AVR tool chain from atmel.

They make it pretty clear that new and delete functionality is not supported and has to be implemented by user.

This makes sense since this is not a desktop application but a implementation on uC.

http://www.atmel.com/webdoc/avrlibcreferencemanual/faq_1faq_cplusplus.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.