/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?

Old thread:

Other urls found in this thread:

igoro.com/archive/gallery-of-processor-cache-effects/
microhowto.info/howto/capture_the_output_of_a_child_process_in_c.html
twitter.com/SFWRedditGifs

so much for muh portability

original penguin please steel

Is this true?

Nope.

why is that book backwards? the cover is on the back.
does it symbolize how backward linux and the cult of c are?

really makes you think.

...

I'd just like to interject for a moment. What you’re referring to as Penguin, is in fact, Japanese anime penguin, or as I’ve recently taken to calling it, Nippon penguino.

Japanese read backwards.

Enable collaboration.

"small shell scripts" is about the last place you'd want to be using C. This is stupid.

igoro.com/archive/gallery-of-processor-cache-effects/
This was a very good read. I recommend it to newbies.

Specially for pedantic user

can I still make money as a programmer if I live in a shitty rural area?

>it's easier to write a C compiler than a C++ one
>every language on this earth can use C libraries

>making money as a programmer

How are you gonna undercut Pajeets writing code for $2/hr?

...

>>it's easier to write a C compiler than a C++ one
That's irrelevant if you just want to use the language. It only matters if you're compiling for some extraordinarily obscure architecture.
>>every language on this earth can use C libraries
extern "C"

sure. ask local businesses to replace their shared excel and access files with a solid software.

In Ukraine 2$/hr is a decent job.

>tfw Sup Forums knows more about programming than /dpt/

>extern "C"
>have to make sure you capture every exception it might throw
>can only export valid C prototype
At that point you might as well be programming in C.

Just because the interface is C doesn't mean the implementation can't benefit from C++ features.
Even Microsofts C library is implemented in C++, lmao.

How do you expect to achieve C ABI compatibility except by exporting symbols that are C functions?
The point is that you can write whatever you like in the body of the code, but the interface remains portable.

you're clearly not webscale enough to know the future of abi compability is not with c, but with wasm.

Do you not realize that interfaces are pretty much the essence of programming styles?
Once you extern C you've committed the user of your library to either write some boilerplate to have them write something other than C in their interactions with your code. And on your library side your entire interface must boil down to C code. It's a very large constraint.

>And on your library side your entire interface must boil down to C code
Yeah and? it's just the interface, which is a tiny part of your library.
It's not hard to efficiently translate between C and C++.

dumb frogposter

>it's just the interface
That's what people who write bad interfaces often say. The interface is a major guiding factor in developing a library.

or.. you could provide a flat api. (incomplete example because its highly dependent on if you use pimpl and opaque pointers, and what the "target" language will be, if you need to extern "C" or not.

// include/foobar.h
#ifndef __cplusplus
#include "flat/foobar.h"
#endif

#ifdef __cplusplus
#include "detail/foobar.hpp"
#endif

This is a great way to make people dislike your library. People tend to assume that one of these are an afterthought. Any flaw will be attributed to that. If someone doesn't like your dialect of C++ they will assume the C++ api is secondary to the C api. And you quickly gain the reputation for only supporting C. But go ahead. It's an attempt at a solution. If people ended up leaning that way you probably didn't lose much.

I'm trying to debug my program but I get random shit at the end of it.
int main(int argc, char *argv[])
{
int a[] = {5, 2, 4, 7, 1, 3, 2, 6};
size_t isize = sizeof(a) / sizeof(a[0]);
size_t i;

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

insort(a, isize, sizeof(a[0]), icmp);

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

return 0;
}

This is the random shit.
(array)
(sorted array), -794678248, 32766, 1, 0, 4196800, 0, -308193232, 32692, 0, 0, -794678248, 32766, 0, 1, 4196464, 0, 0, 0, 1190345096, 492821133, 4195760, 0, -794678256, 32766, 0, 0, 0, 0, 66271624, -492668087, -89966200, -499777201, 0, 0, 0, 0, 0, 0, 1, 0, 4196464, 0, 4196912, 0, 0, 0, 0, 0, 4195760, 0, -794678256, 32766, 0, 0, 4195801, 0, -794678264, 32766, 28, 0, 1, 0, -794672176, 32766, 0, 0, -794672164, 32766, -794672153, 32766, -794672136, 32766, -794672114, 32766, -794672020, 32766, -794672000, 32766, -794671984, 32766, -794671961, 32766, -794671944, 32766, -794671909, 32766, -794671877, 32766, -794671859, .... etc.

I'm not sure how the code prints more than I told it to.

Maybe insort overwrites too much of the array and corrupts the stack (the stack is where isize and i are).

You don't need to flush after printf a \n
What is insort?

I don't think so since isize has the correct value after insort and i is initialized to 0.

Is this true?

Are you sure? Also, what is icmp?

Informatics

icmp is just a simple comparison function. The whole code is here. I'm still trying to debug the insort function to work correctly.
#include
#include
#include

void insort(void *base, size_t n, size_t size, int (cmp)(const void *, const void *))
{
void *tmp = malloc(size);
char *b = base;
int i, j;

for (i = 1; i < n; i++) {
memcpy(tmp, b + (i * size), size);
for (j = i - 1; j >= 0 && cmp(b + (j * size), tmp) > 0; j--)
memcpy(b + ((j + 1) * size), b + (j * size), size);
memcpy(b + (j * size), tmp, size);
}
free(tmp);
}

int icmp(const void *p1, const void *p2)
{
int v1 = *(int *) p1;
int v2 = *(int *) p2;

if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}

int main(int argc, char *argv[])
{
int a[] = {5, 2, 4, 7, 1, 3, 2, 6};
size_t isize = sizeof(a) / sizeof(a[0]);
size_t i;

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

insort(a, isize, sizeof(a[0]), icmp);

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

return 0;
}

Does anyone have experience with linux / ROS?
I am making an application that gathers all the separate parts into a single application.
I am launching the applications using the linux API similar to
microhowto.info/howto/capture_the_output_of_a_child_process_in_c.html
and then basically redirect all stdout and stderr to a string which I then can grep on to see if a program is ready or dead (aside from just see if it is running).
But ROS output is different somehow as I don't get the same output in a terminal as I get in my own application (I don't get any output).
It works fine when I take a simple program like ping, but either it is because ros spawns different programs itself or it is because ROS output is not being logged.
Anyone have an idea to where to start debugging this?

