/dpt/ - Daily Programming Thread | Why the fuck did I get into IT edition

What are you working on?
Previous:

Attached: 1526654585737.jpg (753x999, 160K)

Other urls found in this thread:

refspecs.linuxbase.org/elf/gabi4 /ch4.strtab.html
en.wikipedia.org/wiki/C++11#Explicitly_defaulted_and_deleted_special_member_functions
wiki.installgentoo.com/index.php/Main_Page
sourceforge.net/projects/win-agui/
geeksforgeeks.org/merge-sort-for-linked-list/
twitter.com/NSFWRedditImage

next edition will be nigger edition

learning how to deploy packages on debian.

java

Attached: 1520987687923.jpg (211x239, 6K)

someone post some code

size_t ph_off = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * image->num_segments;
size_t data_start = ALIGN_ADDR(ph_off, image->page_size);

size_t data_end = data_start + image->file_size;

char strs[] = "\0.data\0.text\0.text\0.data\0.bss\0.rodata\0.shstrtab\0";

uint32_t idxs[] = {
1, 7, 13, 19, 25, 30, 38, 0
};

size_t sh_start = data_end + sizeof(strs);

Elf64_Ehdr ehdr;
ehdr.e_ident[EI_MAG0] = ELFMAG0;
ehdr.e_ident[EI_MAG1] = ELFMAG1;
ehdr.e_ident[EI_MAG2] = ELFMAG2;
ehdr.e_ident[EI_MAG3] = ELFMAG3;
ehdr.e_ident[EI_CLASS] = ELFCLASS64;
ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
ehdr.e_ident[EI_VERSION] = EV_CURRENT;
ehdr.e_ident[EI_OSABI] = ELFOSABI_LINUX;
ehdr.e_ident[EI_ABIVERSION] = 0;
ehdr.e_ident[EI_PAD] = 0;
ehdr.e_type = ET_EXEC;
ehdr.e_machine = EM_X86_64;
ehdr.e_version = EV_CURRENT;
ehdr.e_entry = image->vm_entry_point + data_start;
ehdr.e_phoff = sizeof(ehdr);
ehdr.e_shoff = sh_start;
ehdr.e_flags = 0;
ehdr.e_ehsize = sizeof(ehdr);
ehdr.e_phentsize = sizeof(Elf64_Phdr);
ehdr.e_phnum = (ph_off - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr);
ehdr.e_shentsize = sizeof(Elf64_Shdr);
ehdr.e_shnum = 2 + image->num_sections;
ehdr.e_shstrndx = 1 + image->num_sections;

fwrite(&ehdr, sizeof(ehdr), 1, fp);

What wizard language is this?

That would be C

>Elf64_Ehdr ehdr;
> ehdr.e_ident[EI_MAG0] = ELFMAG0;
> ehdr.e_ident[EI_MAG1] = ELFMAG1;
> ehdr.e_ident[EI_MAG2] = ELFMAG2;
> ehdr.e_ident[EI_MAG3] = ELFMAG3;
> ehdr.e_ident[EI_CLASS] = ELFCLASS64;
> ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
> ehdr.e_ident[EI_VERSION] = EV_CURRENT;
> ehdr.e_ident[EI_OSABI] = ELFOSABI_LINUX;
> ehdr.e_ident[EI_ABIVERSION] = 0;
> ehdr.e_ident[EI_PAD] = 0;
> ehdr.e_type = ET_EXEC;
> ehdr.e_machine = EM_X86_64;
> ehdr.e_version = EV_CURRENT;
> ehdr.e_entry = image->vm_entry_point + data_start;
> ehdr.e_phoff = sizeof(ehdr);
> ehdr.e_shoff = sh_start;
> ehdr.e_flags = 0;
> ehdr.e_ehsize = sizeof(ehdr);
> ehdr.e_phentsize = sizeof(Elf64_Phdr);
> ehdr.e_phnum = (ph_off - sizeof(Elf64_Ehdr)) / sizeof(Elf64_Phdr);
> ehdr.e_shentsize = sizeof(Elf64_Shdr);
> ehdr.e_shnum = 2 + image->num_sections;
> ehdr.e_shstrndx = 1 + image->num_sections;

D^:

my apologies for not using arch or gentoo m'lady.

test

one more

private void putDisk(Stack paramStack, int paramInt)
{
assert ((paramStack.isEmpty()) || (paramInt < ((Integer)paramStack.top()).intValue()));

paramStack.push(Integer.valueOf(paramInt));
}

private int takeDisk(Stack paramStack)
{
assert (!paramStack.isEmpty());

int i = ((Integer)paramStack.top()).intValue();
paramStack.pop();
return i;
}

private void moveDisk(Stack paramStack1, Stack paramStack2) {
assert (!paramStack1.isEmpty());
assert ((paramStack2.isEmpty()) || (((Integer)paramStack1.top()).intValue() < ((Integer)paramStack2.top()).intValue()));

putDisk(paramStack2, takeDisk(paramStack1));
numMoves += 1;
showTowers();
}

private void solve(int paramInt, Stack paramStack1, Stack paramStack2, Stack paramStack3)
{
assert (paramInt >= 0);

if (paramInt >= 1) {
solve(paramInt - 1, paramStack1, paramStack3, paramStack2);
moveDisk(paramStack1, paramStack2);
solve(paramInt - 1, paramStack3, paramStack2, paramStack1);
}
}

>making an array of string indices to access elements of multiple variable length null terminated strings stored in a single string

how very indian of you, but how about making an array of strings like a normal person?

autoboxing was added 13 years ago

>how very indian of you, but how about making an array of strings like a normal person?
Because that's how the specification is, you fucking dimwit.

refspecs.linuxbase.org/elf/gabi4 /ch4.strtab.html

autobox roll out

thats just an algorith to solve the Hanoi tower problem.

EXPTime if i remember correctly?

Isn't C fun to work with?

that's for a compiled program's strings you indian, not the names of the assembly segments.

If the api is complicated the api is complicated, nothing to do with C.

It's for all string table sections, including section names. See SHT_STRTAB section.

There has to be a cleaner way of doing it than manually writing down index offsets even if you need the strings to be sequential in memory.

I’ve never had to construct my own elf header though, usually I just read them.

Well, a sane language would have a default constructor that made sense, instead of literally just initializing with garbage data or zeros being my only two options.

>There has to be a cleaner way of doing it than manually writing down index offsets even if you need the strings to be sequential in memory.
It's easier because I can just do sizeof(strs) and get offsets and everything correct. I mean, it's only a couple of sections, I don't even care that much.

>instead of literally just initializing with garbage data or zeros

that’s what a default constructor would do 99% of the time anyway so it really doesn’t make a difference

Doesn't C have a struct initializer?

>that’s what a default constructor would do 99%
Nah, man. It would usually create a valid instance. I mean, needing to fill out magic signatures and manually setting the size of the header is just a waste of time.

welcome to lower level system programming, nigger cattle

It does, but seeing how I need to fill in all the members it would be equally horrendous.

Elf64_Ehdr ehdr = {
.e_indent = {ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, ELFCLASS64, ELFDATA2LSB /* ... */ 0},
.e_type = ET_EXEC,
/* ... */
};

>elf executables
>lower level
This is higher level than what I usually work with.

default constructors are a very thin abstraction. writing a function that allocates an instance and populates the fields does the same exact thing

>default constructors are a very thin abstraction
Exactly. Thin abstractions = good

>writing a function that allocates an instance and populates the fields does the same exact thing
It's menial work and prone for making errors.

Looks much better IMO

wtf the fuck are you rambling you retarded nigger, you would have to define constructor with your shitty oop lang anyways

>default constructor
>"you'd have to define it"
en.wikipedia.org/wiki/C++11#Explicitly_defaulted_and_deleted_special_member_functions

>APIs don't exist

of course there is. Here's my first shot at it.
#include
#include
#include
char * seg;
int max;
int cur;

