Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


So who knows about C++ ?

Reply to topic
Bike Chat Forums Index -> The Geek Zone
 Topic moved: from Dear Auntie BCF... to The Geek Zone by G (12 Feb 2014 - 16:10)
View previous topic : View next topic  
Author Message

Lord Percy
World Chat Champion



Joined: 03 Aug 2012
Karma :

PostPosted: 23:35 - 11 Feb 2014    Post subject: So who knows about C++ ? Reply with quote

I'm pretty sure a fair few people on here know a hell of a lot about this kind of thing, so here is a homework question I simply can't do. The original problem is below.

There are 5 things wrong in this code:

Quote:
#include <string>

int main() {

std::string * strPtr = new string('hello");

std::cout << &strPtr << std::endl;

delete[] strPtr;

std::cout << *strPtr << std::endl;
}


Find the five wrong things and correct them.

I actually got it working, and now I've changed something and it isn't working any more. So I'm starting over and putting it out to BCF.

I have a reasonable grasp on most aspects that I've learned so far, but something here has thrown me and now I feel like I don't know anything at all.

If anyone is feeling kind/nerdy enough to fix this and explain each fix, that'd be great.

Well I'm fairly sure I know a couple of errors already:

- Needs #include<iostream>
- 'hello" has the wrong quotation mark at the start.
- delete[] is wrong because there is no array in there.

I think the thing that's really messing things up is my grasp of pointers and references. Am I even remotely on the right track?

Thanks all Thumbs Up
 Back to top
View user's profile Send private message You must be logged in to rate posts

SQL
World Chat Champion



Joined: 09 Aug 2012
Karma :

PostPosted: 23:52 - 11 Feb 2014    Post subject: Reply with quote

Repost it in Geek Zone you will get better replies.
 Back to top
View user's profile Send private message Visit poster's website You must be logged in to rate posts

Lord Percy
World Chat Champion



Joined: 03 Aug 2012
Karma :

PostPosted: 00:09 - 12 Feb 2014    Post subject: Reply with quote

I thought I'd get better coverage in this section Embarassed

Anyway it looks like I've done it, and I think I actually had already done it all along.

The real problem was some shitty underlying computer issue. While messing around with the code, I'd told it to draw info from an unused bit of memory which caused it to basically have a stroke and not tell me about it. No matter what I did after that, even the most basic bit of coding would crash. After restarting the computer, everything works again, woohoo!

Very Happy
 Back to top
View user's profile Send private message You must be logged in to rate posts

Rogerborg
nimbA



Joined: 26 Oct 2010
Karma :

PostPosted: 14:12 - 12 Feb 2014    Post subject: Reply with quote

5 6 fixes.

Code:

#include <string>
#include <iostream> // #1 Need iostream for cout

int main()
{
    std::string * strPtr = new std::string("hello"); // #2 Mismatched '", #3 needs another std::

    std::cout << *strPtr << std::endl; // #4 We want the object that strPtr points to, not the address of it

    delete strPtr;  // #5 Not an array, don't use array delete[]

    //std::cout << *strPtr << std::endl; // #6 Don't use the object again after deleting it
}

____________________
Biking is 1/20th as dangerous as horse riding.
GONE: HN125-8, LF-250B, GPz 305, GPZ 500S, Burgman 400 // RIDING: F650GS (800 twin), Royal Enfield Bullet Electra 500 AVL, Ninja 250R because racebike
 Back to top
View user's profile Send private message You must be logged in to rate posts

Stowelly
Traffic Copper



Joined: 25 Oct 2009
Karma :

PostPosted: 17:18 - 12 Feb 2014    Post subject: Reply with quote

Rogerborg wrote:
5 6 7 fixes.

Code:

#include <string>
#include <iostream> // #1 Need iostream for cout

int main()
{
    std::string * strPtr = new std::string("hello"); // #2 Mismatched '", #3 needs another std::

    std::cout << *strPtr << std::endl; // #4 We want the object that strPtr points to, not the address of it

    delete strPtr;  // #5 Not an array, don't use array delete[]

    //std::cout << *strPtr << std::endl; // #6 Don't use the object again after deleting it

    return 0;  //#7 int return type is expected 0 is success
}
 Back to top
View user's profile Send private message You must be logged in to rate posts

Lord Percy
World Chat Champion



Joined: 03 Aug 2012
Karma :

PostPosted: 19:23 - 12 Feb 2014    Post subject: Reply with quote

Rogerborg wrote:
5 6 fixes.

Code:

#include <string>
#include <iostream> // #1 Need iostream for cout

int main()
{
    std::string * strPtr = new std::string("hello"); // #2 Mismatched '", #3 needs another std::

    std::cout << *strPtr << std::endl; // #4 We want the object that strPtr points to, not the address of it

    delete strPtr;  // #5 Not an array, don't use array delete[]

    //std::cout << *strPtr << std::endl; // #6 Don't use the object again after deleting it
}


Great stuff, I got all those Thumbs Up. And yeah I saw 6 too.

Well, I kind of saw 6. Point #3 was a thing I didn't quite understand, I fixed it in a roundabout kind of way by adding 'using namespace std' and just deleting all the std bits, but I hadn't spotted the actual root of the error.

All makes sense now Karma
 Back to top
View user's profile Send private message You must be logged in to rate posts

barrkel
World Chat Champion



Joined: 30 Jul 2012
Karma :

PostPosted: 21:07 - 12 Feb 2014    Post subject: Reply with quote

Stowelly wrote:
Code:

    return 0;  //#7 int return type is expected 0 is success

Technically, main returns 0 if there is no explicit return.

ISO/IEC 14882:2003 § 3.6.1 paragraph 5:
Quote:
A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
Code:
return 0;

____________________
Bikes: S1000R, SH350; Exes: Vity 125, PS125, YBR125, ER6f, VFR800, Brutale 920, CB600F, SH300x4
Best road ever ridden: www.youtube.com/watch?v=s2MhNxUEYtQ
 Back to top
View user's profile Send private message You must be logged in to rate posts

Stowelly
Traffic Copper



Joined: 25 Oct 2009
Karma :

PostPosted: 21:20 - 12 Feb 2014    Post subject: Reply with quote

barrkel wrote:
Stowelly wrote:
Code:

    return 0;  //#7 int return type is expected 0 is success

Technically, main returns 0 if there is no explicit return.

ISO/IEC 14882:2003 § 3.6.1 paragraph 5:
Quote:
A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing
Code:
return 0;


ha, makes sense, I guess its quite rare to have an entry point where there is zero chance of failure at any point though, have just become accustomed to having it return success. been programming c++ for 12 years, and still learn new things about the language every day!.
 Back to top
View user's profile Send private message You must be logged in to rate posts

gfrancisdev
Nova Slayer



Joined: 18 Oct 2012
Karma :

PostPosted: 21:57 - 12 Feb 2014    Post subject: Reply with quote

Well if you're being pedantic

Code:
int main(int argc, char** argv);

____________________
ybr125(07 silver)-killed 2013/4/3 on some ice
ybr125(06 blue), KE100(89 green-black-puke)
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

Stowelly
Traffic Copper



Joined: 25 Oct 2009
Karma :

PostPosted: 22:11 - 12 Feb 2014    Post subject: Reply with quote

gfrancisdev wrote:
Well if you're being pedantic

Code:
int main(int argc, char** argv);


as were quoting the standard now

Quote:


An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions of main: int main() and int main(int argc, char* argv[])
 Back to top
View user's profile Send private message You must be logged in to rate posts

Clanger
Stirrer



Joined: 27 May 2004
Karma :

PostPosted: 22:15 - 12 Feb 2014    Post subject: Re: So who knows about C++ ? Reply with quote

Lord Percy wrote:
Re: So who knows about C++ ?


In the world of bus passes, the C+ sign is for a disabled person to take a travel companion on the bus free of charge. As for anything else, I'm afraid I cannot help. I am only a geek in that I buy and read graphic novels / comics. Cool
____________________
Be who you are and say what you feel, because those who mind don't matter, and those who matter won't mind - Dr. Seuss
 Back to top
View user's profile Send private message You must be logged in to rate posts

Rogerborg
nimbA



Joined: 26 Oct 2010
Karma :

PostPosted: 22:20 - 12 Feb 2014    Post subject: Reply with quote

gfrancisdev wrote:
Well if you're being pedantic

https://bookriot.com/wp-content/uploads/2013/03/3879_-_animated_gif_chuck_norris_dodgeball_thumbs_up.gif

C++ is so last millennium, all the hep cats are using D now.

Not actually joking, I rather like it.
____________________
Biking is 1/20th as dangerous as horse riding.
GONE: HN125-8, LF-250B, GPz 305, GPZ 500S, Burgman 400 // RIDING: F650GS (800 twin), Royal Enfield Bullet Electra 500 AVL, Ninja 250R because racebike
 Back to top
View user's profile Send private message You must be logged in to rate posts

el_oso
World Chat Champion



Joined: 17 May 2008
Karma :

PostPosted: 13:33 - 15 Feb 2014    Post subject: Reply with quote

What kind of usage is C/C++/D seeing in industry nowadays. JIT compliers (C# mainly because everyone still uses windows) seem to be used most heavily. They no longer seem to be slow languages and often outperform languages like C++. I do still see some C++ in with C# applications, however it's usually small bits that are very maths intense.
____________________
Duke 390
Previous: '05 XR125L | '96 XJ600S Diversion |'05 Suzuki GSXR1000 | '05 Honda CBR125-R | '97 YZF 600R Thundercat | '11 Honda CBR250
Car: Jeep Wrangler 4.0L
 Back to top
View user's profile Send private message You must be logged in to rate posts

J.M.
World Chat Champion



Joined: 27 Mar 2011
Karma :

PostPosted: 17:08 - 15 Feb 2014    Post subject: Reply with quote

the_godfather wrote:
What kind of usage is C/C++/D seeing in industry nowadays. JIT compliers (C# mainly because everyone still uses windows) seem to be used most heavily. They no longer seem to be slow languages and often outperform languages like C++. I do still see some C++ in with C# applications, however it's usually small bits that are very maths intense.


The last company that I interviewed for told me that they were now straying away from C++, in favour of other languages like Java. They still use it, just not as much.
____________________
2004 R1 & 2018 XSR900
 Back to top
View user's profile Send private message You must be logged in to rate posts

Stowelly
Traffic Copper



Joined: 25 Oct 2009
Karma :

PostPosted: 19:45 - 15 Feb 2014    Post subject: Reply with quote

L
the_godfather wrote:
What kind of usage is C/C++/D seeing in industry nowadays. JIT compliers (C# mainly because everyone still uses windows) seem to be used most heavily. They no longer seem to be slow languages and often outperform languages like C++. I do still see some C++ in with C# applications, however it's usually small bits that are very maths intense.


I'm in the games industry, and can't see it changing for the triple A games any time soon. It's still king for performance and memory critical code. Plus a lot of studios have 20 year code bases that are refined and basically impossible to rewrite in anything else
 Back to top
View user's profile Send private message You must be logged in to rate posts

Dalemac
World Chat Champion



Joined: 15 Oct 2006
Karma :

PostPosted: 22:00 - 15 Feb 2014    Post subject: Reply with quote

I haven't actually ever used c++. Maybe I should give it a go one day.

The majority of the work I do is for the web so i'm usually using either C# or PHP.
____________________
YBR125 -> GPZ500S -> SL1000
 Back to top
View user's profile Send private message You must be logged in to rate posts

barrkel
World Chat Champion



Joined: 30 Jul 2012
Karma :

PostPosted: 03:27 - 16 Feb 2014    Post subject: Reply with quote

Stowelly wrote:
been programming c++ for 12 years, and still learn new things about the language every day!.

I first learned C++ in about 96, then started reading Herb Sutter's GotW, and realized that C++ was like a fractal; the more you studied it, the more flaws came out of the woodwork, and the more you had to know to write bulletproof code. The hilarious interactions that lead to e.g. std::stack<T>::pop() not being able to return the item popped, function try blocks (hahahaha!), Turing-complete templates (infinite wankery for the dedicated nerd), right up to C++11 switchable lambdas (capture by reference or value? either, you choose!). The sheer amount of knowledge you need to write exception-safe code - even with RAII to hand - is ridiculous.

Stroustrup never met a language feature he didn't covet. That leaves it up to individual people, teams, companies to choose the acceptable bits. And there are a bunch of subsets that interact sanely. Trouble is, everybody has different subsets, and library interaction suffers because the libraries aren't always lowest common denominator (at which point, they might as well be C). C++ has never had the kind of rich third-party library ecosystem its popularity would seem to merit.

You're right that it still has its niche. But Go and Rust are starting to creep into mindshare at the elite level. Personally, I hope C++ dies in a fire.
____________________
Bikes: S1000R, SH350; Exes: Vity 125, PS125, YBR125, ER6f, VFR800, Brutale 920, CB600F, SH300x4
Best road ever ridden: www.youtube.com/watch?v=s2MhNxUEYtQ
 Back to top
View user's profile Send private message You must be logged in to rate posts

Stowelly
Traffic Copper



Joined: 25 Oct 2009
Karma :

PostPosted: 10:46 - 16 Feb 2014    Post subject: Reply with quote

barrkel wrote:
Stowelly wrote:
been programming c++ for 12 years, and still learn new things about the language every day!.

I first learned C++ in about 96, then started reading Herb Sutter's GotW, and realized that C++ was like a fractal; the more you studied it, the more flaws came out of the woodwork, and the more you had to know to write bulletproof code. The hilarious interactions that lead to e.g. std::stack<T>::pop() not being able to return the item popped, function try blocks (hahahaha!), Turing-complete templates (infinite wankery for the dedicated nerd), right up to C++11 switchable lambdas (capture by reference or value? either, you choose!). The sheer amount of knowledge you need to write exception-safe code - even with RAII to hand - is ridiculous.

Stroustrup never met a language feature he didn't covet. That leaves it up to individual people, teams, companies to choose the acceptable bits. And there are a bunch of subsets that interact sanely. Trouble is, everybody has different subsets, and library interaction suffers because the libraries aren't always lowest common denominator (at which point, they might as well be C). C++ has never had the kind of rich third-party library ecosystem its popularity would seem to merit.

You're right that it still has its niche. But Go and Rust are starting to creep into mindshare at the elite level. Personally, I hope C++ dies in a fire.


those GOTW articles are quite interesting, is a hell of a lot of gotchas with the language! wouldnt quite agree with hoping it dies in a fire. it definitely has its uses (if only to keep people employed!!). but at the same time ive been doing a lot of erlang programming the last couple of years and finding it does a lot of things just so much better. but i guess its a completely different paradigm solving different problems
 Back to top
View user's profile Send private message You must be logged in to rate posts

barrkel
World Chat Champion



Joined: 30 Jul 2012
Karma :

PostPosted: 16:40 - 16 Feb 2014    Post subject: Reply with quote

Stowelly wrote:
wouldnt quite agree with hoping it dies in a fire. it definitely has its uses

It has its uses because there are no hardened, popular alternatives with wide platform support. When there are - and there will be within the next 10 years, now that LLVM has democratized access to an optimizing back end - it should take only another 10 or 20 years for C++ to die.

So I'd put a bet on C++ being in the minority for its current niche in 30 years time. I wish it could happen sooner, but it's unlikely.
____________________
Bikes: S1000R, SH350; Exes: Vity 125, PS125, YBR125, ER6f, VFR800, Brutale 920, CB600F, SH300x4
Best road ever ridden: www.youtube.com/watch?v=s2MhNxUEYtQ
 Back to top
View user's profile Send private message You must be logged in to rate posts
Old Thread Alert!

The last post was made 12 years, 229 days ago. Instead of replying here, would creating a new thread be more useful?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.21 Sec - Server Load: 0.98 - MySQL Queries: 16 - Page Size: 113.33 Kb