Well, when everything uses the cpp/hpp extension except the flat/*.h they'll be dumb to assume its not C++. As for C and other FFI, it is secondary :^), only a convenience for them.

But you're right, providing a FFI interface for C++ is rather annoying. All the solutions for doing a non C-ABI "object model" have turned out to be ass cancer. COM, D-bus, CORBA, KParts, SOM, XPCOM, etc.

One solution would continue to be an island (only providing a C++ interface). Another laughable one would be to require any "clients" to use the C++ name mangling conventions, C++ function call conventions and the C++ vtable (and maybe deal with multi-inheritance w.r.t to the vtable).

Or to not use "C++ as C++" but rather extern "C" everywhere.

Sh..shut your mouth. I love C.

Is C11 the best choice if you want to write a fast game 2D/3D game engine and port it to:

>Linux/Mac/Windows (already there)
>Android
>iOS
>PlayStation
>XBox
>handheld gaming consoles
>wasm

samefag

Writing a universe simulation. Currently a small intelligent form of life called the Cromuloids have sprouted in the Omega system. It's cute to see them try to walk on the land.

Shill lord, you guessed wrong.

post link to source or webm demo

Use j+1 in your last memcpy

>fatal error C1060: compiler is out of heap space
this is best part of compiling c++ with msvc
lets see your gcc run through 32GB+ compiling code :^)

...

How are you building It?

language/frameworks/libraries/work_environment/project_duration?

How come that's a screenshot of spore?

Ah, thanks that worked. Do you know why it was fucking up and adding all the extra crap?

what no!

>user accidentally recreates spore
Shame. You're now in copyright violation. Go directly to jail.

At least it isnt a loli kidnapping simulator

You can groom and kill children in spore

My latest javascript framework that nobody asked for and nobody needs. I call it C-biscuit :3

I have read some game engines source code and they all seem to reimplement their own allocators and containers at least.
Not sure if there's any point in that because they have never published benchmarks of their own implementations vs the STL containers.

get informed newfag

Can you actually make it good this time?

gnu11 or at least the parts that are also supported in clang.

Really?

No that would be fine but don't mess with EA.

I know what that is you fucking faggot, now what does that have to do with what I said?

Do you consider this me readable than this #include
#include
#include

void insort(void *base, size_t n, size_t size, int (cmp)(const void *, const void *))
{
void *tmp = malloc(size);
char *b = base;
int i, j;

for (i = size; i < n * size; i += size) {
memcpy(tmp, b + i, size);
for (j = i - size; j >= 0 && cmp(b + j, tmp) > 0; j -= size)
memcpy(b + j + size, b + j, size);
memcpy(b + j + size, tmp, size);
}
free(tmp);
}

int icmp(const void *p1, const void *p2)
{
int v1 = *(int *) p1;
int v2 = *(int *) p2;

if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}

int main(int argc, char *argv[])
{
int a[] = {5, 2, 4, 7, 1, 3, 2, 6};
size_t isize = sizeof(a) / sizeof(a[0]);
size_t i;

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

insort(a, isize, sizeof(a[0]), icmp);


for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

return 0;
}

Your comment makes you seem like a retarded kike who isn't aware of anything.

You were corrupting the value stored before the array. Probably isize.

Oh, so you're a newfag on his first road trip out of Sup Forums, you probably saw those /dpt/ posts on r/Sup Forums

Or is this more readable?
#include
#include
#include

#define pos(index, size) ((index) * (size))

void insort(void *base, size_t n, size_t size, int (cmp)(const void *, const void *))
{
void *tmp = malloc(size);
char *b = base;
int i, j;

for (i = 1; i < n; i++) {
memcpy(tmp, b + pos(i, size), size);
for (j = i - 1; j >= 0 && cmp(b + (j * size), tmp) > 0; j--)
memcpy(b + pos(j + 1, size), b + pos(j, size), size);
memcpy(b + pos(j, size), tmp, size);
}
free(tmp);
}

int icmp(const void *p1, const void *p2)
{
int v1 = *(int *) p1;
int v2 = *(int *) p2;

if (v1 < v2)
return -1;
else if (v1 == v2)
return 0;
else
return 1;
}

int main(int argc, char *argv[])
{
int a[] = {5, 2, 4, 7, 1, 3, 2, 6};
size_t isize = sizeof(a) / sizeof(a[0]);
size_t i;

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

insort(a, isize, sizeof(a[0]), icmp);

for (i = 0; i < isize; i++)
printf("%d, ", a[i]);
printf("\n");
fflush(stdout);

return 0;
}


That seems weird because I was checking isize outside the function and it was the correct size, plus wouldn't isize in the function be a copy?

I was there user, including all the times he's resurfaced to show progress.

How did you check it? Do you use gdb?

I printed it out with printf in main after the insort function.

Maybe it's am optimization and the compiler inline the value of isize. Look at the assembly code user.

I don't think clang would optimize it since I didn't explicitly specify any optimization flags. I guess I'll have to look at the assembly. Thanks.

Why do you use clang?

I like it because the error messages are better.

No comments.

C = Computer plumbing
Lisp = Computer science

Agree?

C++ = industry, money and bitches.

Agree.

Is this a bad practice?
int& returnByReference()
{
static int x = 5; // static ensures x isn't destroyed when it goes out of scope
return x;
}

int main()
{
int value = returnByReference(); // case A -- ok, treated as return by value
const int &cref = returnByValue();
}

Python and Java are probably more popular in the field of CS than either of those

ignore case A

Getting a reference to a temporary is generally a bad idea unless you're doing stuff with move semantics. But I don't know what you're doing, this is a contextless example.

Because they've finally cribbed all the features Lisp pioneered.

before gcc 6 (and 5 desu), gcc (g++ mostly) had absolutely shitty errors. here's an approx line number. then clang showed that you can also have a column, and then even point to the identifier / binding thats the actual compile error.
then clang showed that errors messages could be actually helpful.
static int foo (int a, int b) { return a + b; }
int bar (int a) { return foo (a (4 + 1) * 2); }
// gcc
foo.c:2:33: error: called object 'a' is not a function or function pointer
foo.c:2:1: error: too few arguments to function 'foo'

// clang
foo.c:2:33: error: called object type 'int' is not a function or function pointer

then with gcc 6 they copied a lot from clang. and then with gcc 7 they copied the actual informative errors.

..and then with msvc 2017 they added in the "caret" dianostics from clang 2 / gcc 5
whats wrong with that?

It seems useless. What are you trying to accomplish here?

Here's the functionof converting into base-60 in some made up BASIC dialect.
STRING FUNCTION TO60 (INT A)
INT POS, MULT, ANEW, I
STRING RESULT

POS = 0
MULT = 1
RESULT = ""

WHILE (A < POS)
POS++
MULT *= 60
END WHILE

POS--
MULT /= 60

WHILE (POS != 0)
ANEW = A / MULT
RESULT = RESULT + ANEW

FOR I = POS DOWNTO 0
RESULT = RESULT + "'"
NEXT

A -= ANEW * 60
POS--
MULT /= 60
END WHILE

RESULT = RESULT + A + "°"

END FUNCTION

>tfw to much of a brainlet too write haskell in practice

make it return "const int &" so no one tries to fuck with it. it's generally an OK practice if you deal with large structs that are too cumbersome to copy to pass them by value.

I guess it's a simplified example but I'm not too sure about what a good optimizing compiler could do here. Like storing x in a read only memory page. So you really want to prevent any changes to that. (const it)

Do a tiny module at a time, lad.

more popular. but way less fun.

>haskell
>practice
chose one m8

Python is very fun for me. Much more fun that C. Together they work quite fine.

Isn't the user passing a void to the function? Why is it being read as a ' '?

yes. work for yourself and sell your software on the internet. every retard can earn at least $2000 a month with some shitty shareware. if you're in a rural area $2k is enough to live off.

work for 3 months, then spend the rest of the year shitposting on 4chink. repeat every year. the good life.

does MSVC support C99 yet?

yes, python can be very fun. if you've got a chromosome too many.

default parameter

>resorts to name calling
Enjoy your dead useless language, I guess.