These are chat archives for learnclang/general

11th
Jan 2015
Alex Widener
@alexwidener
Jan 11 2015 00:21
Sorry for missing the cast. I don’t have Gitter open all the time, so I don’t see everything right away. Oh well.
Ah, weird. @tokejepsen I just found a (kind of) old e-mail from when you left a comment on my website. Apologies for never responding.
Timm Wagener
@timmwagener
Jan 11 2015 06:52
Couldnt watch the videos live, but can watch them later now since they are on youtube. Big thanks for your effort! :)
Marcus Ottosson
@mottosso
Jan 11 2015 11:05

I don’t have Gitter open all the time, so I don’t see everything right away.

This is indeed a problem I have yet to figure out, it happens to me as well as notifications via email (that I think you should all be getting) doesn't pop up until after some time in the chat.

As an alternative, there is an app for OSX and IOS and looks like Android is getting one too. They'll give you instant updates of messages, like any chat app would. For now, that might be your best bet in staying up to date with events here, as things are moving quite fast!

Marcus Ottosson
@mottosso
Jan 11 2015 11:11

Btw, you can use github releases to attach any binary file to a tag.

Good point! We can go over a few approaches.

  1. Distribution to developers/those familiar with the way of the terminal.
  2. Distribution to computer savvy end-users.
  3. Distribution to our parents.

Each provide an increasing amount of effort on the part of the developer releasing his software out into the wild.

Marcus Ottosson
@mottosso
Jan 11 2015 12:02
Here's an example of how we could go through and build a dedicated web-site for our Terminal Application. :)

www.terminalapp.com

Targeted at #3, our parents. :)
Marcus Ottosson
@mottosso
Jan 11 2015 12:58

Here's a weekend challenge for you.

Signals and Slots in C

How do we do that? Signals and slots (known from the Qt framework) is more or less a version of the Observer pattern and what's more it typically involves objects (read "classes"). As C doesn't provide classes natively, I figured it could be a nice challenge to try and implement it using purely functions.

Here's what I came up with in Python.

"""Observer pattern in Python"""


# Global list of connected signals
subscribers = []


def connect(signal):
    """Helper function for global list of signals"""
    subscribers.append(signal)


def slot(argument):
    """Slot to trigger connected signals, passing `argument`"""
    for subscriber in subscribers:
        subscriber(argument)


# Test signals
def signal_a(message):
    print("SIGNAL A: %s" % message)


def signal_b(message):
    print("SIGNAL B: %s" % message)


if __name__ == '__main__':
    connect(signal_a)
    connect(signal_b)
    slot("Hello")
    # outputs: "SIGNAL A: Hello\nSIGNAL B: Hello"

And there are quite a few tricky subjects involved already.

First and foremost, subscribers is a collection of functions. In Python, functions are first-class citizens just like any variable, but in C I'm not so sure. How can I store and retrieve a function from a collection of sorts?

Marcus Ottosson
@mottosso
Jan 11 2015 14:50

Function Pointers

Yay!

Turns out functions can be passed around just fine using pointers. Apart from some syntactical differences, the approach remained pretty much identical.

#include <stdio.h>

typedef void (*Signal)();

int current = 0;
Signal subscribers[10];


void connect(Signal signal) {
    if(current <= 10) {
        ++current;
        subscribers[current] = signal;
    }
    else
        printf("Sorry, max number of subscribers reached");
}


void slot(char *message) {
    for(int i = 0; i < sizeof(subscribers) / sizeof(Signal); ++i)     {
        Signal subscriber = subscribers[i];
        if(subscriber)
            subscriber(message);
    }
}


void signalA(char *message) {
    printf("SIGNAL A: %s\n", message);
}


void signalB(char *message) {
    printf("SIGNAL B: %s\n", message);
}


int main(int argc, char *argv[]) {
    connect(signalA);
    connect(signalB);
    slot("Hello");

    return(0);
}
Cesar Saez
@csaez
Jan 11 2015 14:58
@mottosso Q: Signal subscribers [10] means there is a maximum of 10 subscribers?
Marcus Ottosson
@mottosso
Jan 11 2015 14:59
Exactly
I haven't figured out how to make arrays of arbitrary sizes..
Do you know if that's advisable even?

From what I gather, there's malloc (that I haven't yet tried) which allocates on the heap as opposed to the stack. But there's three words I really have no idea of what they mean.

  1. Stack is memory reserved for declarations?
  2. Heap is memory reserved for everything else?

But what I haven't figured out is; why is there a separation to begin with?

Alas, bottom line is, having a fixed size of the array resulted in less work.

