Monday, November 10, 2014

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

comp.lang.c++@googlegroups.com Google Groups
Unsure why you received this message? You previously subscribed to digests from this group, but we haven't been sending them for a while. We fixed that, but if you don't want to get these messages, send an email to comp.lang.c+++unsubscribe@googlegroups.com.
Robert Hutchings <rm.hutchings@gmail.com>: Nov 10 12:42PM -0600

Hi all,
 
Here is a question from Bloomberg ----
 
Given the following structure Record and we have million of records on disk.
 
struct Record
{
char[257] name;
int startTime;
char[257] description;
}
 
Now we want to keep a in-memory cache which is represented in class
ManageRecords to perform 2 methods GetDesriptions and GetRecords.
Given that we have very big memory and we can use any data structure so
that the 2 methods can be performed really quick.
Here are the questions:
 
1. what should be the return type for method GetDesriptions
2. what should be the return type for method GetRecords
3. what data structure should we use in the private part as commented
out below
 
 
class ManageRecords
{
public:
 
ManageRecords();
 
? GetDesriptions(char[] name);
 
? GetRecords(int begin, int end); //do a range search based on
startTime of structure Record
 
private:
 
// what data strcture should we use here?
}
Wouter van Ooijen <wouter@voti.nl>: Nov 10 07:58PM +0100

Robert Hutchings schreef op 10-Nov-14 7:42 PM:
> that the 2 methods can be performed really quick.
> Here are the questions:
 
> 1. what should be the return type for method GetDesriptions
 
GetDesriptions
------=------=
 
I don't want no desriptions!
 
More seriously, what do kind of answer do they expect for something that
seems to return multiple things? Maybe a class that has begin() and
end() methodes to facilitate
 
for( auto & record : GetDesriptions() ){
do a lot of desriptive things
// should deleting a record and/or changing
// its description be allowed here?
}
 
Or maybe they expect a container that contains copies of those things?
 
Or a container that contains references, or shared_ptr's?
 
Or maybe it is a trick questions: no real specification => turn in an
empty answer?
 
 
Wouter
Robert Hutchings <rm.hutchings@gmail.com>: Nov 10 01:09PM -0600

On 11/10/2014 12:58 PM, Wouter van Ooijen wrote:
 
> Or maybe it is a trick questions: no real specification => turn in an
> empty answer?
 
> Wouter
Yeah, I guess they want to see your thinking process on vague questions
like this...I would think that GetDescriptions would return a collection
of 1 or more "descriptions"??
Christopher Pisz <nospam@notanaddress.com>: Nov 10 01:14PM -0600

On 11/10/2014 1:09 PM, Robert Hutchings wrote:
> Yeah, I guess they want to see your thinking process on vague questions
> like this...I would think that GetDescriptions would return a collection
> of 1 or more "descriptions"??
 
 
You already know what they want.
They want to see if you are going to be silly and copy millions of
records by value or whether you know better.
Robert Hutchings <rm.hutchings@gmail.com>: Nov 10 01:19PM -0600

On 11/10/2014 1:14 PM, Christopher Pisz wrote:
 
> You already know what they want.
> They want to see if you are going to be silly and copy millions of
> records by value or whether you know better.
 
One person answered the question with this...
 
"Have a balanced BST holding records sorted on timestamp. The question
will reduce to find nodes in given range in a bst. For descriptions keep
a hash with values pointer to the nodes in the balanced BST nad return
all matching descriptions".
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 10 07:37PM

On 10/11/2014 19:19, Robert Hutchings wrote:
> will reduce to find nodes in given range in a bst. For descriptions keep
> a hash with values pointer to the nodes in the balanced BST nad return
> all matching descriptions".
 
Extra point for not re-inventing the wheel and using std::set and
std::map instead.
 
/Flibble
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 10 07:38PM

On 10/11/2014 19:37, Mr Flibble wrote:
>> all matching descriptions".
 
> Extra point for not re-inventing the wheel and using std::set and
> std::map instead.
 
Or more realistically std::multiset and std::multimap.
 
/Flibble
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 10 07:40PM

On 10/11/2014 19:38, Mr Flibble wrote:
 
>> Extra point for not re-inventing the wheel and using std::set and
>> std::map instead.
 
> Or more realistically std::multiset and std::multimap.
 
Or possibly even unordered_multimap.
 
/Flibble
Rosario193 <Rosario@invalid.invalid>: Nov 10 09:55PM +0100

On Mon, 10 Nov 2014 12:42:26 -0600, Robert Hutchings wrote:
> int startTime;
> char[257] description;
>}
 
