21

Today I experienced cruelty of C and mercy of Sublime and SublimeLinter.

So yesterday I was programming late at night for my uni homework in C. So I had this struct:

typedef struct {
int borrowed;
int user_id;
int book_id;
unsigned long long date;
} entry;

and I created an array of this entry like this:

entry *arr = (entry*) malloc (sizeof(arr) * n);

and my program compiled. But at the output, there was something strange...

There were some weird hexadecimal characters at the beginning but then there was normal output. So late at night, I thought that something is wrong with printf statement and I went googling... and after 2 hours I didn't found anything. In this 2 hours, I also tried to change scanf statement if maybe I was reading the wrong way. But nothing worked. But then I tried to type input in the console (before I was reading from a file and saving output in a file). And it outputted right answer!!! AT THAT POINT I WAS DONE!!! I SAID FUCK THIS SHIT I AM GOING TO SLEEP.

So this morning I continued to work on homework and tried on my other computer with other distro to see if there is the same problem. And it was..

So then I noticed that my sublime lint has some interesting warning in this line
entry *arr = (entry*) malloc (sizeof(arr) * n);
Before I thought that is just some random indentation or something but then I saw a message: Size of pointer 'arr' is used instead of its data.

AND IT STRUCT ME LIKE LIGHTNING.

I just changed this line to this:
entry *arr = (entry*) malloc (sizeof(entry) * n);

And It all worked fine. At that moment I was so happy and so angry at myself.

Lesson learned for next time: Don't program late at night especially in C and check SublimeLInter messages.

Comments
  • 2
    haha, nice one!
  • 1
    @Artemix Will definitly look into CLion 😁
    Just one question how heavy it is? I am just asking this question because I use ultrabook and don't have so much power and memory to spare..
  • 1
    @MajorRocki it is light enough
  • 1
    @MajorRocki Clion is kinda heavy to a certain extent but you should be just fine. It's kinda heavy because it has a crap ton of integration and built in stuff. But if you have say 4GB ran and a semi modern CPU of any kind you'll be just fine.
  • 1
    @tankerkiller125 Thanks for the info 😉 yea I have 8GB of RAM. But I am thinking if it even pays of because sublime is really fast and I have a lot of customizations and plugins. Maybe I will give CLion a try sometimes just to see if it pays of.
  • 1
    Just for you to know: You could also use
    entry *arr = (entry*) malloc(sizeof(*arr) * n);
    Beside this, I belief it's a good manner to use a capital first letter for types ("Entry").
  • 1
    My C language prof made us do all our coding via a VPN connected to a server computer at our school. All editing/work had to be done in emacs or VIM. It was rather annoying to debug ;l
Add Comment