Wednesday, August 19, 2020

Digest for comp.lang.c++@googlegroups.com - 19 updates in 3 topics

RM <robert_magdziarz@wp.pl>: Aug 19 01:45PM +0200

W dniu 14.08.2020 o 14:16, RM pisze:
> How to correct my code?
 
I changed my code to the following:
strvector *identifiers, *random_identifiers, *framework_identifiers;
a my program compiles and runs.
Problem solved.
Thank you very much for your answers!
Juha Nieminen <nospam@thanks.invalid>: Aug 19 01:02PM

> a my program compiles and runs.
> Problem solved.
> Thank you very much for your answers!
 
Can you show us your constructor and destructor? Because I have a hunch that
you might not be doing it correctly.
RM <robert_magdziarz@wp.pl>: Aug 19 03:29PM +0200

W dniu 19.08.2020 o 15:02, Juha Nieminen pisze:
>> Thank you very much for your answers!
 
> Can you show us your constructor and destructor? Because I have a hunch that
> you might not be doing it correctly.
 
obfuscator() {
static_assert(i_variables == INDEX_WHAT_NUM - 1);
status = s_not_ready;
keywords = " " + reserved_keywords + " " +
strtoupper(reserved_keywords) + " ";
random_identifiers_length = default_random_identifiers_length;
extension = default_extension;
identifiers = new strvector[INDEX_WHAT_NUM];
random_identifiers = new strvector[INDEX_WHAT_NUM];
framework_identifiers = new strvector[INDEX_WHAT_NUM];
error_messages = "";
}
~obfuscator() {
delete [] identifiers;
delete [] random_identifiers;
delete [] framework_identifiers;
}
Bonita Montero <Bonita.Montero@gmail.com>: Aug 19 03:38PM +0200

Am 14.08.2020 um 14:16 schrieb RM:
>     }
> ...
> };
 
Why do you use an array of pointers to std::vector<string> ?
Use a vector<vector<string>>, and if you have performance-concerns
use .reserve().
Juha Nieminen <nospam@thanks.invalid>: Aug 19 03:08PM

> identifiers = new strvector[INDEX_WHAT_NUM];
 
You are not creating a vector of strings there. You are creating
an array of INDEX_WHAT_NUM vectors, each one containing strings.
 
Somehow I doubt that's what you intended. It looks to me more
like you want *one* vector containing INDEX_WHAT_NUM strings,
rather than an array of INDEX_WHAT_NUM vectors, each containing
strings.
 
You should not be playing with new and delete if you don't know
what you are doing. If what you want is just a vector of strings,
you do this:
 
//----------------------------------------------------
class MyClass
{
strvector identifiers;
 
public:
MyClass()
{
identifiers.resize(INDEX_WHAT_NUM);
}
};
//----------------------------------------------------
 
If what you really want is a vector of vectors of strings,
then you do this:
 
//----------------------------------------------------
class MyClass
{
std::vector<strvector> identifiers;
 
public:
MyClass()
{
identifiers.resize(INDEX_WHAT_NUM);
}
};
//----------------------------------------------------
 
You don't need a destructor in either case.
 
In the first case above you have *one* vector containing
many strings inside it.
 
In the second case above you have a vector containing
one or more strvector objects, each containing many strings
inside them.
 
In neither case is 'new' and 'delete' needed nor recommended.
Barry Schwarz <schwarzb@delq.com>: Aug 19 09:55AM -0700

On Wed, 19 Aug 2020 15:38:40 +0200, Bonita Montero
>> ...
>> };
 
>Why do you use an array of pointers to std::vector<string> ?
 
identifiers and the other variables declared above are not arrays of
pointers. Each is a pointer to an array. That is why each use of new
produced a diagnostic.
 
--
Remove del for email
Bonita Montero <Bonita.Montero@gmail.com>: Aug 19 07:15PM +0200

Ok, it's a pointer to an array of objects and not a pointer-array.
But this isn't RAII-safe. If one allocation fails after another
the allocation before isn't cleaned up. Instead you could use a#
vector of objects.
James Kuyper <jameskuyper@alumni.caltech.edu>: Aug 19 10:33AM -0700

On Wednesday, August 19, 2020 at 9:38:51 AM UTC-4, Bonita Montero wrote:
> > class obfuscator {
> > ...
> >     strvector (*identifiers)[INDEX_WHAT_NUM],
...
> Why do you use an array of pointers to std::vector<string> ?
 
strvector is not a typedef for std::vector<string>, it is a class derived from std::vector<string>.
 
An array of pointers to strvector would be declared as follows:
 
strvector *identifiers[INDEX_WHAT_NUM];
 
What he actually wrote was
 
strvector (*identifiers)[INDEX_WHAT_NUM];
 
which declares a pointer to an array of strvectors of length INDEX_WHAT_NUM.
Bonita Montero <Bonita.Montero@gmail.com>: Aug 19 07:47PM +0200

> What he actually wrote was
> strvector (*identifiers)[INDEX_WHAT_NUM];
> which declares a pointer to an array of strvectors of length INDEX_WHAT_NUM.
 
Your posting is superfluous since I already corrected that.
But my tip to use a vector<vector<string>> is nevertheless correct.
James Kuyper <jameskuyper@alumni.caltech.edu>: Aug 19 11:31AM -0700

On Wednesday, August 19, 2020 at 1:47:46 PM UTC-4, Bonita Montero wrote:
> > which declares a pointer to an array of strvectors of length INDEX_WHAT_NUM.
 
> Your posting is superfluous since I already corrected that.
> But my tip to use a vector<vector<string>> is nevertheless correct.
 
I don't think so. First of all, strvector is derived from
std::vector<string>. He might not have had any good reason for doing so,
but unless you know more about what he's doing than he's publicly
explained, it could very well be that he needs for strvector to be a
non-trivial wrapper for std::vector<string>.
 
More importantly, his latest messages indicate that he's changed over to
 
strvector *identifier;
 
His judgment that this is the correct fix to his problem is suspect, but
if his code actually compiles with that declaration, I think it's
unlikely to be the case that what he actually needs is
std::vector<strvector>.
Bonita Montero <Bonita.Montero@gmail.com>: Aug 19 08:49PM +0200

Don't talk around to be somehow right. He wasn't using a wrapper.
What I suggested was a better replacement for his code.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Aug 19 07:14PM

On Wed, 2020-08-19, RM wrote:
> delete [] random_identifiers;
> delete [] framework_identifiers;
> }
 