possibly it is better write as first element of struct int startTime;
because i believe that each recod for align that int waste 3 chars
 
>that the 2 methods can be performed really quick.
>Here are the questions:
 
>1. what should be the return type for method GetDesriptions
 
a pointer to char
 
>2. what should be the return type for method GetRecords
 
one u32 one index
 
>startTime of structure Record
 
> private:
 
> // what data strcture should we use here?
 
but if i have to choose [i not use private]:
 
public:
 
Record *a;
 
Rosario193 <Rosario@invalid.invalid>: Nov 10 09:58PM +0100

On Mon, 10 Nov 2014 21:55:58 +0100, Rosario193
 
>>struct Record
>>{
>> char[257] name;
 
i don't know i know "char name[257];" not "char[257] name;"
 
Robert Hutchings <rm.hutchings@gmail.com>: Nov 10 03:15PM -0600

On 11/10/2014 2:55 PM, Rosario193 wrote:
 
> public:
 
> Record *a;
 
>> }
 
Okay, when you said "one u32 one index" are you talking about a Struct?
Ian Collins <ian-news@hotmail.com>: Nov 11 10:38AM +1300

Robert Hutchings wrote:
> ManageRecords to perform 2 methods GetDesriptions and GetRecords.
> Given that we have very big memory and we can use any data structure so
> that the 2 methods can be performed really quick.
 
Is that verbatim? The grammar looks like a Nigerian phishing scam...
 
--
Ian Collins
Robert Hutchings <rm.hutchings@gmail.com>: Nov 10 03:44PM -0600

On 11/10/2014 3:38 PM, Ian Collins wrote:
>> Given that we have very big memory and we can use any data structure so
>> that the 2 methods can be performed really quick.
 
> Is that verbatim? The grammar looks like a Nigerian phishing scam...
 
No, it's from a web page that is supposedly an "interview prep"
site...and, yes, the grammar is strange...
Ian Collins <ian-news@hotmail.com>: Nov 11 10:56AM +1300

Robert Hutchings wrote:
 
>> Is that verbatim? The grammar looks like a Nigerian phishing scam...
 
> No, it's from a web page that is supposedly an "interview prep"
> site...and, yes, the grammar is strange...
 
That, couple with the near 50% syntax error per line ratio says all that
needs to be said about that site.
 
--
Ian Collins
agent@drrob1.com: Nov 09 07:41PM -0500

I am using Ubuntu 14.04 amd64 and am trying to learn using c++ for
Dummies (c) 2014 edition.
 
The book uses this syntax often, but gcc is saying
warning: deprecated conversion from string constant to char*
[-Wwrite-strings]
 
g++ -Wall -g -std=c++11 -o studentexample studentexample.cpp
 
This is studentexample.cpp
class Student {
public:
static int noOfStudents;
string name;

// warning flagged here
Student(const char * pName = "no name") : name(pName) {
nOfStudents++;
} // constructor
 
~Student() {
noOfStudents--;
} // destructor
 
const string REF getName() { return name; }
static FUNCTION int getNumber() { return noOfStudents; }
} // class Student
 
int Student::noOfStudents=0; // allocates space for the static data
//member and init's to 0.
 