Cesar Saez
@csaez
Jan 11 2015 15:03
Then it's not exactly the same as the Python implementation... don't get me wrong, I think your code is great, but it's not there yet.
I think we are entering C++ territory (std::vector) ;)
Marcus Ottosson
@mottosso
Jan 11 2015 15:04
Ah, so no way of doing it in C?
Feel free to modify it
And I'm sure my code is not really that great. :D
Cesar Saez
@csaez
Jan 11 2015 15:09
You can roll your own dynamic array in C reallocating the array in memory to fit the new size as needed, but C++ already provides well tested types to do this kind of things.
Marcus Ottosson
@mottosso
Jan 11 2015 15:10
Interesting, how would I go about doing that?
There's also a linked list implementation in learn C the hard way: http://c.learncodethehardway.org/book/ex34.html
Marcus Ottosson
@mottosso
Jan 11 2015 15:16
I fear that's a little above what my mind can muster at the moment
The signals and slots do work, I think an arbitrary amount of subscribers will have to wait until 2.0 :)
Cesar Saez
@csaez
Jan 11 2015 15:56
I agree, low-level code sometimes goes too low... this kind of things makes us realize how much python does for us under the hood (and why it's so slow in comparison, there's a lot of overhead to cover corner cases).
Marcus Ottosson
@mottosso
Jan 11 2015 15:58
Yeah, as I read through your first link there, where he speaks about how it makes us realise why certain things are actually costly to make - like inserting into a list - I thought about that too. Such a simple thing really is a major hassle, isn't it?
But I'm really liking that we're more or less getting a glimpse into the "source code" of the computer, if you will, and can make actual estimations based on actual understanding of what is happening by looking at things like the resulting assembly code along with visualising the contents of memory directly.
Marcus Ottosson
@mottosso
Jan 11 2015 16:21

C Programming: A Modern Approach

image

Have heard it mentioned a number of times as "an alternative to The C Programming Language by K&R", reviews look good, and having just got it it looks like a good (yet heavy 800+ pages) read.

Added to Learning Resources

Marcus Ottosson
@mottosso
Jan 11 2015 17:32

Introduction to Compilers

Now that our next project is underway, the previous project, titled "Hello, World!" has been archived here:

Everything should remain the same, including links as they were all (or at least should have been) relative to the repository. Also feel free to keep adding to the repository, especially if it relates to introducing people to compilation and the various compilers and text-editors; this should be their one-stop-shop to get started on any platform using C.

Marcus

@alexwidener also had the great idea of putting up a listing of projects we've done, so as to make it easier to navigate as we move forwards.

Such a listing have been added to the main Wiki page of the About repository:

As always, the Wiki is open for us all to add to or modify, so should you feel anything is out of place, you are most welcome to make the proper corrections.

Alex Widener
@alexwidener
Jan 11 2015 20:30
Writing up an XCode tutorial right now, does anybody have requests for stuff? I’m keeping it simple enough for Command Line Tools right now.
Marcus Ottosson
@mottosso
Jan 11 2015 20:36
If you can, try keeping the contents and flow of the tutorial similar to the others, like the GCC one, though it isn't 100% necessary as I'm expecting each tutorial to target separate users who wouldn't necessarily try out two different tutorials.
But for those that do, it can be comforting knowing how each tutorial is laid out. I for one am curious to see what it entails. :)
*Even though I'm not on OSX.
Alex Widener
@alexwidener
Jan 11 2015 21:13
Why can’t I show images via Google Drive? The links are shared links.
Marcus Ottosson
@mottosso
Jan 11 2015 21:13
Not sure, but it's probably better you store them on GitHub to keep everything in one place.
Alex Widener
@alexwidener
Jan 11 2015 21:13
Where at?
Marcus Ottosson
@mottosso
Jan 11 2015 21:13
If you drop an image into an empty issue, it'll give you a link to it.
https://github.com/learnclang/about/issues/new
Bit of a hack, but Wiki's doesn't seem to support doing that directly.
Alex Widener
@alexwidener
Jan 11 2015 21:14
That’s weird. Image Uploads have been around for decades.
Marcus Ottosson
@mottosso
Jan 11 2015 21:14
Alternatively, you can also copy the image itself, and paste it into an issue, and it will upload from your clipboard directly
Yeah, probably will come up eventually I suspect, as it's already in Issues
Alex Widener
@alexwidener
Jan 11 2015 21:20
Thanks Marcus.
K. Done.
Marcus Ottosson
@mottosso
Jan 11 2015 21:21

@alexwidener Great work, reading through it now. :)

For the world to see:

Getting setup with XCode

Marcus Ottosson
@mottosso
Jan 11 2015 21:28

I see you used the RPS project as an example in the guide, what would be better is if it could involve the "Hello World" source included in the repo; the project is primarily about absolute beginners getting a first compilation done, the end resulting printing Hello World to the console.

That way, each guide would revolve around the simplest set of building bricks possible to compile a first program. We could then save the more complex things for more complex programs.

What do you think?

Alex Widener
@alexwidener
Jan 11 2015 21:29
I specifically chose the code to illustrate what you need to do when it’s a command line application that needs to be passed in arguments.
Two birds with one stone. It does indeed print, however, you need to pass in arguments so it shows how to navigate in Terminal, run in Terminal, and Build the code as well.
Marcus Ottosson
@mottosso
Jan 11 2015 21:32
That's a little more related to the Screencast repository however, or possibly the next project, the first project is about the absolute minimum and I don't think we want new users to think that the command-line interface is absolutely necessary.
We could minimize the hello world guide, and put additions to the guide into the RPS Wiki?
The RPS project is about command-line arguments after all.
Alex Widener
@alexwidener
Jan 11 2015 21:35
I don’t have time to redo it. I just took an hour and a half out of my day to do that when I have projects that I’m getting paid to do.
Marcus Ottosson
@mottosso
Jan 11 2015 22:10
For the record, me and Alex had a chat, worked it out and are both happy once more. :)