You wrote in your first posting that you're learning C++ ... but
you're learning the wrong things. You don't need C arrays or new in
code like this.
 
I still think you need to unlearn a whole lot of Java.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
James Kuyper <jameskuyper@alumni.caltech.edu>: Aug 19 12:58PM -0700

On Wednesday, August 19, 2020 at 2:50:10 PM UTC-4, Bonita Montero wrote:
> Don't talk around to be somehow right. He wasn't using a wrapper.
 
On Friday, August 14, 2020 at 8:16:13 AM UTC-4, RM wrote:
...
> class strvector : public std::vector<std::string> {
> ...
> };
 
If you don't call that a wrapper, what do you call it?
More directly to the point, regardless of what term you use for it, if he followed your suggestion of replacing
 
strvector (*identifiers)[INDEX_WHAT_NUM]
 
with
 
vector<vector<string>> identifiers
 
what are you recommending he do with the code that is represented by "..." above?
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Aug 19 01:08PM -0700

On 8/14/2020 5:16 AM, RM wrote:
>     strvector (*identifiers)[INDEX_WHAT_NUM],
> (*random_identifiers)[INDEX_WHAT_NUM],
> (*framework_identifiers)[INDEX_WHAT_NUM];
[...]
> How to correct my code?
 
 
Would something like this work for you?
____________________________________________
#include <iostream>
#include <array>
#include <vector>
 
 
#define INDEX_WHAT_NUM 3
 
 
struct strvector : public std::vector<strvector>
{
strvector()
{
std::cout << this << "::strvector::strvector()\n";
}
 
~strvector()
{
std::cout << this << "::strvector::~strvector()\n";
}
};
 
 
struct obfuscator
{
std::array<strvector, INDEX_WHAT_NUM> identifiers;
std::array<strvector, INDEX_WHAT_NUM> random_identifiers;
std::array<strvector, INDEX_WHAT_NUM> framework_identifiers;
};
 
 
int main()
{
{
obfuscator ob;
}
 
return 0;
}
____________________________________________
 
 
?
Frederick Gotham <cauldwell.thomas@gmail.com>: Aug 19 07:41AM -0700

If I compile my program with the GNU compiler and the flag "-fstack-protector", it prints out the following:
 
*** stack smashing detected ***: <unknown> terminated
 
However if I compile my program with "-fno-stack-protector", then it runs fine and doesn't seem to malfunction in any way.
 
I have used "gdb" and "valgrind" on both versions of my program (the one with stack protection and the one without), and neither 'gdb' nor 'valgrind' can spot the problem.
 
Have any of you ever experienced a false positive from the GNU compiler's stack-smashing detector?
Ian Collins <ian-news@hotmail.com>: Aug 19 08:46PM +1200

On 19/08/2020 01:32, Frederick Gotham wrote:
 
> At the moment I'm taking the encryption technique I've coded in
> multithreaded C++ code, and porting it to single-threaded C for use
> on a microcontroller.
 
Why don't you just use a better microcontroller with a C++ compiler?
 
--
Ian.
Juha Nieminen <nospam@thanks.invalid>: Aug 19 09:17AM

>> multithreaded C++ code, and porting it to single-threaded C for use
>> on a microcontroller.
 
> Why don't you just use a better microcontroller with a C++ compiler?
 
Sometimes there's no choice because the microcontroller is demanded by a
client.
Daniel Hyde <Daniel.Hyde71@gmail.com>: Aug 19 11:21AM +0200

> Sometimes there's no choice because the microcontroller is demanded by a
> client.
 
Kill the client !
David Brown <david.brown@hesbynett.no>: Aug 19 12:43PM +0200

On 19/08/2020 11:17, Juha Nieminen wrote:
 
>> Why don't you just use a better microcontroller with a C++ compiler?
 
> Sometimes there's no choice because the microcontroller is demanded by a
> client.
 
If the client asks for something that is clearly virtually impossible -
such as converting a large C++ program into something that will run on
this microcontroller (with its severely limited 8-bit cpu, poor C
compiler, and 3K of ram), including Ethernet handling - then that's
usually something you discuss with the client. Perhaps there are
situations where it is best to simply try as hard as you can, but
usually honesty works best here.
 
In this case, the OP has chosen the microcontroller here himself as far
as I can tell. But I don't have a complete picture - he has described a
device that has two network ports, where unencrypted traffic comes in on
one port and encrypted traffic goes out on the other. How he intends to
do that with this PIC, with one Ethernet connection, is unknown to me.
 
Perhaps he will use two of them, connected back-to-back via a UART
interface. If all the encryption is done at the Ethernet packet level,
rather than TCP/IP or UDP level, then it's conceivable that it could be
done even with this small ram. (What the device would then talk to is
another matter.) It would be "security by making a system so slow that
no one would use it and thus there is no security risk", but maybe
that's his aim.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: