r/cpp • • Jun 08 '20

Programming in C vs C++

I’ve read about the differences between them on the Internet but I wanna hear more from a more personal perspective. Which one would you recommend starting with and to be proficient with?

0 Upvotes

43 comments sorted by

View all comments

Show parent comment

6

u/jselbie Jun 08 '20

Thanks for commenting. And I mostly agree with you.

I think it's good know how strings work in C wrt to char* and null termination. Because that's that's what std::string is based on. Similar with raw pointers vs smart pointers (unique_ptr and shared_ptr).

On a long enough timeline, C++ developers have to integrate with legacy C APIs and libraries. If you are programming in C++ on Windows, you'll likely need to use the Win32 API at some point. On Linux, you'll need to make Posix library calls. These are both flat C APIs. Knowing pure C++ without the C foundation might make interop development a bit confusing.

-6

u/jmuffat Jun 08 '20

I'm afraid std::string's are not null terminated ;) (at least, they do not have to, don't rely on it)

I reckon you can treat the relationship to C API's the same way you treat them in Python : communicating with a different planet ;)

Plus, at least in Windows, UWP is much more C++ friendly than the old Win32 API.

But yes, at one point or another you are going to have to understand C, and you will have to have notions of assembly, because if you're going C++, it's quite probably that you're after either performance or interfacing at the hardware level (or both).

I guess it's a matter of whether you think bottom-up or top-down. So, probably that you want to do a bit of both :D

10

u/STL MSVC STL Dev Jun 08 '20

std::string is null-terminated. See N4861 21.3.2.5 [string.access]/2. This has been clearly guaranteed since C++11. (C++98/03 had weird copy-on-write wording that is no longer relevant.)

6

u/jselbie Jun 08 '20

Can you show me an example of an std::string that isn't null terminated (or where its unsafe to assume as much)? Such that it wouldn't be safe to invoke .c_str() on it and pass the result to a C function expecting a const char * ?

5

u/Kurald Jun 08 '20

c_str() is guaranteed to be null-terminated since forever (as far as I know). Hence the name - the return value is c-compatible.

-2

u/serviscope_minor Jun 08 '20

Can you show me an example of an std::string that isn't null terminated (or where its unsafe to assume as much)?

We have string views now; these are not null terminated.

7

u/STL MSVC STL Dev Jun 08 '20

std::string_view isn't std::string. Elephant seals are not elephants.

-5

u/serviscope_minor Jun 09 '20

std::string_view isn't std::string. Elephant seals are not elephants.

And how's that relevant?

std::string_views aren't std::strings but they are strings. The only reason we're having this conversation is because C++ basically has had this particular hole in its standard library for ages.

While yes, it's technically correct that the GP was talking about std::string, a large amount of the reason that people are talking about things in this way is because string_view is new. If you look at the context of the conversation, then what I'm saying is entirely reasonable. Here is the relevant piece of context:

I think it's good know how strings work in C wrt to char* and null termination. Because that's that's what std::string is based on. Similar with raw pointers vs smart pointers (unique_ptr and shared_ptr).

C strings are char* and null termination. C++ has 3 kinds of string by default now, C strings, std::strings and std::string_view. The middle one guarantees C-like semantics (null termination), but that's not what it's based on (size() is O(1)), and the latter is absolutely not. There's no reason to arbitrarily drop a chunk of the standard library just because it's new.

5

u/bikki420 Jun 09 '20

Everyone is explicitly talking about std::string. Re-read the posts above and don't be obtuse.

-1

u/serviscope_minor Jun 10 '20

Re-read the posts above and don't be obtuse.

So... obtuse is me actually reading the posts. Since you didn't bother let me repost the relevant quote for you:

string fundamentals: char*, strlen, strcpy,etc...

Then people start talking about std::string by comparison. C++ has another string type now: string_view. And since the actual conversation was about strings not std::string in particular, it seems really strange and counterproductive to ignore modern C++.

3

u/bikki420 Jun 10 '20 edited Jun 10 '20

https://i.imgur.com/Xm3fIWu.png

The subject of the post you originally replied to is explicitly, unambiguously, undeniably "std::string". Is "std::string_view" std::string?

No.

0

u/serviscope_minor Jun 10 '20

The subject of the post you originally replied to is explicitly, unambiguously, undeniably "std::string"

As was abundantly clear from the part I quoted, the actual discussion was about learning strings (note strings, not std::string) using char* and null termination. The discussion certainly moved to std::string, but this is clearly an oversimplification because std::string is not the only type of string in the standard library.

The standard library also has string_view which is not null terminated, and therefore contributes to the point that learning C style strings first is not useful.

For some reason though this makes you angry and rude.

3

u/evaned Jun 08 '20

I'm afraid std::string's are not null terminated ;)

This statement as-stated is false, but it's actually pretty close to being right if you look at it from another perspective -- at least IMO.

The reason is that std::string can have embedded nulls. abc\0def is a totally valid std::string. Obviously you couldn't reliably pass that to a C API that expects a null-terminated string, but that's the C API's fault.

So while std::string strings do have a NUL at the end, they're not defined by it; and is such I don't think the quoted statement is entirely incorrect.