/dpt/ - Daily Programming Thread

prev: What are you working on, Sup Forums?

Other urls found in this thread:

sqlite.org/appfileformat.html
soundcloud.com/sytricka/wsg-running-in-the-90s-vaporwave
twitter.com/SFWRedditVideos

You're missing the point entirely.
You shouldn't have to do any of that in the first place. And the fact that you've just accepted it and think it's normal, is pretty sad. I'll never understand how people can fall for Lisp.

Masters degree. There is a 3-year degree too, but it's still 3 years down the drain.

Most of the time though, the lisp compiler will detect types anyway, alert on type errors, optimize out runtime checking.

I don't really program in lisp that much anymore outside of small test programs, and in general I prefer statically typed languages, but you're exaggerating how cumbersome those things are.

Julia is semantically a lisp-1 (it uses matlab-like syntax, but it gets parsed into lisp s-expressions and has lisp macros).

For nontrivial code, it's about as fast as C with little help from the programmer (give or take a factor of 2, slower a little more often than faster). The JIT is a little slow, so it takes a second to warm up, but that's really my only complaint so far.

So it's not that lisp is inherently slow, it's just that the implementations people use tend to suck.

I'm working on dropping my hopes and dreams

Some countries take 5 years to teach a CS degree apparently

Maybe they'll actually learn something in that time.

What country?

Yeah, here too (but it's a very general master's degree, not just CS).

practicing for an interview tomorrow
what does getting compiled into s expressions even mean? if it has an AST it's compiled into s expressions
wroNg

Not compiled, parsed. As in, the matlab-like text goes in, and expressions come out (usually shown as s-expressions if you e.g. try to print them out to the screen to debug). You can write macros (operating on expressions) just like any other lisp.

Master's. Bachelors is 3, but that's more software than CS.
year 1
>maths
>intro
>digital- and computer technology
>circuit technology
>computer communication
>programming
year 2
>maths
>computer architectures
>programming detailed
>databases
>control and regulation technology
>object oriented modeling and design
>computer- and telecommunication
>project
year 3
>project
>constraint management
>real time systems
>optional courses
>computer security
>thesis
Sweden.

>wroNg
Wrong

Is there a good format for storing program resources?

I don't want them baked into the executable, and since they're already compressed they don't need compression like .zip. .tar seems to be the most bloated format in existence, I guess tape archives don't really apply nowadays and understand why .tar is never just .tar and always .tar.gz, .tar.bz2, .tar.xz.

Should I roll my own? This seems dead simple, a header describing a file name, their offset and length.

what kind of resources

audio, images, textures mostly but would like it to fit any file I want to load.

How about ... files?

I don't want data/images/[3000 files]

18 y/o college fresman here. I just got offered a job that requires C# and SQL, although i've only ever used Java and Python. This will be my first programming job, any advice/ resources?

by that logic any language with macros is a lisp

Okay then use tar+gzip

You can't seek in a tar.gz, if I only need 1 out of those 3000 images(without loading all 3000 into memory) I need to uncompress the entire .tar.gz to get those 10KiB I need.

Also images are already compressed so deflate is useless.

...

ask questions
you will get lost in the codebase. this is ok. the more you work with it, you'll piece things together.
you will probably feel inadequate compared to your more senior coworkers. this is also ok. everyone had to start somewhere.
just don't give up and ask plenty of questions. you might feel miserable, but just stick it out.

Macros operating on expression are different from macros operating on text. And an s-expression is a particular type of expression. I would agree that any language that is parses its code into s-expressions and can operate on these expressions is by definition a lisp.

There are formats that exist btw, if you ever played a VN you'll see a .xp3. That's what it does.

I'm just wondering if there's something more simple that doesn't include an broken nippon format with 6 million different incompatible implementations.

What do you mean doubt, what do you want from me.

You could just put everything in a sqlite3 database if you didn't want anything fancy.

Thanks, senpai. I've heard from older people that the only stupid questions are the ones you ask twice, thoughts?

I didn't think of that, not a bad idea.

Although, how's the overhead for sqlite? Isn't sqlite still single threaded?

You need a mutex for writes but can do multi-threaded reads okay I'm pretty sure.

sqlite.org/appfileformat.html

maybe, but who cares. just learn as much as you can, and do work that you think you'd be proud of. learn to work with other people too. you'll probably be dealing with people more than you will be programming.

Thanks m8s, this seems pretty good. I was fucking reading the libarchive source code looking for a way out of implementing something so basic.

I'll try it.

#pragma once
#include
static inline void swap(void *restrict a, void *restrict b, size_t s) {
long *restrict al, *restrict bl;
char *restrict ac, *restrict bc;
for (al = a, bl = b; s > sizeof(long); s -= sizeof(long), ++al, ++bl)
*al ^= *bl ^= *al ^= *bl;
for (ac = (char *restrict) al, bc = (char *restrict) bl; s; --s, ++ac, ++bc)
*ac ^= *bc ^= *ac ^= *bc;
}

If it gets parsed as s-expressions can I just bypass the stupid fucking matlab syntax and write the s-expressions?

2 questions
1. what is the best datalog implementation (do any good datalogs even exist?)
2. would I gain any benefit by using a BEAM language over another language for some kind of simple reverse proxy software

yes, write in lisp

Gotta be fast on compilers from 20 years ago.

Good, but not as good as
#pragma once
#include
#include
#include
static inline void swap(void *a, void *b, size_t s) {
void *c = malloc(s);
memcpy(c, a, s);
memcpy(a, b, s);
memcpy(b, c, s);
free(c);
}

>*al ^= *bl ^= *al ^= *bl;
SEQUENCE POINTS, UB UB UB UB

Strangely, you're probably right on modern compilers. Easier to interpret.

Assignment is right associative

Its side effects aren't right associative, only its evaluation is. Same with any operator that produces a value

Has nothing to do with it, each of those intermittent ^= modifys the lvalue without a sequence point(where you can say x has value y), so once you reach the second ^= *al part you've UB. The value of *al at that point is undefined.

ftfy
#pragma once
#include
static inline void swap(void *restrict a, void *restrict b, size_t s) {
long *restrict al, *restrict bl;
char *restrict ac, *restrict bc;
for (al = a, bl = b; s > sizeof(long);
s -= sizeof(long), ++al, ++bl) {
*al ^= *bl;
*bl ^= *al;
*al ^= *bl;
}
for (ac = (char *restrict) al, bc = (char *restrict) bl;
s; --s, ++ac, ++bc) {
*ac ^= *bc;
*bc ^= *ac;
*ac ^= *bc;
}
}

#include

void Mescape(char *a, char *b)
{
int forward = 0;
int index;

for(index = 0; a[index] != '\0'; ++index) {
switch(a[index]) {
case 10: // if \n
b[index + forward] = '\\';
b[index + ++forward] = 'n';
break;
case 9: // if \t
b[index + forward] = '\\';
b[index + ++forward] = 't';
break;
default:
b[index + forward] = a[index];
}
}
b[index + forward] = '\0';
}

void main(void)
{
char *a = "asfw\t\t\n"; // kdnjgker\nkasndlgeknrh\nwqkeieqrt\t\n\n";
char *b; // crashes horribly because memory is not allocated i guess?

Mescape(a, &b);
puts(&b); // outputs contents of *a without tabulations and newlines
}

replacing newlines and tabulations. program crashes if *a is too big though, am i supposed to allocate memory or is there another way?
K&R exercise 3-2 btw.

Do you guys recommend any courses / video ones for example - there are a lot of deals right now on multiple websites, I would appreciate if you could point me towards some - thank you !

In your program *b is just a pointer, it doesn't point to anything. You're writing random shit to whatever was on the stack, it has nothing to do with *a, it's the b[...] = ... stuff.

>Gotta be fast on compilers from 20 years ago
>Gotta be fast