int main() {
 
// warning flagged for both of these
Student s1("Chester");
Student * pS2 = new Student("Charlie");
 
cout << "No of s1.students " << s1.noOfStudents << endl;
cout << "Created " << s1.getName() << " and " << pS2->GetName()
<< endl;
 
cout << "Deleting " << pS2->getName() << endl;
delete pS2;
cout << " Number of students is now " << Student::getNumber()
<< endl;
 
return 0;
END;
 
 
What is the proper way to do what the book is doing that is not
deprecated?
"Öö Tiib" <ootiib@hot.ee>: Nov 09 04:51PM -0800

> [-Wwrite-strings]
 
> g++ -Wall -g -std=c++11 -o studentexample studentexample.cpp
 
> This is studentexample.cpp
 
Perhaps you did post some slice of real thing. If you want
to post code that gives warnings then post something that
does compile.
Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 10 12:54AM

> [-Wwrite-strings]
 
> g++ -Wall -g -std=c++11 -o studentexample studentexample.cpp
 
> This is studentexample.cpp
 
Really? The program you post and the errors you report are
inconsistent. The code below has a number of simple errors in it
(incorrect names, a missing semicolon, that sort of thing) but it does
not contain a conversion from string to char *.
 
> END;
 
> What is the proper way to do what the book is doing that is not
> deprecated?
 
You do what you've done -- the error does not exist in this code. (In
case you don't know what you've done, it's using const char * rather
than char *. It would generally be better to use std::string from the
outset, but that's another story.)
 
--
Ben.
agent@drrob1.com: Nov 10 08:04AM -0500

On Mon, 10 Nov 2014 00:54:36 +0000, Ben Bacarisse
>case you don't know what you've done, it's using const char * rather
>than char *. It would generally be better to use std::string from the
>outset, but that's another story.)
 
This is the code I actually use, with my macros and all. I tried to
remove these macros before posting, but it seems that I did not do
that correctly.
 
This code compiles on my computer.
 
#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ctime>
#include <string> // string class of c++
 
#define EQ ==
#define NE !=
#define BEGIN {
#define END }
#define ENDIF }
#define ENDFOR }
#define ENDCASE }
#define ENDLOOP }
#define ENDWHILE }
#define ENDDOO } while // intended for the do { } while (expr);
looping construct
#define ENDFUNC }
#define ENDPROC }
#define MOD %
#define AND &&
#define OR ||
#define NOT !
#define THEN {
#define ELSE }else{
#define ELSIF }else if
#define IS {
#define DOO do { // intended for the do {} while (expr);
looping construct
#define DO {
#define LOOP {
#define ADROF &
#define ADR &
#define DEREF *
#define POINTERTO *
#define POINTER *
#define VAR &
#define REF &
#define PROCEDURE void
#define FUNCTION
#define BITAND &
#define BITOR |
#define BITNOT ~
#define BITXOR ^
#define argcargv int argc, char * argv[]
#define ADDRESS void* // typedef line is an error
#define COPY &
#define MOVE &&
 
typedef FILE* FileHandle;
typedef char* PointerToCharType;
typedef char* CharPointerType;
 
class Student IS
public:
static int noOfStudents;
string name;
 
Student(CharPointertype pName = "no name") : name(pName) IS
nOfStudents++;
END; // constructor
 
~Student() IS
noOfStudents--;
END; // destructor
 
const string REF getName() IS return name; END;
static FUNCTION int getNumber() IS return noOfStudents; END;
END; // class Student
 
int Student::noOfStudents=0; // allocates space for the static data
member and init's to 0.
 
FUNCTION int main(argcargv) IS
 
Student s1("Chester");
Student POINTER pS2 = new Student("Charlie");
 
 
cout << "No of s1.students " << s1.noOfStudents << endl;
cout << "No of Student::students " << Student::noOfStudents << end;
// equivalent way to access the static data element.
 
cout << "Created " << s1.getName() << " and " << pS2->GetName() <<
endl;
 
cout << "Deleting " << pS2->getName() << endl;
delete pS2;
cout << " Number of students is now " << Student::getNumber() <<
endl;
 
return 0;
END;
Richard Damon <Richard@Damon-Family.org>: Nov 10 08:26AM -0500

> typedef char* CharPointerType;
 
> Student(CharPointertype pName = "no name") : name(pName) IS
 
(I am presuming the capitalization difference is a typo)
 
First suggest, burn any book that told you to use all those macros to
make your code more "readable", they just make things incomprehensible
for those who really know the language.
 
The above two lines show the problem.
 
CharPointerType is a pointer to NON-CONST char, while string literals
are defined as arrays of const char. (In C, the type was array of
non-writable but not-const char for historical reasons).
 
Since the Student constructor doesn't modify the character string passed
to it, it should declare that it takes a pointer to const char, not a
pointer to char, and things will be fine. (Your original posting of code
did that).
 
The reason this was just a warning and not a full error is that like 25
years ago, before C had const (and C++ was just being born), since the
language didn't have const, string literals would be assigned to char*
pointers (and on some systems WERE writeable). When const was added to
the language, it was thought to make C string literals to be const, but
that would have broken too much code since the pointers they wee being
assigned to weren't const (but many tools were set to be able to give
warnings for this, and over time this has become more of the default, as
hopefully most of that ancient code has been fixed). C++ on the other
hand, since it didn't have that large code base, defined string literals
to be const, but many tools would have an option to make this just a
warning (or even ignored) to help with porting these older programs to C++.
Ben Bacarisse <ben.usenet@bsb.me.uk>: Nov 10 01:57PM


> This is the code I actually use, with my macros and all. I tried to
> remove these macros before posting, but it seems that I did not do
> that correctly.
 
No, and in doing the conversion you fixed the very problem you were
asking about! A simple compile run would have shown you that the
problem had gone away (once you'd fixed the other errors you'd
introduced). Anyway, you now know how to fix the problem, yes?
 
<snip>
> #define ENDCASE }
> #define ENDLOOP }
> #define ENDWHILE }
 
...and so on. Macros like thee are a very bad idea.
 
<snip>
> typedef char* PointerToCharType;
> typedef char* CharPointerType;
 
When you translated your code, you changed these types, thus fixing the
issue you were posting about.
 
<snip>
--
Ben.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 10 06:04PM

> #define ADDRESS void* // typedef line is an error
> #define COPY &
> #define MOVE &&
 
Dafuq? :/
 
/Flibble
Geoff <geoff@invalid.invalid>: Nov 10 01:38PM -0800

On Mon, 10 Nov 2014 18:04:59 +0000, Mr Flibble
>> #define MOVE &&
 
>Dafuq? :/
 
>/Flibble
 
This goes back more than 10 years to something called "Easy C" that
played havoc with the language. When I first saw something like this
it was related to the obfuscated C contests. It was horrible to
contemplate someone learning C this way. I haven't looked at the book
the OP cites but I hope that book doesn't contain this abomination.
Keith Thompson <kst-u@mib.org>: Nov 10 01:54PM -0800

Geoff <geoff@invalid.invalid> writes:
[...]
>>> #define ENDLOOP }
>>> #define ENDWHILE }
>>> #define ENDDOO } while // intended for the do { } while (expr);
[snip]
> it was related to the obfuscated C contests. It was horrible to
> contemplate someone learning C this way. I haven't looked at the book
> the OP cites but I hope that book doesn't contain this abomination.
 
Similar macros go back much further than that. The original Bourne
shell was written in C using macros intended to make it look like
Algol 68.
 
http://stackoverflow.com/a/1067349/827263
http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/mac.h
 
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Marcel Mueller <news.5.maazl@spamgourmet.org>: Nov 10 07:34PM +0100

On 07.11.14 01.37, Öö Tiib wrote:
> Compiler? You mean you keep all the names from different scopes in
> same container and indicate scope with int?
 
Yes. The Scope is only a depth.
 
> It feels upside
> down and it can not be efficient. If it is compiler of something like
> C++ then that single int can not be sufficient for name lookup.
 
No, it is by far not that complex than C++. It is a macro assembler for
a Broadcom GPU. So basically it is about labels, constants, user defined
functions and macros. Nothing like namespaces or ADL.
 
But maybe I use a different approach. If I have a separate dictionary
for each scope I can easily discard deeper dictionaries at the end of a
scope block. The price is N lookups per global symbol in a scope with
depth N. Not optimal, but on nowadays hardware no big deal.
 
From my point of a data structure that uses the symbol name as first
key better fits my needs. Therefore the question. At least I can learn more.
 
 
Marcel
"Öö Tiib" <ootiib@hot.ee>: Nov 10 01:02PM -0800

On Monday, 10 November 2014 20:34:15 UTC+2, Marcel Mueller wrote:
> > Compiler? You mean you keep all the names from different
> > scopes in same container and indicate scope with int?
 
> Yes. The Scope is only a depth.
 
Ok, but on case the outer scope names are hidden ... why
to keep them also in lookup table within scope?
 
Lets say scope 7 declares name "a", and there are no names
that it hides. You can add name "a" to lookup table and
memorize to remove it at end of scope 7.
 
Then scope 7 declares name "b", and there is already name
"b" from scope 5 that it hides. You can modify name "b"
in lookup table and memorize to restore the scope 5 "b"
to power at end of scope 7.
 
> assembler for a Broadcom GPU. So basically it is about
> labels, constants, user defined functions and macros.
> Nothing like namespaces or ADL.
 
If the nesting of scopes is straight (unlike with
classes, namespaces and ADL) then it must be is
even more simple. I see no reason why you should
have so complex, multi-layer lookup table on that case.
 
For assembler it likely does not matter. Assembler
presumably does not preprocess into million line monsters
like C++. However ... simplicity is also quality on its
own.
 
> dictionaries at the end of a scope block. The price is
> N lookups per global symbol in a scope with depth N.
> Not optimal, but on nowadays hardware no big deal.
 
Why you should do several lookups? Is there some
diagnostic about defects that you want to improve
somehow with it?
 
> From my point of a data structure that uses the symbol
> name as first key better fits my needs. Therefore the
> question. At least I can learn more.
 
It may indeed be that I do not know of some details of
your assembler. With what cases such large container is
most convenient and/or most efficient?
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: