Just out of curiosity I was wondering how library developers created their own string classes. I am sort of a novice with programming but I tried creating my own string class to prove that I could do it. What I wasn't able to solve was how to create an array that contained the character sequence. It should obviously be able to change its length. When I passed a reference to the array for the character sequence, I found out the way C++ passes arrays around (via a pointer) makes finding the end of the array impossible!

So my question is how do developers make these string classes? Do they use C++ or some other language, possibly assembly and plug it into C++ by using includes?

A related question I have is how do developers interface with different hardware right out of the box? I mean how do they make their programs use a NIC and send stuff over a network without using a previously written networking library? Do they use some operating system functions to do this? I've taken a few Cisco networking classes so I understand the networking stack...

I found out the way C++ passes arrays around (via a pointer) makes finding the end of the array impossible!

That's true, but you can keep track of how large the array is. That's the usual method.

So my question is how do developers make these string classes?

Typically, an implementation of the standard string class would have three data members:

class string {
  private:
    char* ptr;
    std::size_t size;
    std::size_t capacity;
  public:
    // ...
};

The pointer points to the start of the array of characters. The size keeps track of how many valid characters there are in the string. And the capacity keeps tracks of how many characters the array can hold (i.e., size of the array). Whenever you make an addition to the string and the capacity is insufficient to accomodate the new string, it would just allocate another array that is big enough (with some added margin) to hold the new string, copy all the characters to that new location, and delete the old array. Pretty much everything else is straight-forward from there.

Typically, a real string class implementation, such as that which comes with your compiler, will be much more complicated than that, because of performance optimizations and things like that. But the point is, you can implement a string class quite easily with the above data members.

Do they use C++ or some other language, possibly assembly and plug it into C++ by using includes?

For the most part, the standard C++ library is entirely written in C++, or at least, it can be. You will typically find some C code in there for the legacy C libraries included in the C++ standard library, and some other C++ library components might call some underlying C code. But usually, the whole thing is compiled with a C++ compiler anyways. The other thing you might find is inline assembly code, which might be used in performance-sensitive places.

how do developers interface with different hardware right out of the box?

Depends what you mean here. C++, like many other languages, is a natively compiled language, meaning that compiling a C++ program produces native machine instructions for the target instruction set (e.g., x86, x86-64, etc.). This code can run on a processor that understands that instruction set. If you want your program to run on one particular processor hardware, you have to compile your code for that instruction set. That's how it works for "any hardware".

If you are talking about peripheral hardware, then that usually goes through the operating system's API. Operating systems generally expose a set of functions that you can call from system libraries (.dll (Windows) or .so (Unix/Linux/Mac)). That set of function is called the API (Application Programming Interface) and generally provides functions for file I/O, network I/O, graphics, threads and synchronization, timers, etc... The two main APIs are POSIX (for all Unix-like operating systems, such as Unix, Solaris, Linux, BSD, QNX and Mac OSX) and Win32 API (for Windows). Operating systems are usually written in a mix of C and assembly code, and the APIs that they expose are just normal C libraries, which can be called directly in C++.

In general, if you run code within an operating system, there is no way to access hardware directly in the way that the OS kernel does. That's because the OS has protections against this. And in that sense, you could not interface that directly with hardware. However, you can write an operating system in C++ (and I think there have been some experimental operating systems written in C++), the only reason most OSes don't use C++ is because they are already mostly written in C, and the experts that know how to write kernel code are more comfortable in C.

how do they make their programs use a NIC and send stuff over a network without using a previously written networking library? Do they use some operating system functions to do this?

You use functions provided by the OS's API, such as the POSIX connect() function or the Win32 API connect() function, which, coincidently, are the same (I guess Microsoft devs figured they had no reason to change this one). But often, people will use "higher-level" libraries, such as Boost.ASIO, for this kind of stuff, because they are nicer to use and work on all platforms (under the hood, they still just call the appropriate API functions).

commented: Good answer Mike. +12

Wow! That is really informative-and-helpful! Thank you!! I feel smart because my guess about the NIC interface through the OS was correct. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.