/dpt/ - Daily Programming Thread

Old thread: What are you working on, Sup Forums?

Other urls found in this thread:

youtube.com/watch?v=KMU0tzLwhbE
gcc.gnu.org/onlinedocs/gcc/Link-Options.html
stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
twitter.com/SFWRedditImages

First for D

>literal fag shit
Delete this thread and kill yourself.

there is NOTHING gay about a cute trap

>2017
>Falling for the C meme

anyone know a good book for someone who wants to learn C++ but already knows Java (and a few other languages). Looking at "accelerated C++" and "C++ Primer", can't decide between the two.

Not learning C++ is the best option.

Thank you for using an anime image.

>year of the lord MMXVII
>not pooing in loo

In the mood for a programming talk, any recommendations?

This. Learn Python instead

Is it autistic to zero out structs full of pointers using memset?

How else would you do it?

Probably has something to do with your nullptr clause. Why are you newing 2 extra nodes?

Can you post your class definitions file? I don't want to sound like a faggot but I think you could streamline this quite a bit.

Hello friends. I am trying to implement a Linked List in C++ by myself. Valgrind is saying that there is a memory leak within the append method. I tried to figure out what it was but I couldn't find it. Any tips?

void LinkedList::append(std::string name, std::string occupation){
Node* addNode;
if(this->headptr == nullptr){
addNode = new Node(this->headptr,this->tailptr,name,occupation);
this->headptr = new Node(nullptr,addNode);
this->tailptr = new Node(addNode,nullptr);
}
else{
addNode = new Node(this->tailptr->prev,this->tailptr,name,occupation);
this->tailptr->prev->next = addNode;
this->tailptr->prev = addNode;
}
}

youtube.com/watch?v=KMU0tzLwhbE

Why, though?
struct mystuct s = {0};

I posted here again right before you responded in the other thread. I'll post it.

#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include
#include

class Node {
friend class LinkedList;
private:
std::string name = "";
std::string occupation="";
Node * prev = nullptr;
Node * next = nullptr;

public:
Node(Node* prev, Node* next, std::string name, std::string occupation);
Node(Node* prev, Node* next);
~Node() = default;
}; // Node

class LinkedList {
private:
Node* headptr = nullptr;
Node* tailptr = nullptr;
public:
~LinkedList() = default;
void append(std::string name, std::string occupation);
int deleteItem();
int deleteItem(int position);
//int deleteItem(std::string occupation);
int deleteItem(std::string name);
int length();
void printList();
};
#endif /** LINKED_LIST_H */

Thanks

Doesn't work if it's dynamically allocated.

if(this->headptr == nullptr){
addNode = new Node(this->headptr,

hmmmmm

*ptr = (struct mystuct){0};
or just use calloc.

maybe I am misunderstanding the head and tail of a linked list. I am treating it as a starting point where it doesn't hold any real data. That is why I check if the headptr itself is null so I know if it has anything.

I then create a node that has the new data but I can't really delete it afterwards. What would be a better approach?

I don't think calloc is guaranteed to work if the struct contains pointers; the whole 0 == NULL convention only exists at compile time, not at runtime.

memset won't work with that, either.

head and tail point to the first and last nodes, respectively. If there aren't any nodes, they don't point to anything.

It would simplify things greatly if Node was a struct instead of a class.

Just think it through and be systematic. When you append to (the back) of a double linked list:

1) create a node from given data. Don't worry about its pointers yet.

2) if this is the first node, set the head and tail to this node. The node's pointers will remain nullptrs.

3) otherwise:
set the old tail node's next ptr to the new node
set the new node's prev ptr to the current tail node
set the new tail to the newly created node

you're doing a lot in the constructors. It'd be easier and more readable to handle the pointers separately.

Working on a list of things my dream language would have. Whether I actually implement it or not is another question

1. 'use' for importing
2. # for local imports
3. RefCounting
4. Structs only, no classes
5. Absolutely no ::
6. Extra constraints on a function must be specified in a declaration file (or in some other clean way, idk)
7. D-tier metaprogramming
8. Static, strong (?) typing
9. Compiled
10. Use Posix library as the standard library

Here's an example of what it could look like. Now I don't want to suck my own dick or confuse you, but this should show you the flow of what you need to do:

template
void DLinkedList::insertBack(T data) {

//create node object with our data
dlNode *item = new dlNode(nullptr, nullptr, data);

//if this is first element, set head accordingly
if(elemCount == 0) {
head = item;
}

//else, we already have a head and tail pointing to something
else {
tail->next = item;
item->prev = tail;
}

tail = item;
++elemCount;

}

If I wanted to print the toString method, what would need to be modified?

You've literally described C you tit

in Java, toString() is a common method that returns a string. Generally, you don't want the method itself to print anything.

So to print it, you would just call
Minesweeper m;
...
System.out.print(m);

Or something similar.

I enjoyed what I read of C++ Primer (coming from Python and a little bit of C).

Haven't ever read the first one.

Did you even read the list

Aside from 'use', that's C.

#include, no classes only structs, no ::, constraints on functions are specified in the header file, static and strongly typed, compiled, uses Posix

Thanks mate. I'll just scrape the whole second class thing and just turn it into a struct.

haha

Ban C

Trying to learn Python here.
What is the difference between List and Generator. I am trying to understand when it the proper time to use Generator and Yield over a list. If I only want to iterate over a loop once in my entire program I use a generator but if I want to use the same loop multiple times I use list?

Is that right?

>IMPERATIVE
>LINK
>LISTS
fucking get out
get the fuck out
we don't need you and your shit

t. doesn't know anything about language design, thinks picking and choosing arbitrary features from his favorite meme lang would somehow make a "good" language

>we don't need you and your shit
Don't call us, we'll call you

Computers are imperative, you dork.

Even in an imperative language, persistent immutable linked lists are the only good kind of linked list. If you want a mutable container, use a vector.

>thinks picking and choosing arbitrary features from his favorite meme lang would somehow make a "good" language
Look at ruby, u dumbfuck.

someone slept through complexity class

Say I want to parse through data and sort it while I parse it. Do I still use a vector?

I'm trying to make minesweeper in java but I hit a wall trying to do two things.

I have two arrays called mn[row][column] (boolean) & minedneighbours(int) which represents the minefield & the number of neighbouring squares which contains mines for each squares.

I want to ask how to:
A) create a method with row & column param, which set the tile that will be mined & then increments the neighbouring total of the surrounding tiles using a for loop.
It should return true if the mile is mined & false if the max num of mines is exceeded or the square is already mined.

Anyone know how to do this, as the instructions are really vague?

>#include
No, # does something different
#use lib; // lib is imported only for fun(), may change it and just do it in the function definition
void fun();

>constraints on functions are specified in the header file
I'm more thinking of typeclasses like Haskell has when I say this, C afaik has nothing like that
>strongly typed
C's weakly typed

Also I'm not actually gonna use the Posix library, that was a bad joke

>arbitrary
I mean, I kind of chose them for a reason

gcc: warning: main.o: linker input file unused because linking was not done
gcc: warning: history.o: linker input file unused because linking was not done


Can somebody shed some light on wtf is happening? I have an identically configured Makefile for another project with the exact same flags and it works perfectly, but when trying to add in another file to this project it just starts spitting this error and doesnt produce the executable.

CC = gcc
CFLAGS = -Wall -g -c

all : main

main : main.o history.o
$(CC) $(CFLAGS) $^ -o output

main.o : main.c history.h
$(CC) $(CFLAGS) $<

history.o : history.c history.h
$(CC) $(CFLAGS) $<

why do you guys hate Java so much? What has Java ever done to you?

seems like linking wasn't done

You know damn well what I'm trying to get help with

-c on final link

gcc.gnu.org/onlinedocs/gcc/Link-Options.html
>-c
>-S
>-E
>If any of these options is used, then the linker is not run

minedneighbors should not be an array
public boolean minePosition(int r, int c) {
if(pos[r][c].isMined() || mineCount == maxMines) return false;

else {
pos[r][c].setMined(true);
}
return true;
}


you should only start counting mined neighbors after you're done placing all the mines. Unless you plan on adding mines while in game.

public void updateNum(int r, int c) {

pos[r][c].minedNeighbors = 0;

//count left neighbors
for(int i = -1; i < 2; ++i) {
if( r-1 is in bound ) {
if( (c + i) is in bound ) {
if(pos[r-1][c+i].isMined()) ++minedNeighbors;
}
}
}

}


blah blah blah. you get the point.

Good place to start with C? I'm working through "Learn C the hard way", what would be a good beginner/intermediate to move on to?

try 'pointers on c' kenneth reek

>Learn C the hard way
That book is absolute garbage, and the author is a hack.

What would you suggest?

Literally the only advantage of linked lists over arrays is that you can easily add and remove nodes from any position. An immutable linked list is like a car without wheels.

How good is that book my teacher recomended this meme book to the class

C++

k&r is useless trash for 2017, unless you're a historian

good book, don't listen to

Why c++ over C? I've heard a few people say programming didn't truly sink in for them till they tried C, but why C++?

Cause it is nice to have a skill that could one day get you a job. Don't listen to the C-tards

C++ is more relevant, as close to hardware as C (as far as memory allocation/pointers go), and teaches principles that will help you in pretty much all other relevant programming languages (OOP).

It's just better.

listen to this diehard, read k&r and you will pickup shitty habits and style from 70s that you will end up re-learning later.

or you could begin with (which also covers all the k&r shittyness) and avoid wasting time altogether.

if your teacher advised you k&r, he is either stupid or trolling.

Or he is old school and caught in his old ways. You will be surprised how many people are like that in the working world. Project leads and directors that refuses to budget because "That's the way it has always been done". This doesn't just apply to coding.

Alright, do you guys have any books for c++ then? I know I can find them, but I'd love a recommendation.

What about C#?

the language wars are so unbelievably insignificant to what beginners will do that none of the reasons why you should pick one over the other will change the outcome of your education so long as your teaching material is good, and you can write off idiots peddling their language to you as a beginner

no matter which language you learn it will open a gateway into easily learning the others, and you will definitely be using more than one language EVENTUALLY in your journey as a programmer, but that will happen when you're more knowledgeable about why certain languages may be better in some situations than others
if you want to know how computers work, (you will have to accrue this knowledge eventually, one way or another, to not be a shit programmer) then starting with C is a good option

(((C#))) is by the jews. And I mean it seriously because fuck MS.

As for the books, try
stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

Eval and apply and read-eval-print loops
Conses and cdrs and closures and stack groups
Sussman and Abelson and their teachings
These are a few of my favourite things

Structures and data and big O notation
Factorisation and search and collation
Linked lists and quivers and matching on strings
These are a few of my favourite things

Functions and sets that can both be a number
Hotels where infinite lodgers may slumber
Monoids and functors and vectors and rings
These are a few of my favourite things

When the core dumps
When the bugs show
When I'm feeling sad
I simply remember my favourite things
And then I don't feel so bad

Why not just using inside of the function?
void fun()
{
using lib;
// whatever
}

c is pretty small, you have to implement a lot of stuff yourself, means you have to actually know algorithms,hardware,os,networking,etc. so if you suck at any of it, you code in c will suck, that's why people, who can't or don't have time to implement in c, go into c++ where a lot is already implemented but with the cost of bloatedness.

I'm interested in learning programming, what should I start with?

Lisp.

Do you have prior knowledge with programming?
I suggest Python if you don't. Ignore people who say C or C++. Only do does if you are very sure you will like programming and in it for the long run. Python will give you a bare minimal taste of what programming is like. Decide afterwards if you want to commit to programming and then come here and ask again.

knee socks

Haskell

Knee socks?

Great, C has given us yet another privilege exploit in the Linux kernel.

How do I into lambda calculus?
I have formal language background.

Okay.
Sell me on Rust

It's shit.
Don't bother.

What are you defending?

Don't bother. It is free. Just leave some ferrous object out in the open.

The integrity of iron.

Rust is not a revolutionary language with new cutting-edge features, but it
incorporates a lot of proven techniques from older languages while massively improving upon the design of C++ in matters of safe programming. The Rust developers designed Rust to be a general-purpose and multi-paradigm language. Like C++, it is an imperative, structured, and object-oriented language.

Besides this, it inherits a lot from functional languages and also incorporates advanced techniques for concurrent programming.
In Rust, the typing of variables is static (because Rust is compiled) and strong. However, unlike Java or C++, the developer is not forced to indicate the types for everything as the Rust compiler is able to infer the types in many cases. C and C++ are known to be haunted by a series of problems that often lead to program crashes or memory leaks which are notoriously difficult to debug and solve.
Think about dangling pointers, buffer overflows, null pointers, segmentation faults, data races, and so on. The Rust compiler (called rustc ) is very intelligent and can detect at least 99% of all these problems while compiling your code, thereby guaranteeing memory
safety during execution. This is done by the compiler by retaining complete control over memory layout, without needing the runtime burden of garbage collection. In addition, its safety also implies much
less possibilities for security breaches.

Sup Forums toddlers do not like it because they are. beginners, Rust is a bit too hard for them to swallow. So they are butthurt over it. Most of the Rust haters are pre teen undergrads that know zero thing about programming and what a piece of shit C really is.

>pre teen undergrads
If you are a pre teen and an undergrad, that would make you a genius. I think you mean, pre teens or undergrads.

Also Rust compiles native code like Go and Julia. However, in contrast to these two, Rust doesn't need runtime with garbage collection. In this respect, it also differs from Java JVM and the languages that run on the JVM, such as Scala and Clojure. Most other popular modern languages such as .NET with C# and F#, JavaScript, Python, Ruby, Dart, and so on, all need a virtual machine and garbage collection.
As one of its mechanisms for concurrency, Rust adopts the well-known actor model from Erlang. Lightweight processes called threads perform work in parallel. They do not share heap memory but communicate data through channels, and data races are eliminated by the type system. These primitives make it easy for programmers to leverage the power of many CPU cores
that are available on current and future computing platforms.

I hear Sup Forums babbies moan about rust being not portable, which is very laughable. The rustc compiler is completely self hosted, which means that it is written in
Rust and can compile itself by using a previous version. It uses the LLVM compiler framework as its backend and produces natively executable code
that runs blazingly fast because it compiles to the same low-level code as C++.
Rust is designed to be as portable as C++ and run on widely used hardware and
software platforms; at present, it runs on Linux, Mac OS X, Windows, FreeBSD,
Android, and iOS. It can call C's code as simply and efficiently as C can call its owncode, and conversely, C can also call Rust code. Rust can even "pretend" to be C and produce the same piece of shit unsafe binaries but that'd defeat the purpose of such a great language.

Does it mean Rust is purely functional? No love for OOP?

What high paying job can I get if I know Rust.

> Rust adopts the well-known actor model from Erlang.
No it doesn't.
> Lightweight processes called threads perform work in parallel.
No they aren't lightweight, they are full-blown OS threads.

A simple echo server, desu.

#!/usr/bin/env powershell

# Create a TCP server listening on port 50750
$ip = [Net.IPAddress]::Parse("127.0.0.1")
$port = 50750
$server = New-Object Net.Sockets.TcpListener $ip, $port
$server.Start()

$buffer = New-Object byte[] 1024

while ($true)
{
# Accept a client
$accepted = $server.AcceptTcpClientAsync()
$client = $accepted.Result
if ($client -eq $null) { continue }

# Read data from client and echo it back
$stream = $client.GetStream()
do
{
$n = $stream.Read($buffer, 0, 1024)
$stream.Write($buffer, 0, $n)
} while ($stream.CanRead)

# Clean up
$client.Dispose()
}

Rust's object orientation is not that explicit or evolved as common object-oriented languages such as Java, C#, and Python as it doesn't have classes. Compared with Go, Rust gives you more control over memory and resources, so lets you code on a lower level. Go also works with a garbage collector, and it has no generics or a mechanism to prevent data races between its goroutines that are used in concurrency. Julia is focused on numerical computing performance; it works with a JIT compiler and doesn't give you that low-level control that Rust gives.


Rust is two year old. I don't think you can be a "high paid" programmer if you know only one language in the first place. However Samsung and Mozilla might hire you for their Servo projects.

If I had a choice between C, C++ and Rust for making a system for a powerplant I would choose Rust in a heartbeat.

So I should learn another language if I want to make money and learn Rust if I plan to be a NEET?

> Has a dick
> Liking it but not gay
Are you retarded?

What language are you coming from? C?

Feel free to read

is that powershell on unix? you absolute madman.

What's your point?

I like to learn a skillset that is actually useful in the real world

It's portable, lol.