int * inds;
int indsmax;
int indscur;
void addstr(char * str) {
if(seg == NULL) {
seg = malloc(20);
inds = malloc(2 * sizeof(uint32_t));
seg[0] = '\0';
inds[0] = 1;
cur = 1;
max = 20;
indsmax = 2;
}
if(indscur + 1 == indsmax) {
indsmax*=2;
inds = realloc(inds,indsmax * sizeof(uint32_t));
}
do {
if(cur == max) {
max*=2;
seg=realloc(seg,max);
}
seg[cur] = *str;
cur++;
} while(*(str++));
inds[++indscur] = cur;
}
void finish() {
inds[indscur] = 0;
}
int main() {
addstr("foo");
addstr("fubar");
finish();
for(int i = 0; inds[i]; i++)
printf("%s\n",seg +inds[i]);
}

>int * inds
>int cur
I guess you should change those to uint32_t. You always notice the little things seconds after you post.

elf is so fucking bloated holy shit

last test

Does this general not have a pastebin for resources and books? I'm looking for language agnostic books about software engineering and architecture as well as design patterns

hm clean code and/or clean architecture from robert C. Martin?

design patterns aren't language agnostic. wiki.installgentoo.com/index.php/Main_Page

This is a circlejerk not a fucking general. GTFO

Designing a tracker that I will write in Rust.

public int[][] matrix;
for (int[] a : matrix) {
for (int b : a) {
System.out.println(b);
}
}

is this wrong? how do i iterate through a n-dimensional array using foreach in java

looks good to me.

but maybe try the Stream api?

shouldn't b be another matrix tho?

>a sane language would have a default constructor that made sense
Oh, you're telling him that the correct way to do this is to assume the configuration for the user. How interesting. Why don't you just assume the entire api use for the user then? Just make it one call for the entire program?
Also how in the fuck is a diff between the default and what's desired even remotely close to good API?
On to op of that it's got nothing to do with the language. As you can easily have a Elf64_Ehdr init function if you like.

What you actually want is calls that encode intent in a broader sense than the specifics declared here. Maybe even give users parametric initialization functions if you're feeling cheap.

You'd have to be an idiot to make your suggestion. Which I'm sure you are.

i meant array*

>It's menial work and prone for making errors.
Yes for the library author. Same thing goes for the writing of constructors. What you get with constructors is constraints on when you can initialize. Which has its uses but it's not the general case.

Those are nice strawmans you are arguing there.

>design patterns aren't language agnostic
design patterns are interface abstraction techniques that can be applied with any programming language implementing the data abstraction technique known as object, as proved by the gang of four.

hm not really
line 2 iterates through the first dimension
line 3 iterates through the second dimension

Should I learn vim or emacs? I'm learning programming as a hobby, only know a little C but not nearly enough to make anything interesting yet.

sourceforge.net/projects/win-agui/

>learning a text editor
Just use a decent text editor

can anyone provide me an iterative version of mergesort for linked lists in C?

I'll send you some nigerian prince gibs mani in exchange

Attached: wojak.png (560x560, 57K)

i use vim all the time. There is a Vim binding plugin for emacs. Makes your life a lot easier

>strawmans
No. It's not a strawman because it's claimed directly in the post.
>a sane language would have a default constructor that made sense
This is a claim about the API deficiency expressed as a language feature, incorrect.
>instead of literally just initializing with garbage data or zeros being my only two options.
Expresses complaints about the API, not the language. Pretending that there's an obvious general default configuration.

The complaints you may have is the sizeof nonsense and the lack of initialization functions. The way to choose the magic number was poor but that's not an API fault.

also, how do I fix segfaults for stack overflow on large fucking lists (>100k nodes) when using recursive?

what are the best 2/3 languages to know for cyberphycical systems?

Latin

post your recursivefunction

>design patterns are limited to oop
>all design patterns make sense in all languages
wew lad

>No. It's not a strawman because it's claimed directly in the post.
Except it isn't, I literally never argued that the API should assume neither configuration (I merely pointed out that fixed fields could be auto filled such as magic signatures) nor that the language should be extended with specific support for Elf (you can simply implement a sensible API for this).

>Pretending that there's an obvious general default configuration.
First of all, I didn't. Secondly, there actually is though. Linux is the only UNIX-like system that still uses ELF for 32 bit and the only system that ever implemented the 64-bit version of the standard, which means that you could easily autofill members assuming Linux compatibility.

Definitely C and C++. Maybe Java.

