Converting a project from c to cpp?

Go To Last Post
61 posts / 0 new

Pages

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

That (single character) is a better solution! Fewer indexing operations, less passed as parameters.

 

But, certainly, one should be able to pass a variable length character array by reference?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Tue. Nov 21, 2017 - 08:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
But, certainly, one should be able to pass a variable length character array by reference?
std::string is a far better container for "strings" than C's char * because, amongst other things, they carry their length with them.

 

Only problem is that std::string is not immediately available in avr-g++ because it's a component of the Standard Template Library (STL) and by default C++ for AVR does not have STL.

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

It doesn't seem to be possible to pass a reference to an array of unknown size, the compiler demands the size of the array:

void PutChar( const char (& SendString)[10], uint8_t & charnum ) {

    //just exit if last character
    if ( SendString[charnum] == '\0' )
	return;

    //wait for previous character to finish
    while ( !(UCSR0A & (1<<UDRE0) ) ) {}

    //write to UART data register, increment charcter index, and exit
    UDR0 = SendString[charnum];
    charnum ++;

    }    //end PutChar()}

This makes sense when you consider the view that references are aliases, since you can't really have arrays of an unknown size. In some cases, the compiler can calculate the size automatically, but it's never unknown.

 

edit: However, due to the incredible flexibility of C++, we can still use templates and other language facilities to do crazy stuff like this:

 

#include <stdint.h>

template <class T>
void PutChar_template( const char (& SendString)[sizeof(T)], uint8_t & charnum ) {
  ; // ...
}

int main()
{
    const char msg[] = "Hello World!";
    uint8_t count = 10;
    auto & PutChar = PutChar_template<decltype(msg)>;

    PutChar(msg, count);
}

 

Last Edited: Tue. Nov 21, 2017 - 11:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

 

std::string is a far better container for "strings" than C's char * because, amongst other things, they carry their length with them.

Not "far far"?

“It is a far, far better thing that I do, than I have ever done; it is a far, far better rest that I go to than I have ever known.”

Charles Dickens, A Tale of Two Cities

Beauty is in the eye of the beholder.  Of course you new-fangled ++ fanboys would make the claim.  The discussion is almost as old as endianness.  If you care to carry the baggage of length and bounds checking and associated, then continue Pascal tradition.  If you want to

Keep the spirit of C.

...

• Trust the programmer.

• Don’t prevent the programmer fr

om doing what needs to be done.

• Keep the language small and simple.

• Provide only one way to do an operation.

• Make it fast, even if it is not guaranteed to be portable.

...then get rid of the "far"'s, and maybe even the "better".

 

YMMV.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:
Provide only one way to do an operation

IS that really a "spirit of C" thing?

 

"many ways to skin a cat" seems more C-like to me ...

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

awneil wrote:
IS that really a "spirit of C" thing?

http://www.open-std.org/jtc1/sc2...

 

Rationale for International Standard—

Programming Languages—C

Revision 5.10

April-2003

Chapter 0:  Introduction

 

The last proverb needs a little explanation. ...

Like Will Rogers, I only know what I read in the papers.

 

“All I know is just what I read in the papers, and that's an alibi for my ignorance.”

Will Rogers

http://www.thisdayinquotes.com/2...

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

clawson wrote:
std::string is a far better container for "strings" than C's char * because, amongst other things, they carry their length with them.

A heavier argument, IMO, i that a C++ std::string is much more safe than a C char*. You can get the C pointer to point to just about any invalid location with little "effort", without any protest and with dire consequences at run time. The Cpp std::string is a strong(er) typed object in several senses. It's not a pointer so does not easily fuddle with a bad memory location. This is a build-time security.

 

As Lee notes, there are some runtime costs for certain operations, so just as your system resources vary and your time spent coding and debugging is a variable, so is your mileage. 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"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

This project relies heavily on strings - CSV "records" written to uSD or to the serial port. I will do some more reading as this is a serious issue and will probably be back in a new thread. I am concerned about Cliff's comment:

Only problem is that std::string is not immediately available in avr-g++ because it's a component of the Standard Template Library (STL) and by default C++ for AVR does not have STL.

Beyond being clueless about templates, the fact that it does not seem readily available is troubling. Arduino does something - pointers rather than references, maybe? I hope that I don't spend a lot of time that I cannot afford on this!

 

Thanks

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Tue. Nov 21, 2017 - 04:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just googled around about STL for avr-g++ and this thread on Arduino seems to sum things up:

 

https://forum.arduino.cc/index.p...

 

Of that the consensus seems to be that while the "Andy Brown" STL is possibly the most widely used it's possible the "maniacbug" one ("StandardCPlusPlus") might be the best/easiest to use.

 

Having said that I also found this:

 

https://libavrstdcpp.codeplex.com/

 

On the surface that looks "promising" (very "minimal") but then I look at something like:

 

https://libavrstdcpp.codeplex.co...

 

and the file just looks like an empty placeholder while for something like "string":

 

https://libavrstdcpp.codeplex.co...

 

the implementation looks fairly complete - so it may be "half finished"

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

For now, I am going to stick with C-style pointer based character arrays. I simply cannot afford the time and effort to figure out how to make one of those work. If I were more experienced, I might do otherwise, but I am not (more experienced)!

 

Cheers

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Pages