My friend, this a SFW board. Just because it's 10:00PM, on a friday and I'm wasted out of my mind doesn't mean that my grandmother isn't reading /dpt/ with me.

This was quite offensive, apologize to her.

#pragma once
#include
static inline long *restrict lxor(long *restrict a, long *restrict b) {
*a ^= *b;
return a;
}
static inline char *restrict cxor(char *restrict a, char *restrict b) {
*a ^= *b;
return a;
}
static inline void swap(void *restrict a, void *restrict b, size_t s) {
long *restrict al, *restrict bl;
char *restrict ac, *restrict bc;
for (al = a, bl = b; s > sizeof(long);
s -= sizeof(long), ++al, ++bl)
lxor(al, lxor(bl, lxor(al, bl)));
for (ac = (char *restrict) al, bc = (char *restrict) bl;
s; --s, ++ac, ++bc)
cxor(ac, cxor(bc, cxor(ac, bc)));
}

sorry here yuor aplogy

thank, her walker stoped hurtin

>In your program *b is just a pointer, it doesn't point to anything.
i-i wanted a dynamic sized array. :(
point wherever, write to as many bytes that is available "in" *a until NULL is found, append a NULL at the end, puts() does its thing.
but, *kaboom*.
if it isnt lack of memory in the stack, i would appreciate enlightenment.

hello_world.rs

brb updating my LinkedIn profile.

You're passing the address of b to Mescape. b is supposed to be pointing at something, and that value should be passed into Mescape. Right now b is uninitialized so who knows what it contains.

If you want an array of arbitrary size decided at runtime you need to set the value of b to the address returned by a call to (char*)malloc( THE_SIZE_OF_ARRAY_I_WANT_IN_BYTES). It's casted to a (char*) because malloc returns a void pointer.

Alright, I'll explain pointers to you in a way that's simpler than in books.

int a;
printf("%d\n", a + 3);

What is 'a' in this code? Undefined, you didn't give 'a' a value so 'a' + 3 is meaningless.

char *b;
b[0] = 1;

Same thing, 'b' is not a buffer. b is a pointer, it points to an area of memory. That area of memory is undefined.

char a[100];
char *b = &a;
b[0] = 1

Valid, b is a pointer that points to buffer 'a'(static allocation).

char *b = malloc(100);
b[0] = 1;

Also valid, you dynamically allocated a piece of memory using malloc and now b points to what you just allocated.

>// crashes horribly because memory is not allocated i guess?
You guess correctly. b is not assigned to an allocated block.
However, the other guy is only almost right, not quite actually right. b doesn't necessarily point somewhere in the stack. In fact, it's probably null, and if it does point somewhere, it's almost certainly not anywhere allocated in ANY way, stack or otherwise.
In C, pointers have no default value, nor does declaring a pointer allocate space for it. If you want a true dynamic sized array, you can only have it via realloc.

why are you passing char ** b
to a function that expecting char * b ?????

why are you put an array to a char in the stack?

>am i supposed to allocate memory or is there another way?
unless you know the length of the string a, and the amount of the tabulation char, you need to use Dynamic Memory
since your code will generate a longer string from the initial string.

I want to knead yuuki in the face.

I'm a bit confused, why is "Dynamic Memory" the only part of your post with capitalization?

I need a computer that I can use as a dedicated git repository/build machine for my project (a retro fighting game), but I'm pretty poor, and can only spend like $200 max on such a machine + a reliable harddrive to go with it. Would an Odroid XU4 be okay for this? I'll be using SDL + minGW to compile it for my systems.

>poor
>two systems
literally what is wrong with your current computer?

What are you posting on now? If a phone and that budget, go ask Not a meme, you can get something good enough for less than that with a good keyboard and gnu/linux support.

>it's probably null
thought that would never happen, thank you my dude.
soundcloud.com/sytricka/wsg-running-in-the-90s-vaporwave
so i don't need to copy everything from main() to an array within Mescape(), and that is what (appears to be) happening. im aware the compiler hates that though.

I'm always installing stuff on my current computer that could mess with my environment, and if more people ever get involved with my project than that would add even more complications to the mix. A dedicated build machine will rarely have it's environment change, and it can be kept nice and clean with only the files and programs relevant to the project on it.

I'm posting on my computer, but I do have a Moto g4 (Prime version though, so I can't unlock the bootloader). Are you saying that I can use my phone as a dedicated linux/gnu build machine with an external hard drive? Regardless, I'll definitely look into that.

