/dpt/ - Daily Programming Thread

Old thread Recursive specialized templated compile time fizz buzz edition.
What are you working on?

Attached: Screenshot_20181103-142819__01.jpg (1064x1455, 178K)

Other urls found in this thread:

en.wikipedia.org/wiki/C18_(C_standard_revision)
youtu.be/_8-ht2AKyH4
en.wikipedia.org/wiki/Model–view–controller
en.wikipedia.org/wiki/SVG_animation
pastebin.com/PiusFmgY
osf.io/xt4j8/
twitter.com/AnonBabble

can't spell "in GoD we tRust" without D Go Rust

This is pretty good 2bh

>Jow Forums can't understand the difference between
i++;
i += 1
i = i + 1

You guys should know this

the first one increments after the current value is used in the expression

Correct, but not the answer I was looking for. Imagines these as statements. Meaning as you already have the full context of the statements

so C++ is basically the same as C or what

I feel like I'm doing training programming. Implementing so and so algorithm on so and so data structure. I feel like I'm unable to do any of these though. How do I learn how to do this type of stuff

Attached: 1528191320957.png (3840x2160, 1.61M)

last two lines are missing the semicolon so the beginning of the third line is a syntax error.

Will C ever make a comeback in the software industry?

Attached: 512px-The_C_Programming_Language_logo.svg.png (512x544, 31K)

Of course not. It's shit and they're barely even trying to update it any more. C11 was 12 years after the last update and added like 5 features.

>added like 5 features
C11 was literally the biggest update in C history

I don't understand what you want to hear.

>Meaning as you already have the full context of the statements
So they're not overloaded operators in C++? Then they're all equivalent.

I suspect you're thinking of C++11. Both C99 and C89 were bigger updates to C. Also, apparently C18 is out now so we can judge their last 7 years of work: en.wikipedia.org/wiki/C18_(C_standard_revision)

Learning Apache Spark

i++ is used in operation and allows the incrementation of the variable after the current value of the variable is used and the line is processed and all previous values have been used, equivalently ++i modifies the current value of i and uses it in the operation.

i += 1 is shorthand for i = i + 1 but obliges the addition of i to anything after the equal sign, while simple i = allows for the dismissal of currently recoded value. All of these equate to a change of pointer.

This is pretty retarded as an explanation but you asked a pretty retarded question.

Don't mind me, just passing my from Jow Forums

> C18 addressed defects in C11 without introducing new language features
C is dead. DEAD.

C has been perfected

To be fair, the memory model was a HUGE defect and even just fixing that is a significant improvement.

Dead as COBOL

>The __STDC_VERSION__ macro is increased to the value 201710L.
Did nothing and still missed their schedule. I hope someone got good at foosball at least.

pick ones that interests you that are in the sameish category and do research and it should be simple.

I've personally done some of the networking based ones and web based ones on that list.

>memory model
What's the problem?

How come nobody told me gdb was so easy

All return prvalues

who told you it was hard? I still find lldb comfier now that I've gotten a handle on the syntax; the commands are a lot more organized than in gdb.

guys I have this java problem and I feel absolutely retarded for not understanding it

Now, you can't add a Cat to a List

It's as strong as ever. Just because JavaScript and web cancer is prominent now doesn't mean C is going anywhere.

is this real?

Attached: longlongpoo.jpg (475x767, 48K)

Moar like this.

I define long long as just long in all my code since sizeof(long) == sizeof(long long) nowadays. This reduces code smells.

>Given a text file, choose a random word from it and output it

Is there any non painful way to do this in C?

parse all words into an array then just generate a random number in a valid range and use that as the index array?

*array index

Yeah seems good thanks. Can make it even easier by replacing spaces by newlines in bash and just line count to get the array size.

What are you using to make your cluster?

If I malloc and set in a for loop and then store those into a data structure with a longer scope than the loop, the pointers in the data structure will still point to valid memory correct?

or just use wc -w instead of wc -l
but if you know the number of spaces or newlines ahead of time, just generate your random number there and then parse through that many spaces

malloc'd memory does not have a scope (as long as you have a pointer to it)

Loop the text file, everyone the index enters a word (switching from space/tab to alphabet), increment the count by one. Classic K&R approach.