design patterns are not tied to a specific programming language. once you have objects, either that it is classes based or prototypes based or closures based, you can implement design patterns. it's like you were saying that functions are not pl agnostic.

adaption of
geeksforgeeks.org/merge-sort-for-linked-list/

>Linux is the only UNIX-like system that still uses ELF for 32 bit and the only system that ever implemented the 64-bit version of the standard
Wrong.

You can always rewrite recursion with explicit stacks or something.

Here's my iterative mergesort. I cut out the part that makes it "natural." aka faster, it's not hard to tack that on. Let me know if you want the code for making it natural. Lost my C version, so have the Scheme one. Have fun rewriting it in C.
(define (merge a b)
(cond ((null? a) b)
((null? b) a)
((< (car a) (car b)) (cons (car a) (merge (cdr a) b)))
(else (cons (car b) (merge a (cdr b))))))
(define (mergesort lst)
(let loop ((rem (map list lst)))
(if (null? (cdr rem))
(car rem)
(loop
(let pairloop ((rem rem))
(cond ((null? rem) '())
((null? (cdr rem)) rem)
(else
(cons (merge (car rem) (cadr rem))
(pairloop (cddr rem))))))))))

>once again returning to fucking objects
Design patterns aren't limited to OOP. And yeah, functions aren't programming language agnostic. They have different semantics in different languages. Broaden your horizon.

Still no reason headers with fixed values couldn't be autofilled by a constructor.

You would still need to write that constructor, so I don't see what your point is.

Some ELF library would do it once.

>You would still need to write that constructor,
APIs and libraries exist, you know.

listen you dumbfuck nigger cattle, you can do everything you need from a simple init function which is 1:1 interchangeable with an OOP constructor

it’s just that your tiny subhuman (nigger) pea brain can’t handle initialization done by an init function and that’s entirely your own problem

>They have different semantics in different languages.
again, you are confusing the concept with its implementations. this is embarrassing.

>Design patterns aren't limited to OOP.
they are

This is supposed to be my assginment and I can't understand JACKSHIT

FML

Attached: 55943505.png (674x696, 33K)

Well, merge isn't iterative, but if you use the trick in this code, you can make it iterative:
(define (map f xs)
(let ((ret (cons (f (car xs)) '())))
(let loop ((pair ret) (xs (cdr xs)))
(unless (null? xs)
(set-cdr! pair (cons (f (car xs)) '()))
(loop (cdr pair) (cdr xs))))
ret))

What does that have to do with constructors?
It takes a few seconds to write a simple 10 line function returning an elf header struct with default values if you really need one, so who cares?

>getting this upset over constructors
Init functions that do nothing but set members and that must be explicitly called in a certain order is bad practice.

It takes a few seconds to implement printf using write system calls also, why even bother to have a standard library at all?

No I'm not you close-minded tard. Read up on dynamic scope. t. emacs lisp

i don't know how to code lol

First for windows batch script

first, you are confusing closure with functions. second, emacs lisp has procedures, not functions.

functional programming languages are based on lambda calculus. once you have functions, you can apply all the techniques offered by lambda calculus.

>In functional programming, a monad is a design pattern

Attached: think.png (600x600, 143K)

>It takes a few seconds to implement printf using write system calls also,
No, it doesn't.
Printing floating point numbers is highly non trivial if you wanna do it correctly.

A library for file formats shouldn't force any defaults on you, they should just give you the raw data structures for the spec and define some constants and maybe helper functions for calculating varies offsets, checksums, etc.

>not bash for windows

it is an objective truth

>being this literal
I was making an analogy, for crying out loud.

>A library for file formats shouldn't force any defaults on you
But I'm not even talking about different configurations, I'm talking about members that have a fixed value and must have those values according to the specification.

>they should just give you the raw data structures for the spec and define some constants and maybe helper functions for calculating varies offsets, checksums, etc.
And that's how you end up with horrible mess like

>not powershell script
get with the times

>not just using python and its set of libraries

>not being backwards compatible with MS-DOS 6.2

not haskell.

Attached: 1516782608251.png (900x810, 689K)

>implying python works on windows

>implying it doesn't