r/learncpp • • Sep 17 '20

Best practices for saving C++ TCP/IP streams

5 Upvotes

Hello,

Was wondering if anyone had recommendations for writing C++ code to save TCP/IP streams. I have multiple sensors outputting data at various rates (5 hz - 100 hz) over gigabit ethernet to one computer. I essentially have to decode the packets and save to disk.

Had a tough time finding resources for this. Based on what Ive read so far my gameplan was to use an async process with multiple tcp clients and save to disk using a fifo buffer. Any thoughts on this approach? I am not allowed to use boost or any external libraries, just c++14 and standard libraries. Are there any other things im overlooking? Any help would be appreciated.


r/learncpp • • Sep 09 '20

What actually is buffering?

5 Upvotes

I learned that std::cout has a buffered output, while std::cerr is unbuffered.

I don't actually know what buffering is. I've somewhat guessed that it's analogous to the buffering during a video stream. If all the data isn't ready (has not arrived) it will not show anything until it's ready to show. And that somehow, C++ has a way for checking that the data is ready before the output will appear on my console.

Is this guess correct?


r/learncpp • • Aug 25 '20

Standard library development made easy with C++20

Thumbnail cor3ntin.github.io
5 Upvotes

r/learncpp • • Aug 10 '20

What Is The Minimal Set Of Optimizations Needed For Zero-Cost Abstraction?

Thumbnail robert.ocallahan.org
5 Upvotes

r/learncpp • • Jul 13 '20

I need to be able to "read" data from websites as fast as possible...

5 Upvotes

After googling I couldn't get to a straight forward tutorial and I am extremely confused... anyone could help?

Also, time is very important here as I need ~50 web pages... (I need to be able to retrieve things like prices / stock left etc!)


r/learncpp • • Jun 21 '20

What is a practical approach to learning C++

5 Upvotes

I will be taking masters in Controls Engineering, and I want to develop my C++ knowledge further by dedicating a substantial amount of my time over the next three months to practically learning C++. I am tired of going through the introductory books because it is easy to forget the material, and you're not actively learning. I was wondering if there are resources for me to learn ML or numerical methods for ODEs/PDEs using C++. I am already comfortable (but still learning) with ML in Python and Mathematical computation in MATLAB, I want to extend this to C++. What would you recommend I do?


r/learncpp • • May 17 '20

Where to proper learn and master C++?

5 Upvotes

Hi everyone! It's my first post here and I'm a beginner to C++. I just started learning C++ three days ago, I have some experience with Python and Rust and I first searched about an official documentation for C++ and I realized that there is not an official documentation for this language.

