[ / / / / / / / / / / / / / ] [ dir / 1cc / ameta / bbbb / htg / mexicali / strek / tijuana / wai ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Name
Email
Subject
Comment *
File
Select/drop/paste files here
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

File (hide): 13ea14761db7a00⋯.png (18.36 KB, 796x685, 796:685, Untitled.png) (h) (u)

[–]

 No.816812>>816870 >>816958 [Watch Thread][Show All Posts]

Ok so I feel stupid as fuck right now. I'm trying to print a simple sinusoidal function in a console in c++.

Am I even going about it the right way? Can I ever learn to code?

Pick related.

 No.816820

dude

check this out

http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

they even have a man page

`man ncurses`


 No.816870>>816913

>>816812 (OP)

Are you trying to do something like one of these?

#include <iostream>
#include <cmath>
#include <algorithm>
#include <thread>

using namespace std;

constexpr int sz = 20, sz2 = sz / 2;

void version1()
{
char draw_buffer[sz * sz];
fill(begin(draw_buffer), end(draw_buffer), ' ');
for (double res = 0; res < 30; res += 0.1){
int approx_sin = sz2 * sin(res) + sz2,
approx_cos = sz2 * cos(res - sz2) + sz2;
draw_buffer[approx_sin * sz + approx_cos] = 'X';
}
for (int i = 0, ij = 0; i < sz; ++i, cout << '\n')
for (int j = 0; j < sz; ++j, ++ij)
cout << draw_buffer[ij];
}

void version2()
{
char draw_buffer[sz * sz];
fill(begin(draw_buffer), end(draw_buffer), ' ');
for (double res = 0;; res += 0.1){
int approx_sin = sz2 * sin(res) + sz2,
approx_cos = sz2 * cos(res - sz2) + sz2,
index = approx_sin * sz + approx_cos;
draw_buffer[index] = 'X';
for (int i = 0, ij = 0; i < sz; ++i, cout << '\n')
for (int j = 0; j < sz; ++j, ++ij)
cout << draw_buffer[ij];
draw_buffer[index] = ' ';
this_thread::sleep_for(40ms);
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
}

int main()
{
//version1();
version2();
}


 No.816888>>816890 >>816908 >>816916 >>816971

i was playing this this in the challenge thread. runs fine, no compile errors.


#include <stdlib.h>
#include <stdio.h>

int main(void)
{
char *a=malloc(1);
char *b=malloc(1);

a[0]='a';
a[1]='b'; //b-e haven't been allocated
a[2]='c';
a[3]='d';
a[4]='e';
a[5]='f';

b[0]='d'; // why doesn't this overwrite a
b[1]='i';
b[2]='n';
b[3]='d';
b[4]='u';
b[507]='n';
b[508]='u';
b[509]='f';
b[510]='f';
b[511]='i';
b[512]='n';

printf("a:%s\n",a);
printf("b:%s%s\n",b,&b[7]);

return 0;
}

output:


$./a.out
a:abcdef
b:dindunuffin

malloc size is a placebo


 No.816890

>>816888

forgot to change

printf("b:%s%s\n",b,&b[7]);

should be

printf("b:%s%s\n",b,&b[507]);


 No.816908

>>816888

malloc implementations usually allocate from blocks of a uniform size, for efficiency reasons. So malloc(1) is really asking for a block of at least one byte.

you're almost certainly getting a smaller block than the 513 bytes you access, but the rest of it is still mapped into memory, because the malloc implementation expands the heap in large increments


 No.816913>>816922 >>816932

>>816870

Yeah, holy fuck I don't think I'll ever get to this level. Maybe I should just change majors or kms fml.


 No.816916>>816973

>>816888

You don't deserve those trips. The reason why this works was already explained to you in the challenge thread. In explicit detail at that. What the fuck is wrong with you?


 No.816922

>>816913

Maybe you just need practice; e.g. the easy challenges of >>812872 or some programming challenge site like hackerrank.


 No.816932>>816955

>>816913

Nah. Don't quit yet. Identify whether the problem is that you lack a sufficient conceptual understanding of the problem at hand, or that you understand the problem, but you're having difficulty creating a solution with the tool at hand (in this case, C++ and the features it provides).

If the former, you're going to have to work harder to ensure that you have a solid understanding of the problem you're trying to solve before you attempt to tackle it in code. What is it, exactly, that you're trying to do? You may even need to get paper and pencil and work it out visually, by sketching or otherwise. I was once trying to work out a problem that had to do with a cube, so I actually made a physical cube out of paper to help me visualize.

If the latter, you're going to have to work to understand how to translate your understanding of the problem into something amenable to solution with the tool at hand (C++). Try to identify what's going wrong. Have you made incorrect assumptions about the way a C++ feature is working? Break the problem down into smaller parts (if possible) and try to solve them individually.

If you have both problems, well, then you have even more hard work in front of you. But it's not impossible. Try to get enough rest, but spend as much time as reasonably possible working toward a breakthrough.

One final piece of advice, though it's not always applicable. C++ is a high-level language, but many people don't find it as easy as, say, Python or Ruby. Sometimes, it can be beneficial to prototype something in Python before tackling it in C++ (or C, or another compiled language). I was once trying to implement an algorithm I read about in a CS journal in Java, and I thought I had it, but something was totally fucked up. The program compiled and ran, but the results I was getting when I ran the algorithm on a known dataset were totally wrong, and I couldn't tell whether the problem was with my understanding of the algorithm, or my understanding of Java (which I didn't know well). So I tried it in Python (which I was much more familiar with), and quite quickly had the algorithm implemented and got the correct results. It was helpful, because it told me that I understood how the algorithm worked, but something about my understanding of Java was fucked up, and I needed to go back and test and figure out where my understanding of what I was doing in Java had gone wrong. This was ~10 years ago, and I forget what it was, but I did eventually figure it out. I probably saved more time with my Python detour than I would have just staring at the Java code with no idea of how/where I fucked up.

Work hard, and in the end, you'll probably get it. If you enjoy the hard work (or, at least, if the result it worth it), you're good. If you hate it, consider whether you hate it more than you would doing something else with your life. If not, stick with it. Most people hate their jobs.

Good luck.


 No.816955>>816957 >>816961 >>816971

>>816932

>C++ is a high-level language


 No.816957

>>816955

what did he mean by this?


 No.816958

>>816812 (OP)

1. It looks like you are passing degrees instead of radians into sin/cos

2. Swap your x and y variable names. When you go down a line you would expect y to change, not x.

3. Scrap the for loop stuff. You can either replace it with ncurses or use escape codes to move the cursor around in your terminal. For the later approach you print out some blank lines then move the cursor to where the x needs to go and then print it

There's probably a better way to do this, but without knowing the specifications for what you want to do, I can't give a good suggestion.

eg. A cleaner way to do it would be to iterate over all the (x, y) and compute whether it needs to be filled or not.


 No.816961

>>816955

Unless you're one of the "achtually it doesn't extend C at all" autists, C++ is nothing more than additions to C (hence, plus plus) and nearly all of that is firmly in high level language territory. Templates are even functional programming.


 No.816971

>>816888

>// why doesn't this overwrite a

>hurr why writing data to a specific memory location doesn't overwrite the content of another completely unrelated memory location

Protip: add printf("a = %p - b = %p\n", a, b); at the end of your code.

>>816955

It is definitely not as high-level as Haskell or Python, but I wouldn't call a language with templates, lambdas, smart pointers, or even classes strictly "low-level". It ultimately depends of your definitions of low-level and high-level, but I would call it a high-level language with low-level facilities.

Anyway it's an useless semantic debate.


 No.816973

>>816916

i think i have a basic understanding of why it's not fucking up. i posted it in the "dumbass code" thread because i can imagine fucking up this way in a much more subtle accidental way down the line with an off by 1 or something, and it's going to be fun to try to debug when there's no warnings or errors and it'll work fine until it doesn't. i'm sure the shit assigned in the 500+ range could get overwritten at some point, but it's a could, not a guarantee, so if I understand this right if it does get overwritten and fuck everything up, it's not going to be repeatable every time.


 No.816982

#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

const gchar *str = "We must secure the existence of our people and a future for white children. ";
gint direction = 1;
gint cx = 0;
gint slen = 0;

GtkLabel *label;

void
change_dir(GtkLayout *o, gpointer d) {
direction = -direction;
}

gchar *
rotateby(const gchar* str, gint start, gint len) {
gint i, j, iter_len = len;
gchar *newstr = calloc(len+1, 1);

for (i = start, j = 0; iter_len > 0;
iter_len--, i = (i + 1) % len, j++)
newstr[j] = str[i];

newstr[len] = 0;
return newstr;
}

gboolean
scroll_it(gpointer data) {
if ( direction > 0 )
cx = (cx + 1) % slen;
else
cx = (cx + slen - 1 ) % slen;
gchar *scrolled = rotateby(str, cx, slen);
gtk_label_set_text(label, scrolled);
free(scrolled);
return TRUE;
}


int
main(int argc, char **argv) {
GtkWidget *win;
GtkButton *button;
PangoFontDescription *pd;

gtk_init(&argc, &argv);
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(win), "卍 HEIL HITLER 1488 卍");
g_signal_connect(G_OBJECT(win), "delete-event", gtk_main_quit, NULL);

label = (GtkLabel *)gtk_label_new(str);

// since we shift a whole character per time, it's better to use
// a monospace font, so that the shifting seems done at the same pace
pd = pango_font_description_new();
pango_font_description_set_family(pd, "monospace");
gtk_widget_modify_font(GTK_WIDGET(label), pd);

button = (GtkButton *) gtk_button_new();
gtk_container_add(GTK_CONTAINER(button), GTK_WIDGET(label));

gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(button));
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_dir), NULL);