Doing it for learning, so it may be the hard way, but i have learned some valuable information. Even if I don't/never use it in the same context again.

So I have a game, and I have the game VM.

The game handles the input, "asset" management (an asset is like the sprite, animation sequence, entity name definition, tileset definition, etc.), drawing, collision, among some other things.

The game VM handles logic that is more attached to individual entities themselves (ex. goomba, koopa, mario, luigi, etc.).

This is more of a question where I want your opinion, but in your opinion is this a good way of separating the "game" from the "engine"?

I've been thinking of making a main() for the gameVM side as well, and this would allow the user to have access to some things like game state (in menu, in level, etc.), input, among some other things.

What do you think?

Oooh wait I think I get it. Strings are arrays of chars. Arrays are pointers to the first element of the array. So a string is a pointer to a char.

Sorry, haven't written in C in a long time.

Where do you guys get your legal lolicon pictures?

A singular long is the size of an address on the given machine. So 4 bytes on a 32 bit machine and 8 bytes on a 64 bit machine.

Long long is always 8 bytes, no matter what the architecture.

You'll often see long long typedef'ed to int64_t, or uint64_t for the unsigned version. (These, plus other widths, are included in stdint.h)

You only have to worry about scope when you're dealing with things on the stack. Malloc'ed memory is on the heap, which is explicitly not on the stack, so you don't have to worry about that memory becoming invalid. As the other user said, as long as your pointer to that memory still exists, you're good.

what's the difference between the stack and the heap?

Exactly, and your names array points to the first of four char pointers that are created on your stack

This is generally true but they're all officially implementation-defined. You should use the fixed-size types, int32_t, int64_t, if you want a specific size and intptr_t if you want address size.

youtu.be/_8-ht2AKyH4

Why can't I get a reference to an item in a collection in C# like I can in C++?

Attached: 1535315694015.png (741x609, 27K)

en.wikipedia.org/wiki/Model–view–controller

Why can't I look at C++ without getting cataracts like I can with C and C#

But C++ is the most aesthetic pleasing language ever made

Attached: 1524535907246.jpg (778x512, 34K)

It made a lot of optimizations and other program transformations theoretically unsound even though they obviously work and are implemented in real compilers. So it was useless.

Has anyone here ever used a union for something useful?

Converting a set of four chars coming in on a serial line that represent an int into an int

int n;
char* p = (char*)&n;

??

union{
int i;
char c[4];
}int_and_char;

int_and_char convert;

read(fd, &convert.c[0], 4);

printf("as int: %d\n", convert.i);
printf("as chars: 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", convert.c[0], convert.c[1], convert.c[2], convert.c[3],);

I don't understand this. Where do you actually convert the string into an int?

Attached: unions_lmao.png (508x121, 19K)

I wanted to say, "yeah but you're only reading in 4 bytes, how then do you know the rest of those 32 bits?", then I realized I'm an idiot.

Is here anyone who know how to work with qt creator gui?
I'm starting off and can't find answers on the internet.
So first off all when I want to create a widget I can do so in main.cpp by creating QtWidget object and adding it to main window.
I also can create a widget in design mode.
Now my problem is, when i create something in design mode, i can't find code for that i have created anywhere, only then clicking go to slot but whats not what i want.
I would like to add widget i have created in main.cpp to another widget which i created in design mode, but i can't find code where everything in design mode is created.

When you create "TheWidget" and edit it in Design view, do you create a "Qt Designer Form Class"? This option will generate a .h/.cpp/.ui file.

You can reference widgets that you've laid out in the designer by going through the "ui" member within the widget code.

In your main.cpp, you can create an instance of the widget and add it to your main window.

Please explain

I want to write a program that will generate possibly hundreds of little animations involving simple squares and text moving around and changing value.

SVG with SMIL animations sounds like the best options
en.wikipedia.org/wiki/SVG_animation

but before I dive into the rabbit hole of writing something in C++ (has to be C++) that generates SVG and SMIL compliant XML data is there any other option I should know about.

Attached: Serial.gif (520x360, 169K)

Pure C and OpenGL.
Also theres a library called Simple something something.

These animations are meant to be served on the web

