arisuchan    [ tech / cult / art ]   [ λ / Δ ]   [ psy ]   [ ru ]   [ random ]   [ meta ]   [ all ]    info / stickers     temporarily disabledtemporarily disabled

/λ/ - programming

structure and interpretation of computer programs.
Name
Email
Subject
Comment

formatting options

File
Password (For file deletion.)

Help me fix this shit. https://archive.arisuchan.jp/q/res/2703.html#2703

Kalyx ######


File: 1520654689944.jpg (306.66 KB, 866x1248, tumblrorlzl0MSaU1rwszdgo2r….jpg)

 No.1014

This is a thread for people who is starting with programming to ask simple questions, request simple things and post their code to get help.
If your question doesn't have a thread were it fits bring it here.
Remember to do some research before asking a question.

 No.1015

/*C Premier Plus exercise 6-2*/
/*Use nested loops to produce the following pattern
$
$$
$$$
$$$$
$$$$$*/
#include <stdio.h>

int main (void)
{
    char Dollar;
    int Limit, Control, Reference;
    
    Dollar = '$';
    Limit = 5;
    Reference = 0;
    
    printf("%s", Dollar);
    
    while (Reference < Limit)
    {
        Control = 0;
        while (Control < Reference)
        {
            printf("%c", Dollar);
            Control++;
        }
        printf("\n");
        Reference++;
    }
    
    return 0;
}