slen = strlen(str);

g_timeout_add(125, scroll_it, NULL);

gtk_widget_show_all(GTK_WIDGET(win));
gtk_main();

return 0;
}

gcc $(pkg-config --libs --cflags gtk+-2.0 pango) -o animtext animtext.c


 No.817001

File (hide): 05c5f69d621fcb8⋯.png (64.98 KB, 1029x728, 147:104, itstime.png) (h) (u)

File (hide): 3821cf8b116bc33⋯.png (17.42 KB, 630x143, 630:143, for.png) (h) (u)

File (hide): 1f7fd39f78222fe⋯.png (3.81 KB, 423x104, 423:104, some.png) (h) (u)

bad code time?


 No.817002

File (hide): bd09b19ced82a79⋯.png (156.11 KB, 793x1525, 13:25, shit.png) (h) (u)

File (hide): af2d919e6a392da⋯.jpg (111.36 KB, 966x1003, 966:1003, code.jpg) (h) (u)

File (hide): 6791acffc88b555⋯.png (5.66 KB, 335x120, 67:24, yo.png) (h) (u)


 No.817010>>817022 >>817023

My generic makefile. Is there a simpler way to do PGO? Using recursive make forces me to seperate user and makefile set C/LDFLAGS (painfully).


TARGET := '$(shell basename -- $(shell pwd))'