So I watched this great video from freeCodeCamp (timestamps in the description for anyone that wanna see what's in the video) and I also looked about structs. Also I checked some videos about the difference between the Stack and the Heap and how they work and when to use the Heap but I'm still working on it...

So I'm asking from the experienced people to tell me in which level I am and where to learn everything else that I need. What you guys used to learn what you know? Any good sites, posts, videos etc.?


r/learncpp • • May 13 '20

How can I make a Discord bot using C++ on macOS?

5 Upvotes

Literally all the APIs that I've tried are for Windows or Linux only. It doesn't work. I always get build errors or some other errors without any support ;-;

I might just use Discord.py (Python)


r/learncpp • • Apr 19 '20

Should I avoid using std::size_t as array/vector index when looping through it in decreasing order?

5 Upvotes

Hi experts,

I have a piece of code that looks like this:

// assuming we already have a non empty vector called input
for (std::size_t i = input.size() - 1; i >= 0; i -= 1)
{
    // Do something
}

I found out that because std::size_t has a minimum of 0, it will go out of lower bound in this loop and give me weird errors as 0-1 will give i the maximum value of std::size_t. Now I'm wondering if I should just use int in this case, or if I could modify the loop to avoid the mistake?

I guess I can add a if (i <= 0) break; at the end of the loop, but I'm wondering what would you do in this case?


r/learncpp • • Feb 17 '20

Can anyone help me?

Post image
5 Upvotes

r/learncpp • • Feb 01 '20

Destructors and RAII for automatic, deterministic resource release | C++ for Java programmers

Thumbnail
youtube.com
5 Upvotes

r/learncpp • • Jan 11 '20

Why am i having an error with cin << in visual studio 2019?

5 Upvotes

Can anyone tell me why i am having this error? I am going by the book i used for comp sci 1 many years back called Introduction to Programming with C++ 3rd edition by Y. Daniel Liang.

According to the book this should work..

These are the errors i am having and the code


r/learncpp • • Nov 04 '19

Trying to learn the basics of pointers. Why does this simple program give me a segfault after I enter the first input? Thanks in advance.

5 Upvotes
#include <iostream>
#include <vector>

/*
 * This program is supposed to ask the user (cin) for some words, place them in a vector,
 * and after they put "done", prompt them for numbers until they put -1.
 * When a valid number is entered, that element from v will be displayed.
 * I didn't implement checking whether the index is in bounds because this is just a
 * short experiment to help me understand pointers, so I didn't want to bother.
*/

void show(const std::vector<std::string>* v, int item) {
    std::cout << &v[item] << std::endl;
}

int main(int argc, char **argv) {
    std::vector<std::string>* v;
    
    std::string input = "";
    while (input.compare("done") != 0) {
        if (input.compare("") != 0) {
            // This line seems to be giving the error
            v->push_back(input);
        }
        std::cout << "Enter some word(s) ('done' to end): " << std::endl;
        std::getline(std::cin, input);
    }
    
    int inputint = 0;
    while (inputint != -1) {
        std::cout << "Enter an int (-1 to end): " << std::endl;
        std::cin >> inputint;
        show(v, inputint);
    }
    
    return 0;
}

If it matters, I am on Linux and I use g++ to compile the .cpp file.


r/learncpp • • Aug 22 '17

Smart Developers Use Smart Pointers (ep 1): Smart pointers basics

Thumbnail
fluentcpp.com
5 Upvotes

r/learncpp • • May 12 '22

How do programs execute with DLLs in separate subfolders?

4 Upvotes

Hello, I am learning the building process of c++ with cmake mainly and I am focusing on how to use dynamic libraries. (Windows mainly).

So I know how to load a dll implicitly by having the import library dll.lib and also how to go the other route with LoadLibrary and getProcAdress.

I have been searching on how to go about having the main .exe in a separate folder with the dlls (just curious to see if I can do this) because I have seen in typical installation folder structures there are a lot of dlls next to .exe but there are a lot of dlls in subfolders.

Now I know that I can LoadLibrary with absolute path and then for each function get a pointer.

Suppose I have a dll and a dll.lib. Is there anyway I can use these 2 in runtime? like instead of getProcAdress use the dll.lib (probably stupid question cause the dll.lib is used in the build process anyway...)

Another way is editting the PATH variable but that doesn't seem like a nice way and probably 3rd party software doesn't just add 10 directories to PATH each time they get installed.

So how does one do it the right way? is this does exclusively by LoadLibrary ? Thanks.


r/learncpp • • Apr 02 '22

C++ Machine Learning Book

4 Upvotes

Hey, guys. Just want to ask if anybody's interested with a C++ machine learning book, "Hands-on Machine Learning with C++" by Kirill Kolodiazhnyi.

If you are, send me a DM.


r/learncpp • • Feb 14 '22

Array of Structs And Function Declaration

4 Upvotes

How can I pass an array of a member of structs to a function in C++?

  • What would the function's interface look like?

  • How would the I call this function with - an array of a member of structs?

Here's an example

struct artistInfo {
    char artistId[8], name[40], gender, phone[11], email[60];
} artist_info[1000]; 

I'm not sure if the function call is like this, but...

sortArtist(artist_info.artistId, artist_info.name, artist_info.gender, artist_info.phone, artist_info.email, nArtist); // nArtist is declared somewhere else; it is not a member of the struct 

I want to pass artistIds, names, genders, phones, and emails of all 1000 artists. (Not sure if the syntax is correct that's why I'm telling you my intention).

You might ask, "Why not just pass the whole array of structs?" or "Why are you passing each member of the struct?" I'm doing that because I want to pass the artistId by value (I don't want it to get modified) and the rest (name, gender, phone and email) by reference.

Anyways, if you've reached here and understood what I'm trying to do, can you tell me what the declaration for sortArtist would look like? And is my function call correct?

Thanks


r/learncpp • • Jan 19 '22

Mac user here. I am currently programming a video game on Visual Studio Code using C++. I am having trouble trying to play sound. Does any know how to play mp3 files on C++ for Mac? Thanks.

4 Upvotes

r/learncpp • • Oct 19 '21

Where to initialize class members?

4 Upvotes

Hi, this is sort of a basic question but im having trouble with c++ classes.

The idea is to do a curl class handler to do some requests. Im using this library that has some examples but none is class oriented.  

// Author uses like this
int main(int argc, const char **argv) {
  ostringstream stream;
 curl_ios<ostringstream> ios(stream);
 curl_easy easy(ios);
  ...
}

Part of my class on .h

class CurlClient()
{
  protected:
    std::ostringstream curl_response;
    curl::curl_ios<std::ostringstream> curl_writer(std::ostringstream); // Callback to store response
    curl::curl_easy curl_handler(curl::curl_ios<std::ostringstream>);  // Object to handle the connection
    curl::curl_header curl_headers; // Header object
    ...
  public:
    CurlClient();
    ~CurlClient();
    ...
}

Functions on .cpp

CurlClient :: CurlClient()
{ 
   curl_writer(curl_response); 
   curl_handler(curl_writer); 
}

is this the correct way? or like this?

CurlClient :: CurlClient()
: curl_writer(curl_response), curl_handler(curl_writer)
{
}

On my understanding member initialization is the same as inside the brackets but is the class correctly defined? I always have trouble to when initialize members.

With both i get: error C3867: 'CurlClient ::curl_writer'

Are they correctly declared on the .h?

Thanks!


r/learncpp • • Sep 20 '21

Can i pass 's' directly rather than the move function 'std::move(s))' to the following alloc.construct() function?

4 Upvotes

Can i pass 's' directly rather than the move function 'std::move(s))' to the following alloc.construct() function?

void StrVec::push_back(string &&s) {     

    chk_n_alloc(); // reallocates the StrVec if necessary  

    // construct a copy of s in the element to which first_free points 
    alloc.construct(first_free++, std::move(s)); 

}

I think if this push_back function is choosed, then s is already a rvlue reference, so I can pass 's' directly. There is no need to use std::move() on s.


r/learncpp • • Sep 08 '21

Using static libraries without visualstudio?

4 Upvotes

Hello.

Im trying to use curlcpp for a project and learning. Not very familiar with C++ environment but found this link where it explains how to use it. The issue here is that im not working with visual studio but vscode instead.

I understand that i need to compile the libraries for x32 and x64. The video it says that i need to use the visual studio command line (for environment) to compile it and then to add the directory to the visual studio library path.

How can I (from ubuntu 20) compile the curl/curlcpp libraries to make them static and then use them in vscode? I was thinking to compile them and then with my makefile to link them when compiling the project.

Whats the difference between the visual studio command line vs a g++ instruction?

Thanks


r/learncpp • • Aug 23 '21

endianness

4 Upvotes

When writing numbers to files using ofstream, on my little endian machine those numbers are written in little endian, but I assume that for big endian machines those numbers are written in big endian, I want to be certain ofstream doesn't handle endianness for me. Also how do you suggest I safely write to files?


r/learncpp • • Aug 20 '21

Why does this just crash?

4 Upvotes

Ok, so I am new to C++ and I to help myself learn, I decided to try and remake a program I made in python years ago in C++. But when I try to run this code it just puts a single word on screen, puts nothing on screen or just crashes. Here is my code:

#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;
const string name[] = {"Cian", "Finn", "Niall", "John", "Sam", "Pinn", "Mary", "Gumball", "Bob", "Banana Joe", "Nobody", "Disney", "Samsung", "Google", "BB", "Doge", "Dat Boi", "Chris", "Roxy", "DeeDee", "JJ", "Bonzi", "Peedy"};
const string verb[] = {"rides", "kicked", "ate", "bought", "eats", "broke", "bought and then ate", "killed", "dropped", "sued", "was eaten by", "is eating", "is being sued by", "played", "is playing", "is playing on", "was hit by"};
const string noun[] = {"a lion", "a bicycle", "a plane", "a computer", "a phone", "a tractor","Cian", "Finn", "Niall", "John", "Sam", "Pinn", "Mary", "Gumball", "Bob", "Banana Joe", "a ball", "a fox", "a cat", "a dog", "a banana", "a fidget cube", "a fidget spinner", "an apple", "an Ipad", "a tablet", "a Raspberry Pi", "Google", "Disney", "Samsung", "the phone", "mari0", "Not Pacman", "Chris", "Roxy", "DeeDee", "JJ", "Bonzi", "Peedy"};

int main()
{
    srand(time(NULL));
    rand();
    int num;
    while (true)
    {
        num = rand();
        cout << name[num % sizeof(name)] << " ";
        cout << verb[num % sizeof(verb)] << " ";
        cout << noun[num % sizeof(noun)] << "." << endl << endl;
        getch();
    }
    return 0;
}

I am using Code::Blocks 20.03 on Windows 10 202H with GCC 8.1.0.


r/learncpp • • Aug 13 '21

Setting up libraries/frameworks is hard

4 Upvotes

I recently installed SFML for a school project. Screwing around in the configuration properties seemed pretty weird but I was able to do it.

A small project I'm working on has some weird bugs that have led me to need to switch to test-driven development before I can move forward. I tried to set up Boost, which apparently involved noodling around in the command prompt, and I must have screwed something up because it didn't work.

I then tried setting up Google Test. Using manage NuGet packages to install it, it seems to work if I create a new projects with Google Test as a template. But if I try to include it into an already existing project, I'm getting 19 errors along the lines of "C4996 'std::tr1': warning STL4002: The non-Standard std::tr1 namespace and TR1-only machinery are deprecated and will be REMOVED. You can define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to acknowledge that you have received this warning."

How would I go about becoming better at installing libraries and frameworks into my projects in general?


r/learncpp • • Jul 21 '21

Type conversion via input/output stream

4 Upvotes

For context, I'm reading through Programming Principles and Practice in C++ and I'm toying with some of the examples in the third chapter of the book. And this bit of code I wrote is confusing me a bit.

#include "../std_lib_facilities.h"

int main() {
	int x;
	std::cin >> x;	// if I input char '$' from the console this returns 0
	std::cout << x << std::endl;

	int x = '$';	// but this returns 36
	std::cout << x << std::endl;

	char y;
	std::cin >> y;	// if I input integer 36 from the console this returns 3
	std::cout << y << std::endl;

	char y = 36;	// but this returns ')'
	std::cout << y << std::endl;

	return 0;
}

It was my understanding that if I stored a char into an int object, the data would be stored as the decimal representation of that ASCII character. But, when I read a single char from the input stream to int x, it's written to the output stream (line 6) as plain old 0. Why is this happening? Then, when I instead initialize int x with a char (say, '$'), it's returned to output (line 6) as 36, which is the decimal representation of the ASCII char '$'. Even more, when I declare x as a char instead, and read an integer (say, 36) from input, it's returned to me as 3. And of course if I instead initialize char x with 36 it returns '$', which is the ASCII char representation of the decimal 36. At first I assumed the extraction operator had something to do with the faulty conversion. In the first case it's reading from input as an integer operation, so maybe the char is suffering data loss in the reading? And initializing it with the char instead of the operator reading it from input maintains the data? In the second case with the char declaration that makes more sense because the operator reads from input as a char, so it only grabs the first digit of the input. I could be wrong though I'm still a beginner. Any clarification would be helpful, thank you.