gee user, maybe don't break your distro every week instead of wasting what little money you have left on a shittop.

>so i don't need to copy everything from main() to an array within Mescape(), and that is what (appears to be) happening. im aware the compiler hates that though.

what?

My bad. I didn't even properly look at the link you referred to. I'll look into that for sure.

I never meant to imply that, but yeah you kind of can do it depending on your phone.

I was wondering about:
>my computer
That one you're posting on, why do you need another?

include

void mEscape(char *a, char *b)
{
int j = 0;
int i;

for(i = 0; a[i] != '\0'; ++i, ++j) {
switch(a[i]) {
case 10: // if \n
b[j] = '\\';
b[++j] = 'n';
break;
case 9: // if \t
b[j] = '\\';
b[++j] = 't';
default:
b[j] = a[i];
}
}
b[j] = '\0';
}

void main(void)
{
char *a = "asfw\t\t\n";
char *b = malloc(2*strlen(a)*sizeof(a));//if all the characters in a happen to be tabs or new lines, then b at most only needs twice the number of characters allocated

mEscape(a, b);
puts(&b);
}

Sorry, I have a tendency to write the important parts with capitalization in my notes, and it slipped away while writing this.

>so i don't need to copy everything from main() to an array within Mescape()
No.
If you copy something from main() into other variable inside Mescape(), then when the program exit mescape(), it will be deleted,
or considered as lost / unreachable.
if I were you, what I will do is :
1. change the passed argument to mescape to : void Mescape(char * a, char ** b)
2. get the amount of the tabulating character within string a
3. allocate b with the result of strlen(a) + (amount_of_tabulating_character) before the for loop
4. free b before exiting main.

note: number 3 only works if you want to replace all '\n' and '\t' into '\\' and 'n' / 't', and it will require some extra math to calculate other character transformation.

Whatever you're doing, sizeof(a) is also meaningless for your purpose. It's sizeof(a pointer), which is implementation defined.

Forgot to quote . Also just noticed your puts. I believe it should be:
puts(b);[code/]

Why is /dpt/ so full of beginner level Ctoddlers today?

C is shit.

We've always had a steady amount of CS kiddies and Sup Forumsermin come here and post C shit.

This is the regular state of things, just because people endlessly arguing why rust is/isn't shit doesn't mean people aren't currently learning C.

This.

Not him, but this post intrigue me.
I know it's a bit of convention to use 2^n as buffer for a dynamic array,
where n is the minimum integer as possible, but big enough to keep 2^n bigger than the required array size.

Why it is 2^n?
Isn't it a bit waste of memory for doing it?
Is there any better value to conserve memory but still keeping enough buffer to keep the array expandable?

nevermind user, i saw char ** a where i should have read char ** b. sorry

Is there any way to ease the usage of pass by reference in C?

constantly dereferencing a pointer to change the value is getting annoying quickly,
and it's easy to forgot to dereference it, and causing crash / UB
i'm aware that it's possible to hack it with C preprocessor as:
void (FOO * foo)
{
#define Foo *foo
Foo = //some stuff
#undef Foo
}
But that method is not really scalable and going to need tons of define and undef for multiple variable

Is there any typedef wizardry or some shit to make it easier?

use a lang with pass-by-ref feature like c++?

Not really but usually foo is a struct and you aren't assigning it a value, just setting members.

In the case where you're operating entirely on a pointer value, just set a non-pointer variable to the value and set the pointer at the end of the function.

