Skip to content

Instantly share code, notes, and snippets.

View Aposhian's full-sized avatar

Adam Aposhian Aposhian

View GitHub Profile
@Aposhian
Aposhian / main.cpp
Created February 5, 2023 01:44
transform dissimilar containers
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
struct Data
{
Data(int d)
: data(d) {}
int data;
@Aposhian
Aposhian / main.cpp
Last active January 19, 2023 18:08
Moving local into shared_ptr
#include <memory>
#include <iostream>
#include <vector>
struct Obj {
std::vector<int> data;
};
int main() {
std::shared_ptr<Obj> local_shared;
@Aposhian
Aposhian / main.cpp
Created January 11, 2023 20:52
Streaming uint8_t pitfall
#include <map>
#include <string>
#include <iostream>
#include <cstdint>
int main() {
std::map<std::string, uint8_t> map{{
{"zero", 0},
{"sixty-five", 65}
}};
@Aposhian
Aposhian / main.cpp
Created December 30, 2022 22:44
demo how std::atomic can fix race conditions
#include <atomic>
#include <thread>
#include <iostream>
#include <string>
static constexpr int ITERATIONS = 1000000;
int main() {
// std::atomic<long long> num{0};
long long num{0};
@Aposhian
Aposhian / build.sh
Created November 7, 2022 23:35
LLB Build
#!/usr/bin/env
if [ -z "$(docker ps --filter=name=buildkitd -q)" ];
then
docker run -d --name buildkitd --privileged moby/buildkit:latest
fi
export BUILDKIT_HOST=docker-container://buildkitd
go run main.go | buildctl build
@Aposhian
Aposhian / Dockerfile
Last active June 24, 2022 18:58
Reproduction of FastDDS failing to activate Nav2
# syntax=docker/dockerfile:1.4
ARG BASE_IMAGE
FROM $BASE_IMAGE as base
RUN --mount=type=cache,target=/var/cache/apt,sharing=private \
--mount=type=cache,target=/var/lib/apt,sharing=private \
apt-get update && apt-get install -y \
ros-$ROS_DISTRO-demo-nodes-cpp \
ros-$ROS_DISTRO-nav2-bringup \
@Aposhian
Aposhian / Dockerfile
Created June 2, 2022 19:25
Run as user inside of Dockerfile
FROM debian
ARG UID
ARG GID
RUN groupadd -g ${GID} me
RUN useradd -g ${GID} -u ${UID} me
USER ${UID}:${GID}
@Aposhian
Aposhian / double-free-shared-from-this.cpp
Created May 17, 2022 19:58
double free from shared_from_this
#include <memory>
class Outside;
class Inside {
public:
Inside(std::shared_ptr<Outside> outside): outside_(outside) {}
private:
std::shared_ptr<Outside> outside_;
@Aposhian
Aposhian / double-free.cpp
Created May 17, 2022 19:50
double free on creating shared_ptr for member
#include <memory>
class Outside;
class Inside {
public:
Inside(std::shared_ptr<Outside> outside): outside_(outside) {}
std::shared_ptr<Outside> outside_;
};
@Aposhian
Aposhian / capture-this.cpp
Created May 9, 2022 16:08
Implicit capture of class members when `this` is captured
#include <iostream>
class MyClass {
public:
void do_stuff() {
auto fn = [this]() {
do_more_stuff();
};
fn();
}