C++ Fooling with forces beyond my understanding

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Curious as always, I thought I'd spend some time playing with C++ on my current project. This has an xMega chip.

I found "Jax Coder's" page at http://jaxcoder.com/Projects.asp... and blinking an LED seemed an appropriate first task for making a class.

I had to adapt his class to xMega by adapting the class:

CBlink.h

/*
 * CBlink.h
 *
 * Created: 7/20/2014 2:41:00 PM
 *  Author: Tom
 */ 


#ifndef CBLINK_H_
#define CBLINK_H_

class CBlink
{
	public:
	//Constructor with the last parameter set to false as default.
	CBlink(volatile PORT_t * port, uint8_t pin, bool initiallyLit=false);
	
	bool IsLedLit() { return m_isLedLit; }
	void SetLitState(bool state);
	void ToggleLitState();
	
	private:
	bool m_isLedLit;
	
	volatile PORT_t * m_port;
	uint8_t  m_pin;
};



#endif /* CBLINK_H_ */

(Curiously, the clipboard has been working of late. Perhaps fixed by an MS update?)

CBlink.c

/*
 * CBlink.cpp
 *
 * Created: 7/20/2014 2:41:16 PM
 *  Author: Tom
 */ 

#include 
#include "CBlink.h"

CBlink::CBlink(volatile PORT_t * port, uint8_t pin, bool initiallyLit/* =false */)
{
	//Set the port addresses for the DDR and PORT values
	m_port = port;

	//Set the DDRn.pin as output
	m_port->DIRSET = _BV(pin);
	
	m_pin = pin;
	m_isLedLit = initiallyLit;
	
	//Set initial state of the LED
	SetLitState(m_isLedLit);
}

void CBlink::SetLitState(bool state)
{
	m_isLedLit = state;
	
	if (m_isLedLit)
		m_port->OUTCLR = _BV(m_pin);
	else
		m_port->OUTSET = _BV(m_pin);
}

void CBlink::ToggleLitState()
{
	SetLitState(!m_isLedLit);
}

Things went haywire when I tried to build the app, because there wasn't any _cplusplus.h Not knowing what else to do about it, I deleted the include statement, and my app builds and... egad, the led blinks!

/*
 * LearnC__01.cpp
 *
 * Created: 7/20/2014 2:37:23 PM
 *  Author: Tom
 */ 



#define F_CPU 2000000


#include 
#include 
//#include "_cplusplus.h"
#include "CBlink.h"

int main(void)
{
	CBlink  blink(&PORTA,3);
	
	while(1)
	{
		blink.ToggleLitState();
		

		_delay_ms(2000);

	}
}

He includes what looks like the _cplusplus.h file, so I added that:

/*
 * _cplusplus.h
 *
 * Created: 7/20/2014 3:22:50 PM
 *  Author: Tom
 */ 


#ifndef _CPLUSPLUS_H_
#define _CPLUSPLUS_H_

/*
    This is applicable if using virtual inheritance.
*/
__extension__ typedef int __guard __attribute__((mode (__DI__)));
 
extern "C" int __cxa_guard_acquire(__guard *);
extern "C" void __cxa_guard_release (__guard *);
extern "C" void __cxa_guard_abort (__guard *);
 
int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
void __cxa_guard_abort (__guard *) {};
 
/*
    This is applicable if using pure virtual inheritance.
*/
extern "C" void __cxa_pure_virtual(void);
 
void __cxa_pure_virtual(void) {};
 
/*
    Operators required for C++
*/
void* operator new(size_t size);
void operator delete(void* size);
 
void * operator new(size_t size)
{
    return malloc(size);
}
 
void operator delete(void* ptr)
{
    free(ptr);
}
 



#endif /* _CPLUSPLUS_H_ */

But when I try to include this code I don't understand, I get errors I don't understand. Strange, how that works, uMM?

Under Operators required for C++

/*
    Operators required for C++
*/
void* operator new(size_t size);

The error points to size_t and says two things:
"size_t was not declared in this scope" which is pretty obvious that it doesn't know what a size_t is.

Um, so is there something more I need to know about using C++ in studio 6.2?

I believe it's now time for a Sunday Afternoon walk.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

size_t is defined in stddef.h. Try including that at the top of _cplusplus.h (or better stdlib.h which includes stddef.h).

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That header file is not part of the avr-gcc tool chain (was not in WinAVR, is not in Atmel toolchain).

The header file contains prototypes for dynamic object creation with the new operator, object destruction (delete) etc. It does not contain definitions (implementations) of them.

As they came/come neither of these tool chains has implementations of the this new operator etc. If you don't need it, and the trivial code above does not use it, then just don't include that file. The formulation "needed for C++" is not correct. It should be "needed for dynamic object allocation/destruction" or some such.

If you need new/delete, perhaps not include that file (since it is not a complete solution) but search out the (long) discussion in the avr-gcc forum on getting avr-gcc to do C++.

Apart from that: size_t (which is not specific for C++, but there even in C) is typedef'ed in stddef.h. This file is in turn included from a lot of other header files in avrlibc (e.g. stdlib, stdio, string..).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm. Not sure I'd use new to get pointers to new objects, but object initialization and destruction seems a main feature of c++. Maybe I'll seek out that thread. However, it's a beautiful Sunday Evening in Tulsa, and I think I'll go back outside for a while :D

I see the trivial class having a construct method. Is there a way to make a destruct method so I can goof things up when the instance goes out of scope?

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"It takes strength to resist the dark side. Only the weak embrace it!
It is more powerful than you know..." Obi-Wan Kenobi

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems to me C is the dark side. And I used to use FORTH, so I KNOW darkness

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
I KNOW darkness
No you don't:
http://nickm.com/cis/a_box_darkl...
;)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
but object initialization and destruction seems a main feature of c++.
Yes, it is a central concept of C++, however it applies to all objects, not just the ones that are dynamically created. If you have a class Foo, then:

void function()
{
    Foo foo;
...
}

when this function is called, the constructor of foo is called, and when the function ends, the destructor of foo is called. No new or delete in sight.

Quote:
Is there a way to make a destruct method so I can goof things up when the instance goes out of scope?

class Foo
{
   Foo(); //constructor
  ~Foo(); //destructor
}

Quote:
Maybe I'll seek out that thread.
Easy to find: it is a sticky thread in the gcc forum.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One problem here is assumption and ignorance. You can get construction and destruction without new/delete operators. The key word for new/delete is "dynamic". It does not matter how you allocate an object. A constructor will run. It is only for dynamic allocation you need new/delete.

It is rare to learn C++ only by looking at examples and not doing serious reading of books etc.

And be aware that while C is a quite small language C++ is a large language.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think my curiosity it settled for a time.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut.