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.

TI Compiler v5.2.5 C++ "new" not running constructor.

Other Parts Discussed in Thread: TM4C123GH6PM

The "new" operator is not behaving as expected in my code. I am using TI Compiler v5.2.5 on CCSv6.1.2 in Linux(Ubuntu x64 14.04) with a Tiva TM4C123GH6PM over an Olimex XDS100v2 emulator.

This code works perfectly fine on the tiva:

class TestClass
{
public:
	TestClass()	{
		value = 5;
	};
	int value;
private:
};

int main(void) {

	TestClass tc;

	int test = tc.value;
	
	return 0;
}

"value" is set to 5.

However changing it to use a pointer to the "TestClass" causes the constructor to not be called and "value" is never initialized.

class TestClass
{
public:
	TestClass()	{
		value = 5;
	};
	int value;
private:
};
	TestClass *tc;

int main(void) {

	tc = new TestClass;

	int test = tc->value;
	
	return 0;
}

Here "value" is a random number, presumable the existing value at the location it references in memory.

  • Try changing

    tc = new TestClass;

    to

    tc = new TestClass();

    Stephen

  • Adding the parentheses didn't change the outcome. The constructor is not called and "value" is not initialized to 5.

  • It may be that the compiler is discarding the initialization because the variable "test" is never used. Try printing it out with printf.
  • It seems to have worked. Let me try it in my main code where I was having the issue before.

  • I am still having the same issue in my main project. I've gone ahead and uploaded the code here: https://github.com/AkBKukU/TimingTesting . The goal was to create a framework to have a base class that is part of a linked list that automates calling updates on objects and a fixed or custom interval. I have created a basic implementation of the base class that should pulse PF1 at 100Hz. The constructor never gets called on the created object though and it never gets added to the list or assigned an ID. My test in main just tries to retrieve the id through a get.

  • I was not planning on releasing this code, this was meant to just be a quick test so forgive the lack of documentation.

    The idea is to have a UpadateManager class that hold an plain root object(initialized in "UpadateManager::init") to use as the base for the list. It contains the reference for the next object like a traditional linked list. There is an interrupt that triggers every millisecond that calls the update function in UpadateManager. The update function will keep track of how many times it has been called and will update the fixedFunctions of the UpdateBase class objects every 10 milliseconds.

    There is a flaw in the design right now that could cause the interrupt to be called before the update cycle is done, but I don't believe it is related.

  • Okay, thank you; I'm looking at your files right now. Looking in TimingTesting-master/Debug/main.obj, I see a call to LEDBlink::LEDBlink at offset 0x1c, right after the call to operator new, so the construct is definitely getting called. Looking at LEDBlink.obj, the first thing that constructor does is call the constructor UpdateBase::UpdateBase, which calls UpdateManager::assignID. At first glance, the compiler is doing everything it's supposed to.

    Now, looking at the executable file TimingTesting.out, I see that your .sysmem is only 8 bytes long, which is almost surely the problem. Most likely, operator new is returning NULL and the constructor is scribbling on data near address 0. You can observe this by compiling with C++ exceptions turned on; in that case, operator new will throw std::bad_alloc. Set .sysmem to a reasonable size with the --heap_size linker option, and that should fix the problem.

    [ Edit: heap, not stack -- Archaeologist ]

  • That seems to have fixed it! It called the constructor(I'm stepping through the code in CCS) and set everything as it should. It immediately encounters an error I forgot to take into consideration where it needs the root objects initialized to add an object and the root objects try to access themselves, but I think it will be fine after I fix that. Thank you very much for the help!