Meriones

u/Meriones



r/C_Programming•

I'm writing some 'generic' code for a dynamic array. I have a buffer declared as void * and the user passes in the size of the type to each function. The problem is in order to index the buffer I have to write out (char *)thing->buf + size * index each time. I had the idea of setting a pointer to a VLA char (*buf)[size] = thing->buf; at the top of each function so I can just do buf + index and the compiler should do the math for me. Is this going to blow up on me?

22

r/rust•

I want to write a trait that allows iteration over the type. If we had associated type constructors this would be easy but since we don't I tried to do this

trait Iterable
    where for<'a> &'a Self: IntoIterator 
{}

Unfortunately this fails as soon as I try to use T: Iterable as a bound (playground link).

Is this intended behaviour and is there a better way to express this?

61

r/C_Programming•

To be clear, I understand the uses of function pointers. But as far as I know, there is no way to use actual function types in the language, so why bother with pointers? We already have syntax sugar so instead of writing

int (*fptr)(int) = &some_func;
int x = (*fptr)(5);

we write

int (*fptr)(int) = some_func;
int x = fptr(5);

Why not just use the function type directly?

int fptr(int) = some_func;
int x = fptr(5);
1615

r/CS_Questions•

I'm doing the Google foobar challenges and I am stuck on the first part of level 4. The challenge is posted here. Some googling revealed this is called the maximum flow problem and I tried to implement the Edmonds-Karp algorithm. I tried my solution (here) on a bunch of test cases I found online and it worked for all of them but it is failing 2 of the google test cases. Since we don't have access to the test cases I'm not really sure where to go from here.

I guess I am mostly looking for idea

10

r/techsupport•

Hey, I have been having some connection issues in games recently. I get a regular loss of connection for about 2 seconds. When I try ping google.com -t in command prompt I consistently get 18 successful pings and then a timeout which coincides with a connection drop in my game and then a "device reconnected" popup on my laptop.

My PC is connected via ethernet to my laptop which has wifi. Weirdly, my laptop has none of these issues, so presumably the problem is not with the router.

I have tried restarting both machines and the router.

PC is using Windows 10, laptop is running Xubuntu 14.04

14

r/C_Programming•

I am writing a doubly linked list struct which looks like this:

struct Node {
  Node *prev, *next;
  Value value;
};

struct List {
  Node *head;
};

The list will be circular and head will always be valid even when the list is empty. Since head will never have a value I want to save space by doing something like

struct NodeBase {
  Node *prev, *next;
};

struct List {
  NodeBase *head;
};

AFAIK it is not necessarily safe to then convert a Node* to a NodeBase* because prev might have different alignment. I think this should work:

struct Node {
  NodeBase *links;
  Value *value;
}

But I was wondering if there w

34

r/DotA2•

Probably a bug, but currently couriers are able to use items. Using soul ring on the courier allows it to use items with a mana cost. Using midas with the courier gives gold but not xp.

21

r/algorithms•

I am trying to find some resources on the algorithm mentioned here. In particular

we use an algorithm from robotics that's often called "wall-tracing."

I'm looking for an algorithm that will take a basic route from the A* algorithm and adjust it to make it higher 'resolution' as shown here (white = A* route, red = adjusted route, blue = obstacles)

53

r/cpp_questions•

I have a class containing an array which I want to initialize with a variadic constructor. My code can be found here. As far as I can tell this is technically correct using brace elision but I get an annoying 'suggestion' because some braces are missing. Is there an easy fix other than just compiling with -Wno-missing-braces?

22

r/cpp_questions•

I am writing a header to handle units of length, mass and time and derived units of these (e.g. feet per year). My code so far compiles fine on g++ but using clang++-3.8 i get this error

clang++-3.8 -stdlib=libstdc++ --std=c++1y -Wall -Wextra -Wshadow -pedantic -Werror -g test/test.cpp -I include  -o bin/test
/tmp/test-8620db.o: In function `Unit<&units::none_c, &units::none_c, &units::none_c>':
/home/andrew/Documents/units/include/units.h:39: undefined reference to `units::Unit<1, 0, -1, &units::none_c, &units::none_c, &units::none_c>::scale'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Below is the

15

r/cpp_questions•

Here is a minimum non-working example of my problem:

struct Base {
	Base* data;
};

struct Derived : virtual public Base {};

int main() {
	Base x;
	x.data = new Derived;
	delete x.data;
}

This code fails for me with

*** Error in `./bin/test': free(): invalid pointer: 0x00000000017ea018 ***
Aborted (core dumped)

The key seems to be that Derived is a virtual subclass but I was under the impression this only made a difference to subclasses of Derived. Why is this happening and how can I work around it?

24

r/cpp_questions•

I don't understand why this code behaves the way it does. I have a constant pointer included in two files which takes different values in each.

header.h

void file1();

int* const global_ptr = new int;

file1.cpp

#include "header.h"

#include <cstdio>

void file1() {
	printf("File 1 ptr: %p\n", global_ptr);
}

file2.cpp

#include "header.h"

#include <cstdio>

void file2() {
	printf("File 2 ptr: %p\n", global_ptr);
}

int main() {
	file1();
	file2();
}

I know the two files have their own copies of global_ptr since it is const but when I run the code you can see they point to different locations:

File 1 ptr: 0x2503030
File 2 ptr: 0
211

r/cpp_questions•

I feel like I am missing something really basic here:

#include <array>

class Unit {
	public:
		Unit(int x) {posx = x;}

		int posx;
};

class Container {
	public:
		Container() {
			units[0] = Unit(1);         // Doesn't work
			units = {Unit(1), Unit(2)}; // Also doesn't work
		}
		std::array<Unit, 2> units;
};



How can I initialize the array?

[EDIT] The error I get is

test/test.cpp: In constructor ‘Container::Container()’:
test/test.cpp:12:15: error: use of deleted function ‘std::array<Unit, 2ul>::array()’
   Container() {
               ^
In file include
15