CC ?= cc
_CFLAGS := -D_DEFAULT_SOURCE -std=c99 -pedantic -Wall -Wextra \
-fdiagnostics-color=auto
CPPFLAGS := -MMD -MP
_LDFLAGS :=
# If using a ~/local library prefix
# PREFIX := "$$HOME/local"
# CPPFLAGS := -I$(PREFIX)/include -MMD -MP
# _LDFLAGS := -Wl,-rpath $(PREFIX)/lib -L$(PREFIX)/lib
LDLIBS :=

CONFIG ?= Debug

ifeq ($(CONFIG),Debug)
_CFLAGS += -Og -g3 -fsanitize=address,undefined
_LDFLAGS += -fsanitize=address,undefined -rdynamic
else ifeq ($(CONFIG),Profile)
_CFLAGS += -Og -g3 -DNDEBUG
else ifeq ($(CONFIG),Release)
_CFLAGS += -O3 -DNDEBUG
else
$(error CONFIG invalid value; must be "Debug", "Profile" or "Release")
endif

SRC := $(wildcard *.c)
OBJ := $(SRC:%.c=%.o)
DEP := $(OBJ:%.o=%.d)


.PHONY: all clean pgo test

all: $(TARGET)

$(TARGET): $(OBJ)
$(CC) $(_LDFLAGS) $(LDFLAGS) $^ -o $@ $(LDLIBS)

%.o: %.c
$(CC) $(CPPFLAGS) $(_CFLAGS) $(CFLAGS) -c $< -o $@

clean:
rm -rf -- $(TARGET) $(OBJ) $(DEP)

pgo:
$(MAKE) clean
$(MAKE) CONFIG=$(CONFIG) CFLAGS="$(CFLAGS) -fprofile-generate" \
LDFLAGS="$(LDFLAGS) -fprofile-generate"
$(MAKE) test
$(MAKE) clean
$(MAKE) CONFIG=$(CONFIG) CFLAGS="$(CFLAGS) -fprofile-use" LDFLAGS=$(LDFLAGS)

test:
./$(TARGET)

-include $(DEP)


 No.817022

>>817010

I use something simpler, without dependency generation.

CC?=gcc
CFLAGS?=-O2
CPPFLAGS?=
LDFLAGS?=
LIBS?=

PREFIX?=/usr/local
BINDIR?=$(PREFIX)/bin

OUT_PROG?=my_prog
DEBUG?=0

ifeq ($(DEBUG),1)
CPPFLAGS+= -DDEBUG
endif

include src/libshit/Objects.mk
include src/main/Objects.mk

OBJECTS:=\
$(LIBSHIT_OBJECTS)\
$(MAIN_OBJECTS)\
src/main.o

.PHONY: all install uninstall clean

all: src/$(OUT_PROG)
@echo " DONE $(OUT_PROG)"

src/$(OUT_PROG): $(OBJECTS)
@echo " LD $(OUT_PROG)"
@$(CC) $^ -o $@ $(LDFLAGS) $(LIBS)

%.o: %.c
@echo " CC $(OUT_PROG)"
@$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)

install: all
@cp -v src/$(OUT_PROG) $(BINDIR)/
@echo " INSTALL $(OUT_PROG)"

uninstall:
@rm -fv $(BINDIR)/$(OUT_PROG)
@echo " UNINSTALL $(OUT_PROG)"

clean:
@rm -fv $(OBJECTS) $(OUT_PROG)
@echo " CLEAN $(OUT_PROG)"

What benefits does dependency/makefile generation have?


 No.817023

>>817010

PGO is always going to be messy, it's just how it is.

Recursive make is usually how it's done. Hacking on variables should really be done as a configure step so you don't end up with horror. The meme of avoiding autotools is a meme.




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 5
21 replies | 2 images | Page ?
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / 1cc / ameta / bbbb / htg / mexicali / strek / tijuana / wai ][ watchlist ]