{
int val = *foo;
/* do shit with val */
*foo = val;
}

Don't use C.

This a meme read?

Yeah, use const *ptr

Design patterns are signs that a language is deficient.

It is literally a book about object oriented memes

that's like saying idioms are a sign that an irl language is deficient

>explain or Ill take it as hogwash.

sizeof(a) isn't meaningless. You can call the sizeof operator for a pointer. He can also use sizeof(char) in its place, if sizeof(a) happens to return the total size of the chars pointed to or the size of the pointer itself.

>Why it is 2^n?
It's not in this case, it's 2*n. To answer your question to the convention of using more dynamic memory than required, its easier to allocate more memory and trim it down as opposed to continually extending your memory. I used 2*n in this case because he's making each tab and carriage (each one char/byte) into two characters (two bytes). No matter what n is, b won't need more than twice n since he at most doubles the number characters in mEscape.
>Isn't it a bit waste of memory for doing it?
Yes. For this program, it doesn't matter too much because the strings are small anyway. In general, allocating extra memory is just a necessary evil.
>Is there any better value to conserve memory but still keeping enough buffer to keep the array expandable?
For this program, was right about counting out the tabs and carriages first then allocating the appropriate memory. That would be more efficient, but its not my program, so I just edited it to run not to be efficient.

> He can also use sizeof(char) in its place
the user you replied to is right, sizeof(a) is senseless there. he probably meant to use sizeof(*a) although it's not needed since sizeof(char) is defined to be 1

I found it meaningless in this context"

2*strlen(a)*sizeof(a)

He's allocating a buffer for a string, how would the size of a (char *) come into play here?

I'm not saying there's no use for sizeof(pointer).

I swear reading this make me think I learned programming walking backwards.

Yes she is, don't be ridiculous. i had a great tome and YOU can bee able too much is not the same.

Your markov chain or english is lacking.

>Can't set direction flag
>Can't even allocate 1 bit
wtf I thought C gives you control. Going back to Java now. I have real work to do.

#include
#include

void Mescape(char *a, char *b)
{
int forward = 0;
int index;

for(index = 0; a[index] != '\0'; ++index) {
b = realloc(b, index + forward + 1); // index and forward are zero'd, so add 1
switch(a[index]) { // using realloc like this is probably inefficient as fuck
case '\n':
b[index + forward] = '\\';
b = realloc(b, (index + ++forward + 1)); // add yet another byte and increment forward
b[index + forward] = 'n';
break;
case '\t':
b[index + forward] = '\\';
b = realloc(b, (index + ++forward + 1));
b[index + forward] = 't';
break;
default:
b[index + forward] = a[index];
}
}
b = realloc(b, index + forward + 1);
b[index + forward] = '\0';
}

void main(void)
{
char *a = "asfw\t\t\n\nkdnjgker\nkasndlgeknrh\nwqkeieqrt\t\n\n";
char *b = malloc(0); // the size does not matter

Mescape(a, b); // are these referenced or is data copied to a dedicated place in memory?
puts(b);
free(b);
}


after reading the posts

#include
#include

void Mescape(char *a, char *b)
{
int forward = 0;
int index;
int alloc = 2; // NULL and index

for(index = 0; a[index] != '\0'; ++index, ++alloc)
if(a[index] < 32)
++alloc;
b = realloc(b, alloc);

for(index = 0; a[index] != '\0'; ++index) {
int i = index + forward;
switch(a[index]) {
case '\n':
b[i] = '\\';
++forward;
b[++i] = 'n';
break;
case '\t':
b[i] = '\\';
++forward;
b[++i] = 't';
break;
default:
b[i] = a[index];
}
}
b[index + forward] = '\0';
}

void main(void)
{
char *a = "asfw\t\t\n\nkdnjgker\nkasndlgeknrh\nwqkeieqrt\t\n\n";
char *b = malloc(0); // the size does not matter

Mescape(a, b); // are these referenced or is data copied to a dedicated place in memory?
puts(b);
free(b);
}