I want to start this thread this exercise I been working on from the c premier plus.
My compiler gives me:
ejr.c:40:1: error: expected identifier or ‘(’ before ‘while’
 while (i < 6) {
 ^
ejr.c:50:5: error: expected identifier or ‘(’ before ‘while’
     while (Dollar < Limit)
     ^

I don't know what does it expect me to put before the while statements, from what I've looked these errors happen when one forgets the curly brackets but that isn't the case here.

 No.1016

>>1015
are you sure the code you posted is the same as the code you compiled?

 No.1017

>>1016
It should be. I use cygwin to compile and my source code is on the folders cygwin use.
What else could be going on?

 No.1018

>>1017
The errors you posted happened on line 40 and line 50, but the code you posted is only 34 lines long and the two loops are only 2 lines apart.

Try copying the code you posted into a new file and compiling it, do you still get the errors? If yes, how do you exactly compile it? What compiler do you use, what are the parameters you give it, etc. If no, post the actual code you have problems with otherwise we won't be able to help.

 No.1019

>>1018
it compiles now.
I had some code from earlier version of the program laying around on the same document but forgot to tag it as comments so the compiler could ignore them.
This should be a good time to ask what's the best way to keep track of the changes I make on my programs beside keeping the old code on the same document.

 No.1020

>>1019
People use version control for that, like git.

 No.1029

Can anyone explain me in detail why the following C code gives a segmentation fault?
int main() {
    main()
}


Now when a printf() is put in front of the main call inside he function I can see that it must have something to do with the stack getting too big but how does it work exactly?

 No.1030

>>1029
The CPU needs to know where to return after the call finishes so it saves a `return address` to the stack for each call and pops it on return. Your code is filling up the stack with these return addresses since it keeps calling
main
without ever actually returning.

 No.1034

>>1029
>>1030, but you're code here is missing a ';', no?

 No.1036

>>1034
Yes, it does.

 No.1053

How do I use a git/repository?
I was looking at the repositories of projects that interest me and I have no idea how do they work or where to start.

 No.1054

File: 1522781235830.jpg (47.15 KB, 540x399, 1439440025262.jpg)

>>1053
It is best to mess around with git a bit in the terminal.

If the repository is yours, you simply (git) add files/chages to a commit, and then you push that commit so it is merged in the repository.
For repositories that are not yours, you usually fork them, commit to that, and then do a pull request to the main repository so that they will check the changes made in your commit to your fork, and they will decide whether they will use that commit in the repository.

Git is a very advanced tool, originally made by Linus Torvalds himself, so it's a gem.

 No.1125

Does this bit of code (https://ideone.com/lJCFQy or under the spoiler) uses dynamic dispatch? like with vptr or something
As I understand, at line 13, compiler cannot possibly know at compile time which implementation of (==) to call, yet the code compiles and produces correct results.
Can someone please explain, what kind of implementation lies behind this?

    {-# LANGUAGE ExistentialQuantification #-}
     
    data Value = A Int
     
    data ForallFunc = forall a. Eq a => Forall (Value -> a) 
     
    unpackA (A int) = int
     
    equalityTest :: Value -> Value -> ForallFunc -> Bool            
    equalityTest arg1 arg2 (Forall unpacker) =
      let a1 = unpacker arg1
          a2 = unpacker arg2 in
        a1 == a2
     
    main = putStrLn $ show $ equalityTest (A 1) (A 1) (Forall unpackA)

 No.1127

>>1125
In your example the compiler could run the whole thing at compile time and just put the result into the executable since there's no external input. In fact, if you run GHC with at least
-O1
, main is optimized into
main = putStrLn $ show $ True

Now, to actually answer your question, it is dynamic dispatch, the compiled function has extra parameters that contain type information which is supplied to it at the call site, where the type is known. == uses this information to determine what to do.

 No.1129

>>1029
Short: The program resulted in an infinite recursion. The compiler adds code that puts some value on the stack despite not having any local variables. The OS detected the stack as growing beyond a limit and sent a signal for Segmentation fault.

In the x86 assembly prologue of every function, the program first stores the return address (if you called a function within a program, the computer must know where to go after the function is done, hence the return address) on the stack, as well as the memory address denoting the beginning of the caller function's stack (makes it easy to access function parameters among other purposes). It then moves on to the code between the curly braces. After it's done with everything within the curly braces, the stack base pointer is popped off the stack to be used by the caller function if there is any, followed by the return address, to continue execution.

In your case, your main method calls itself with no base case, so the computer kept calling the main function, pushing the return address and the stack base pointer onto the stack, then rinse and repeat (or atleast until the OS detects the stack overflow)

 No.1130

>>1129
In x86 the
call
instruction saves the return address on the stack, which is always the next instruction after the
call
. Even if you wanted to save the return address on the stack manually without using
call
and instead jumping around, you would need to save it from the calling function, since the called function has to be general enough to be callable from anywhere and for this reason cannot know from where it was called from/where to return unless the caller tells it.

 No.1131


 No.1132

>>1131
It says that you can use it for dynamic dispatch, but it doesn't say how is it implemented. For all we know it could specialize it for every call-site like C++ templates or do some kind of black magic.

 No.1326

whats a good book on C to find if I know literally nothing about programming and have only done a few minor bash scripts?

 No.1337

>>1326
C Programming Language. It's an old standard but this book will get you started pretty quick, and it's always better learning from the creators themselves

 No.1338

Hey Arisu!

I have a variable (a integer for example) which I want to modify using my own function. My current code looks like this:
 variable = myFunction(variable, parameter);

But to me this looks like something a noob like me would do, I never see this when reading "proper" code on GitHub. I was thinking of
variable.myFunction(parameter);

but I seem to not know the right name for this since me throwing it at search engines does not give any answers.

I am programming this for my Arduino, which is basically C++. I know this is a pretty simple question, but my knowledge in the programming field is very random due to me being a unorganized learner.

 No.1339

>>1338
Look into object-oriented programming for the particular construct you're referring to, although using pointers may be preferable on embedded platforms like Arduino. For example, the following piece of code assigns parameter to variable.
void myFunction(int *ptr, int param) {
    *ptr = param;
}

/* ... */
myFunction(&variable, parameter);


 No.1341

>>1338
Not that it would be helpful for arduino programming. But if you want to do it for something else you're looking for a class method. As mentioned in >>1339

You'd probably want to look into object oriented programming. but it would look something like this

class variable {
   public:
      variable myFunction(parameter) {
      // function definition
      return variable;
       }
}


but writing with the equals sign is fine and with arduino you can't have C++ classes anyway.

 No.1402

I want to start learning about coding bots, for now at least very simple ones to use with binance (crypto trading), my question is, should i go with python or js + node? I've seen that people rather use python, but im nore interested in node+js but i dont know if it will do the job (graphic calculation) sure, there will be frameworks but i also dont want to hardcode everything.

Thx for the help chums

 No.1403

>>1402
> interested in node+js
why are you interested in javascript?
what are the differences betwen python and js you find important?

>hardcode

>(computing) To build absolute and unchangeable values into a program such that they can only be changed by modifying the source code.
Neither language would force you to hardcode anything, if that's what you mean.
or maybe you meant code everything yourself?


Overall, they are pretty similar languages. However;
Python has more variety of libraries, whereas JS tends to be more focused on web browser related things.
I would say Python is better designed, although they both have their +s and -s.

so i would suggest python myself.

 No.1425

Do you know good resource about asm?

 No.1426

>>1425
For which CPU?

 No.1427

>>1426
Since he doesn't name any I assume it does not matter and he just wants to start with ASM somehow.

That said one should look at easy CPUs that are well documented to start learning, like the 8502 (Commodore), Z80 (Gameboy) or the 2A03 (NES). For all of these you should be able to find lots of tutorials, tools, communities and so on. Just throw the terms into your favorite search engine and see what comes up.

If you have more specific questions please ask!

 No.1428

>>1427
>>1426

x86 for window

 No.1431

>>1427
Exactly those CPUs are indeed the best and easiest to begin with. Very easy, not too many registers and yea, well documented. If you get a GB and a flashcard you even have some haptic fun with it, without being too theoretic and just dry testing in an Emulator. Feels very good to see your program running on the real hardware. <3
Even when it's only a stupid Hello World for the very beginning (even that might not be that trivial).

 No.1436

what is good book your learn ruby fron

 No.1438


 No.1530

Right now i'm going through Adam Osborne's Introduction to Microcomputers volume 1. I already read volume 0. I'm confused about RAM and ROM addresses. What I understand is that addresses are split into word address bits and chip-select bits. The word address is one memory word, so I think it's as large as the microprocessors's word size. Not sure though. I don't understand how chip-select bits work. If you have 16-bit addresses, but the chip select bits take up 6 bits, doesn't that limit the word size? If you've got say 64k of ram and 1024 x 6 chips are used, how would the chip select bits look? I've been reading the same two pages without understanding this any better.

 No.1531

>>1530
I think i've figured it out.

 No.1694

File: 1564946770860.jpg (896.63 KB, 1487x2000, large-hiroshiyoshida-morni….jpg)

>>1014
Hope this is the right place to ask this qeustion.
I'm trying to install Searx fallowing the instructions from this video:
https://www.youtube.com/watch?v=fKOorVJzQas

and once a reached this step:
./manage.sh update_packages

im getting the output:
./manage.sh: 19: ./manage.sh: pip: not found

whats going on? how can i fix this

also heres a timestamped link to the point in the video im at: https://youtu.be/fKOorVJzQas?t=237

 No.1695

>>1694
You need to install pip. What GNU+Linux distro are you using?

 No.1696

>>1695
devuan

 No.1699

>>1694
apt-get install python-pip



[Return] [Go to top] [ Catalog ] [Post a Reply]
Delete Post [ ]