When you say
int_and_char convert;
You say, hey computer, allocate memory for my int_and_char type variable convert.
Usually if it was a struct, it would allocate sizeof(int) + sizeof(char) * 4 , but since it is an onion, computer allocates only the size of biggest member of onion. Also not like struct, memory which is allocated with onion is same wherever you access char or int.

now then, read(fd, &convert.c[0], 4) says read 4 bytes from convert.c[0] location from fd file descriptor

and then you can access and printout what you have read in bytes or in whole number int.

may invoke undefined behavior

i meant to say read 4 bytes to conver.c[0] location not from sry

like what?

like how?

trap representations

Spring + Thymeleaf is a pain. I managed to make a simple database with add+delete buttons but can't figure out how to get an edit functionality in. I'm probably just dumb.

Well yeah, it's not portable, but I wrote it in an embedded application for a determined set of hardware so, whatever.

Attached: 1330868766493.jpg (392x468, 142K)

stay strong buddy, programming requires persistence, nothing comes fast and easy, you just need time, and im sure you will make it, just dont give up!

Thank you! Gonna keep looking into it. The solution will probably be some variation of what I did for add/delete.

Reminder that this is only defined for char*, not the other way round or for any other type.
It should be defined, but not portable when sizeof(int) != 4. Better to use int32_t and char * instead of array.

Why are you deferencing c then taking the address instead of just passing convert.c ? read(fd, convert.c, 4); is the same thing.

I don't really know, but I almost always do it that way in similar circumstances.
Maybe to remind myself that it's an array, or because I like the way it looks, I guess.

There's probably a good chance the compiler optimizes it away, anyway.

Attached: 1338401370636.jpg (1920x1080, 495K)

double d[k * cols]
This works for k < 41 and cols = 6400, but fails for k >= 41 and cols = 6400.
double* d = new double[k * cols]
This works for both cases. I'm frankly baffled.

You're probably blowing your stack. Linux only gives you 8MB to begin with. The heap isn't so small.

Sure, but I don't know why new doesn't make the stack blow up and just allocating an array of that size does. I always thought new works on the same underlying mechanism, it just hides it for you.

No. new uses dynamic allocation (probably malloc under the hood, but not guaranteed to be).

I meant malloc, yeah. I figured that the compiler made d[whatever] into d = malloc(...) though.

t. dummy

Of which nearly nothing actually got implemented.

No. Declaring an array (or any variable) directly creates automatic storage, calling malloc creates dynamic storage. You may also find these regions named stack and heap.

Pretty much sums up Jow Forums understanding on C. If true Jow Forumsentoomen weren't shut in socially inept autists, they would flood GH PR's after reading the first chapter of

What if the file is 35Mb in size, but target platform only has 2Mb of RAM?

Parse the file once to get the number of words N
then pick a random value i in [1, N] and parse the file again to retrieve the word
In the worst case this algorithm takes twice as much time but time complexity is still linear while space complexity is constant

WHAT THE FUCK

Attached: intuition.png (401x140, 165K)

I want to calculate the weighted proportion of GSS survey data in python. I use this code for the calculation which seems kind of trivial:

np.sum(grp.BGW*grp.WTSSALL)/np.sum(grp.WTSSALL)


complete code: pastebin.com/PiusFmgY

BGW is boolean and WTSSALL are the weights. For some reason the derived percentages deviate significantly (22.9% vs. 23.1 and 5.6% vs. 7.6%) from the results some sociologist reports even thought I use the same filtering (year,race and remove missing data) or the GSS data. My code reports that Wtf is going on? Maybe someone who is familiar with STATA can help me out:

osf.io/xt4j8/

>literally too stupid for java

yeah I am the stupid one if it will not let me put a String inside an array of strings

Maybe:
Collection c = new ArrayList();

Really don't know because it's Java but it's similar to C#.

Because you said it isn't an array of strings. Or at least, it's static type cannot prove that it is.
Collection is the supertype of all Collection types. It may be a Collection but it may also be a Collection or Collection. It isn't typesafe to add an object of any type to that, the only thing that you can really do is extract objects from the collection. And the only thing you can say about those objects is that they are Objects.

>what is upcasting
back to school pajeet

that's not how it works

create an actual string, like "I'm retarded", and then you can add it to the list. It's a list of strings, not a list of string declarations.

List l = new ArrayList();