Installing GitLab 5 on CentOS 6.2

You now have no excuses to not be using git for your projects.
Beside great FREE git hosting services and paid self hosted solutions like github and bitbucket there is also the option to go self hosted for FREE

You can have full control over your servers and environment and only allow access to your projects to the people involved in it.

GitLabs is a full fledged open source self hosted git management solution.

The installation of gitlabs is very straight forward.
Here are the steps that I followed:

1. Download

The folks at gitlabs published a script to install gitlabs on Ubuntu but not on CentOS.
However, thanks to Mattias Ohlsson, that compiled all the notes about how to install gitlabs on cento, now there is a script that installs from top to bottom gitlabs on centos.
You can find the script here:
Or try the steps manually following these notes: https://github.com/gitlabhq/gitlab-recipes/tree/master/install

So the commands you need to run are:

wget https://raw.github.com/mattias-ohlsson/gitlab-installer/master/gitlab-install-el6.sh

*I edit the file and set the mysql password manually instead of leaving up to the script to create one. If you don’t care about the mysql pass just leave the script the way it is that later it will spit out which password it chose for the mysql db

2. Install

chmod +x gitlab-install-el6.sh
HOSTNAME=yourhostnamehere ./gitlab-install-el6.sh

It will take a few mins and after it is done you will have a working version of gitlabs on your machine \o/

3. Fix issues

The only problem I had was with the path to ruby.
The script completed fine, without any errors, however when I tried to push a repo to the new server I got this error message:

/usr/bin/env: ruby: No such file or directory

The server was not finding the path to ruby.
To solve it was pretty simple.
I just added ruby to the $PATH.

cat > /etc/profile.d/root.sh << EOF
export PATH=/usr/local/rvm/src/ruby-1.9.3-p392:$PATH
export PATH=/usr/local/rvm/src/ruby-1.9.3-p392/bin:$PATH
EOF

source /etc/profile.d/root.sh

After running the commands above you should be able to push without any trouble to your gitlab server.


Dependency injection with Node.js

In the last project I was working on I had the chance to apply some dependency injection patterns on a node.js application.
Before I get into the details of the implementation it is important to understand how using dependency injection could benefit your project.

Wikipedia’s definition

Dependency injection is a software design pattern that allows removing hard-coded dependencies and making it possible to change them, whether at run-time or compile-time.[1]

This can be used, for example, as a simple way to load plugins dynamically or to choose stubs or mock objects in test environments vs. real objects in production environments. This software design pattern injects the depended-on element (object or value etc) to the destination automatically by knowing the requirement of the destination. Another pattern, called dependency lookup, is a regular process and reverse process to dependency injection.

Basically, dependency injection gives you the flexibility to separate the module’s functionality from it’s dependencies.
This decoupling can come in handy during testing or even when you find yourself in the need to modify some dependencies of a module later on.

Creating the module

Lets look at how you would be able to implement some dependency injection patterns with node.

I’m going to use the WebVirt project to show some examples in action.

The code blow represents a single controller that manages some express routes:

var VirtController = function (di) {

};

VirtController.prototype.actions = function (req, res) {

};

VirtController.prototype.hostStats = function (req, res) {

}

VirtController.prototype.list = function (req, res) {

};

module.exports.inject = function(di) {
   if (!_virtController) {
    virt = di.virtModel
    Step = di.Step;
    _ = di._;
    logger = di.logger;
    _virtController = new VirtController(di.config.logger);
  }

  return _virtController;
}

The controller has three basic methods:

  • actions
  • hostStats
  • list

However, only the inject method is exported.
That’s the only entry point of the module, you can perform some validation, initialization procedures, anything that needs to be done before you instantiate the module.

In the example above we only check if an instance was already created so we don’t create two equal objects, applying the Singleton pattern.

Injecting dependencies

To use the module all we need to do is to “inject” the dependencies and receive back the initialized instance:

// Load dependencies
var _ = di._ = require("underscore");
di.Step = require('../../external/step/lib/step.js');
di.exec = require('child_process').exec;
di.config = config = require('../../config/config.js');
di.logger = logger = require('../../utils/logger.js');

exports.virtModel = di.virtModel = require("./models/virt-model.js").inject(di);

exports.virtController = virtController = require("./controllers/virt-controller").inject(di);

One of the major benefits we gained by applying dependency injection into our project was that gave us the flexibility to quickly identify what the module needed to operate on, and if any changes were needed we could quickly patch them.
For example;
The WebVirt project is composed of two different pieces, the WebVirt-Manager and the WebVirt-Node.
They are separate modules that share the same code base but are designed to run on different hosts. Each one of them have specific dependencies.
The WebVirt-Manager requires Redis to store the users of the system as well other bits of data.
However the WebVirt-Node does not need Redis.
That posed a huge problem since both apps were sharing the same code base and we were using a Logger module that was saving the logs to a Redis db.
And only the WebVirt-Manager host had a Redis db running.

To fix this problem we passed a “Custom Logger” to the WebVirt-Node.
Instead of requiring the Logger that was talking with the Redis db, we passed a Logger that only logged stuff to the console.

// Load dependencies
var _ = di._ = require("underscore");
di.Step = require('../../external/step/lib/step.js');
di.exec = require('child_process').exec;
di.config = config = require('../../config/config.js');
var logger = {
  error: function (err, metadata) {
    console.log("err: ", err);
    console.log("medatata: ", metadata);
  }
}
di.logger = logger;

exports.virtModel = di.virtModel = require("./models/virt-model.js").inject(di);

exports.virtController = virtController = require("./controllers/virt-controller").inject(di);

And by just changing a few lines of code we were able to modify the module’s dependencies without altering it’s functionality.


DPS915 Workshop 1 – Initial Profile

Int the first workshop for the DPS915 course(Parallel Programming Fundamentals) we had to profile a simple application.
I wrote a previous blog post listing the steps to profile an application on osx.

The application we had to profile was:

// Profile a Serial Application - Workshop 1
 // w1.cpp

 #include <iostream>
 #include <iomanip>
 #include <cstdlib>
 #include <ctime>
 using namespace std;

 void init(float** a, int n) {
     float f = 1.0f / RAND_MAX;
     for (int i = 0; i < n; i++)
         for (int j = 0; j < n; j++)
             a[i][j] = rand() * f;
 }

 void add(float** a, float** b, float** c, int n) {
     for (int i = 0; i < n; i++)
         for (int j = 0; j < n; j++)
             c[i][j] = a[i][j] + 3.0f * b[i][j];
 }

 void multiply(float** a, float** b, float** c, int n) {
     for (int i = 0; i < n; i++)
         for (int j = 0; j < n; j++) {
             float sum = 0.0f;
             for (int k = 0; k < n; k++)
                 sum += a[i][k] * b[k][j];
             c[i][j] = sum;
         }
 }

 int main(int argc, char* argv[]) {
     // start timing
     time_t ts, te;
     ts = time(nullptr);

     // interpret command-line arguments
     if (argc != 3) {
         cerr << "**invalid number of arguments**" << endl;
         return 1;
     }
     int n  = atoi(argv[1]);   // size of matrices
     int nr = atoi(argv[2]);   // number of runs

     float** a = new float*[n];
     for (int i = 0; i < n; i++)
        a[i] = new float[n];
     float** b = new float*[n];
     for (int i = 0; i < n; i++)
        b[i] = new float[n];
     float** c = new float*[n];
     for (int i = 0; i < n; i++)
        c[i] = new float[n];
     srand(time(nullptr));
     init(a, n);
     init(b, n);

     for (int i = 0; i < nr; i++) {
         add(a, b, c, n);
         multiply(a, b, c, n);
     }

     for (int i = 0; i < n; i++)
        delete [] a[i];
     delete [] a;
     for (int i = 0; i < n; i++)
        delete [] b[i];
     delete [] b;
     for (int i = 0; i < n; i++)
        delete [] c[i];
     delete [] c;

     // elapsed time
     te = time(nullptr);
     cout << setprecision(0);
     cout << "Elapsed time : " << difftime(te, ts) << endl;
 }

We had to run the application with 12 different combinations to see how much time the program spent executing the “add” and “multiply” functions.

Here is the profile results:

To easy the process of generating the profile data, I create a bash script to automate the runs:

#!/bin/bash

# First Set
N[0]=80
NR[0]=50

N[1]=160
NR[1]=50

N[2]=320
NR[2]=50


# Second Set
N[3]=80
NR[3]=100

N[4]=160
NR[4]=100

N[5]=320
NR[5]=100


# Third Set
N[6]=80
NR[6]=200

N[7]=160
NR[7]=200

N[8]=320
NR[8]=200


# Fourth Set
N[9]=80
NR[9]=400

N[10]=160
NR[10]=400

N[11]=320
NR[11]=400


if [ $(uname) = "Darwin" ]
then
OS="mac"
  CC="g++-4.7"
else
OS="linux"
  CC="g++"
fi

echo "OS $OS"

OPTIONS="-std=c++0x -O2 -g -pg"
OBJ="w1"
SRC="w1.cpp"

INSTRUMENT_TEMPLATE="/Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/Resources/templates/Time Profiler.tracetemplate"
#compile workshop
$CC $OPTIONS -o $OBJ $SRC

#generate profile info
for i in {0..11}
do
echo "Running ${i}th set"
  if [ $OS = "mac" ]
  then
echo "Running on MacOS"
    instruments -t "$INSTRUMENT_TEMPLATE" -D results/mac/"${N[$i]}x${NR[$i]}.log" $OBJ ${N[$i]} ${NR[$i]}
  else
echo "Running some linux distro."
    ./$OBJ ${N[$i]} ${NR[$i]}
    gprof -p $OBJ > "results/linux/${N[$i]}x${NR[$i]}.log"
  fi
done

The script works both on mac and linux.
If it’s running on a mac, it uses the Instruments Time Profiler, on a linux distro it uses gprof.

I’m committing all my course work to github

Any suggestions are more than welcome :)


Using Instruments Time Profiler

Gprof problem

On OSX 10.8.1 (Mountain Lion) the gnu profiling tool wasn’t working.
I’ve looked it up online and there was very little documentation about the problem.
I read in a couple of places saying that gprof in fact didn’t work but I couldn’t find any final answers.
Basically what happened is that when the program was compiled with the “pg” option, the gmon.out file was not created, thus not being able to run gprof to gather profile information for a specific program.

At first I thought the problem could be related to the fact that I was running gcc 4.2.1(the one that comes by default with XCode) so I tried to compile the latest version of gcc from source to check if it solved the problem.
I compiled gcc version 4.7.1. However it didn’t fix the problem.

I even try linking the profiling lib manually, but the gmon.out file was not being created.

**I’m still trying to find why the gmon.out file wasn’t being created, if anybody knows the reason or have any suggestions please leave a comment below.
My next step will be to compile the libc from source to add some profile symbols.
I’m following these references:

A couple of resources that are not related to gprof but nevertheless very useful:

 

Time Profiler

With all that being said, I needed to profile a c++ program on the mac, so I went looking for alternatives.

Luckly, I found that XCode comes with some extra tools called Instruments
A few tools included in the Instruments toolset are:

  • Allocations
  • Leaks
  • Activity Monitor
  • Time Profiler
  • System Trace
  • Automation
  • Energy Diagnotics

To get started with the Time Profiler is very simple, you first need to create a Xcode project.

Select the Profile option under Product (Command + I)

Select the Time Profiler template

Finally it will display the profile of your application

So far so good, I managed to generate profile information for my application. However, what if I wanted to get the information via the command line?
In my case I had to run the same application several times with different arguments to inspect how some functions behaved in certain situations and if they needed some optimizations.
With that in mind, running the time profiler via XCode was out of the question since I would need to manually modify the arguments and run the profiler each single time.
Instead I created a bash script to automate the runs.

Now I needed to find how to run the Instruments Time Profiler via the command line.
It wasn’t easy, there is very few documentation online and the manual has some outdated information.
Instead of [-d document] the correct is [-D document]
Anyway, to run Instruments from the command line:

instruments -t PathToTemplate -D ProfileResults YourApplication [list of arguments]

To see a list with all the available templates:

instruments -s

The result is a trace file that will contain the information regarding the profiling of the application.


Building Firefox on Mountain Lion 10.8

All the work that I’ve done on Firefox so far has been on a linux box.
I bought a mac recently so I’m in the process of switching all my dev tools.
To build Firefox on a mac is almost as straight forward as building on a linux distro.

Here are the steps:

1.

First you’ll need to install macports.
Download the pkg installer for Mountain Lion or whatever version you are running and install macports

After the installation you’ll need to restart your shell so the $PATH gets updated.
You can find more details here

Once macports is installed:

$ sudo port selfupdate
$ sudo port sync
$ sudo port install libidl autoconf213 yasm mercurial ccache

The commands above will install all the dependencies you need to build firefox.

**More info on how to configure ccache here

2.

Next it’s time to checkout the source code.

hg clone http://hg.mozilla.org/mozilla-central

It might take a while to clone the whole repo.

3.

Now that you have both the dev dependencies and the source code the last thing missing is a .mozconfig file.
Below is a default configuration:

ac_add_options --enable-debug
ac_add_options --enable-trace-malloc
ac_add_options --enable-accessibility
ac_add_options --enable-signmar

# Enable parallel compiling
mk_add_options MOZ_MAKE_FLAGS="-j12"

# Treat warnings as errors in directories with FAIL_ON_WARNINGS.
ac_add_options --enable-warnings-as-errors
ac_add_options --with-ccache

# Package js shell.
export MOZ_PACKAGE_JSSHELL=1

You can find more info about .mozconfig here

4.

Now it is time to start building.

First run:

make -f client.mk configure

That will make sure everything is setup properly, if you don’t see any error messages then you can start the build:

make -f client.mk build > build.out

A trick is to redirect the output of make to a file, it not only makes it easier to spot errors but it also decreases the build time.

Depending on your computer the build might take some time, don’t expect the build to finish before 15min, it will probably take something between 30min to 2h

5.

Once the build is done, you can run Firefox by going to dir obj-dir/dist/NightlyDebug.app/Contents/MacOS and launch the firefox executable.

References:
Simple Firefox build
Mac OS X Build Prerequisites


Getting started with CUDA on OSX 10.8 – Driver Problems

To install all the dev dependencies for CUDA enabled GPUs is not that bad, I faced a few issues but overall the documentation is pretty good.

You can find more information about how to get started here, it has all the links for the download of the driver + toolkit + SDK for windows, linux and mac

They also posted a PDF giving detail instructions about how to install everything.

 

Road Blocks

I’m running a MacBook Pro 2012 that comes with a GeForce GTM 650M.
On their website, they have the driver version 4.2 for download. However, I can update the CUDA driver to version 5.0.24 through the CUDA Preferences window under the System Preferences tab.

So after following the instructions they have posted on the Get Started pdf, I would get the message “Driver not supported” when running the deviceQuery test script.
I looked up online and found that this problem usually happened when the driver had a lower version than the SDK, I thought it was weird since I had downloaded all files they had instructed on the website.

I started browsing on the System Preferences when I saw the CUDA preferences tab.
On the tab it had the option to update the driver.
After the update, my driver was on version 5.0.24, and the deviceQuery test would work. \o/

After running the deviceQuery test, they suggested to run the bandwithTest to make sure the communication with the GPU was working properly.
To my surprise, when I ran the bandwithTest the computer crashed, some weird noises came from the case and a kernel panic messaged appeared.


Interval Since Last Panic Report:  75 sec
Panics Since Last Report:          2
Anonymous UUID:                    CD3F065C-4392-433E-8B7B-9D466743EE14

Tue Sep 11 23:16:23 2012
panic(cpu 4 caller 0xffffff802e8b7b95): Kernel trap at 0xffffff7faef9d18e, type 14=page fault, registers:
CR0: 0x0000000080010033, CR2: 0xffffff8191902000, CR3: 0x000000006b34b06c, CR4: 0x00000000001606e0
RAX: 0xffffff815123d000, RBX: 0x00000000406c5000, RCX: 0x00000000101b1400, RDX: 0xffffff8043302374
RSP: 0xffffff815117b650, RBP: 0xffffff815117b650, RSI: 0xffffff8043302004, RDI: 0xffffff80432ff804
R8:  0x00000000003f6a01, R9:  0xffffff815117b664, R10: 0x0000000000ffffff, R11: 0xffffff8100d10004
R12: 0xffffff80432ff804, R13: 0xffffff8043302374, R14: 0x0000000000000000, R15: 0xffffff8043302004
RFL: 0x0000000000010206, RIP: 0xffffff7faef9d18e, CS:  0x0000000000000008, SS:  0x0000000000000010
Fault CR2: 0xffffff8191902000, Error code: 0x0000000000000002, Fault CPU: 0x4

I wasn’t sure if the kernel panic was connected with the driver update, so I went back and ran some other scripts that come with the CUDA SDK, I ran the particles, simpleGL, volumeRender and a few others, then to my surprise again, when I ran the mergeSort another kernel panic was generated.
By now I was starting to get worried, I went back to the scripts dir and run a few others to make sure my GPU was still functioning properly, I ran the particles, simpleGL, volumeRender and the clock script, and again, after starting the clock script another kernel panic.

Now I knew for sure something was wrong, that shouldn’t be happening.

It was almost 12pm and I was getting tired and frustrated.
I did the only logical thing left to do… google it.

I entered the search: “mac 2012 crash with cuda driver 5″

 

Solution

To my relief it appeared that the kernel panics were in fact a known problem with the CUDA driver version 5 for the MacBook pro 2012.
I found this post on Adobe’s blog explaining the issue.
Apparently having the “Automatic Graphics Switching” option enable causes some CUDA applications to crash.
Turning the option off solves the problem.

Without the automatic graphics switching ON I ran the bandwithTest, mergeSort and clock apps and they worked just fine.

That Adobe’s blog post was created on August 29, so I believe that a fix for this problem should be coming out very soon.
Only Mountain Lion (Mac OSX v10.8) and Lion (Mac OSX v10.7) are affected by this bug.


Semester Roadmap

A new semester just began this week, and right now it looks to be one of the most challenging/exciting so far.

Bellow is my course list:

DPS915 Introduction to Parallel Programming
BTN710 Information Security
BTB720 Marketing Principles and Practices
BTS730 Project Management Methodologies
BTH740 Human Factors in Computing
CPP700 Co-op Integration and Career Planning

(I’m enrolled on the Software Development Degree at Seneca College)

I’m really looking forward for the “Introduction to Parallel Programming” and “Information Security”
The plan is to blog about both courses.
I’ll keep updating this post with the latest info about them.

On the DPS915 course, we will be learning how to run programs on the GPU using CUDA, by the end of the semester we’ll need to demonstrate the optimizations an application can have by porting some of the most heavy calculation areas to run on the GPU instead of the CPU.
My friend Simon(who’s also taking the course) has been working on a project to create md5 hashes for all possible combinations of words. Before, he faced some problems to generate the hashes, since the more chars in the words, the amount of time that it took to generate them was starting to become unfeasible.
The datastorage for all the hashes was also proving to be a problem.
The idea is to utilize the power of the GPU and leverage the amount of time it takes to generate the hashes.
For the data storage we are considering using redis, but we are still looking for other possibilities.
We’ll try to document the whole process of the project here.

A couple of other topics I’m also going to be blogging about is Blackberry10 Development and NodeJS+MongoDB.
Right now we are about to release the second iteration of Sobol, a construction management tool, and getting ready for iteration 3.
We have a lot of cool ideas to implement on the project and I want to document them here.

The new line of Blackberry devices will be coming out early next year, and RIM has been giving great support and incentive to developers. They redesigned all their APIs and the fact that the new BB10 platform is built on top of QNX makes even easier to port other developer tools.
The technologies supported are WebWorks, Cascades, Native and Java
WebWorks is the Javascript+Html5+Css stack
Cascades is the UI framework built on top of Qt
Native is C/C++
And since it’s possible to run Android apps on BB10 Java apps are also supported.

We’ll be focusing on Cascades and WebWorks development, and we already have a few apps on the oven that we plan to release in the coming weeks.

Finally, I also want to keep contributing to the Mozilla community, and will keep working + blogging about Firefox bugs.

Buckle up because this will be a hell of a semester!


Trimming spaces and comments in C

This is a simple C function that reads a line from a file and trim all comments and spaces.
You can see that the function receives a pointer to a file, a pointer to a char and an integer holding the size of the line to be read from the file.
The function then returns a line from the file without spaces(leading/trailing) and comments

Function signature:

char * readLine (FILE* fp, char* line, int size)

One thing I want to point out is the fact that it is possible to trim the string without creating a new one.
By using pointer arithimetics you can manipulate the chars of the line and remove anything you want.

For example, lets say you have a string like this:

***I’m using 0x00n just as an example to demonstrate the memory location of each char.

So the string A1 has two leading and trailling spaces.
Assuming that the line read from the file is: ” a1 “

1.

First we remove the comments, none is this case:

s = strchr(line, '#');
if (s) *s = '\0';

We use strchr to search the string for any occurrences of #

If # is found in the string we set the null byte to the position of the first #

2.

The next step is to remove all the trailing spaces:

s = line + strlen(line) - 1;
while (isspace(*s)) s--;
*(s+1) = '\0';
  • First we assign to s the position of the last char in the line string.
  • Second we check if the char is a space using the isspace function(it checks not only for spaces, but for other delimiters as well). If the char is a space we subtract one from the s, meaning we subtract a char from s, setting s to point to one char before.
  • Once we find a char that’s not a space we break the loop.
  • Finally, we add one to s, and set the null byte to the position of the first space after the string ends.

3.

To remove leading spaces is even simpler:

s = line;
while (isspace(*s)) s++;

We set s to point to the first char in the string read from the file.
Then we loop through the string checking if the char is a space and incrementing the pointer by 1.

After all the trimming, s will point to the first non space char in the string, and the null byte will be positioned right after the last non space char. It will also have all the comments removed.

One thing to notice with this approach, is that the function must receive a char* and return a char*.
The reason beginning is that the char* needs to be declared in the function that is calling readLine, in this example the main. Since if not, the scope of the char* would be tight to the readLine function and thus the calling function would not be able to access the trimmed string.

Another possibility could be to manipulate the char* line itself, removing the necessity to return a new char.

This trim function could be adapt to not only work with files, but with different data structures as well.

If you have any suggestions or tips please leave a comment bellow :D

Full Source Code:

#include
#include
#include

char *
readLine (FILE* fp, char* line, int size)
{
    char* s = NULL;

    while (!feof(fp) && fgets(line, size, fp)) {
        // Strip comments
        s = strchr(line, '#');
        if (s) *s = '\0';

        // Remove trailling spaces
        s = line + strlen(line) - 1;
        while (isspace(*s)) s--;
        *(s+1) = '\0';

        // Remove leading spaces
        s = line;
        while (isspace(*s)) s++;

        // Don't return empty lines
        if (*s) return s;
        printf("empty line\n");
    }

    return NULL;
}

int
main (void)
{
        FILE* fp = NULL;
        char line[256];
        char* s = NULL;
        fp = fopen("file", "r");
        if (!fp) return 1;
        while ((s = readLine(fp, line, sizeof(line)))) {
                printf("s: %s. || line: %s.\n", s, line);
        }
        return 0;
}

Test file:

#Some comments
start
  a1
  a2
  a3 #other comment
end

start
  b1
  b2
  start
    c1#comment....
    c2
  end
end

typedef & C structs

Recently I’ve been working mostly with C code and one thing I noticed over and over was that most of the structs were declared with a typedef..

For example:

typedef struct {
    int a;
    int b;
} my_struct_t;

instead of:

struct my_struct {
    int a;
    int b;
};

The main difference between the two declarations is that the one with the typedef creates a new type called my_struct_t and the latter creates a tag called my_struct, not a type.

So this code would be valid:

struct my_struct my_struct;

it is creating a variable with the name my_struct that has a type of
struct my_struct. The compiler treats tags and types differently.

Using typedef when declaring structs besides saving some keystrokes makes the code easier to read since you don’t need to explicit say the keyword struct everytime you want to refer to your struct.

So instead of typing:

struct my_struct some_function(int a, struct my_struct);

you can reference struct my_struct by its new type:

my_struct_t some_function(int a, my_struct_t);

Another important thing to mention, is if you need to reference the struct you are declaring as one of its own members, for example in a linked list.

typedef struct S1 {
        int a;
        int b;
        struct S1 *s;
        struct S1 s; // error: field ‘s’ has incomplete type
        S1_t *s; // error: ‘S1_t’ does not name a type
} S1_t;

The error “field ‘s’ has incomplete type” happens because one of the members of the struct is the struct itself, so the compiler looks up for the struct S1 type but it can’t find, since it has not been declared yet.

The same thing happens if you try to reference the name giving in the typedef, in this case S1_t, S1_t represents a struct S1. However during the declaration of struct S1, S1_t doesn’t exist yet.

The solution is to create a pointer to a struct S1.
A pointer points to a memory address, it doesn’t matter the type of the data the pointer is pointing to, the memory address will always have the same size, so the compiler knows how to interpreter during compilation time.

Now at runtime you can allocate memory for a struct s1 and assign to the s pointer.
The only thing the compiler will check is if the memory block represents a struct s1, since it knows what a struct s1 looks like.

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

typedef struct S1 {
        int a;
        int b;
        struct S1 *s;
        // struct S1 s; // error: field ‘s’ has incomplete type
        // S1_t *s; // // error: ‘S1_t’ does not name a type
} S1_t;

int main (void) {
        S1_t s1;
        S1_t s2;

        s1.a = 1;
        s1.b = 2;

        s2.a = 3;
        s2.b = 4;

        //s1.s = &s2;
        s1.s = (S1_t*) malloc(sizeof(S1_t));
        s1.s->a = 3;
        s1.s->b = 4;

        printf("s1.a: %d\n", s1.a);
        printf("s1.b: %d\n", s1.b);
        printf("s1.s->a: %x\n", s1.s->a);
        printf("s1.s->b: %x\n", s1.s->b);

        return 0;
}

You can find a more detail explanation here.


Upgrading memory RAM on a Gateway ID54 laptop

I’ve been waiting to upgrade the ram of my laptop for almost year now. I finally got tired of waiting and decided to upgrade today.

I have a gateway ID54 laptop.
It is a cheap low spec model from gateway, but is has been good enough to get my work done.

I had never upgraded the ram on a laptop before, so I didn’t know what to expect, it turned out to be way easier than I had imagined.

First I checked on gateway’s offical website the specs of the laptop.

The RAM they listed on the spec was the following:

Up to 2 GB of dual-channel DDR3 1066 MHz memory, upgradeable to 4 GB using two soDIMM modules

I have 2 slots on my laptop, one with 2GB and the other with 1GB.

Knowing the exact model I needed, I went searching to find where I could buy it.

I found the ram in three different places, amazon, tiger direct and canada computers.
Out of those three, canada computers had the best price, and it was just a few blocks from where I’m living.

I bought a Kingstom 2GB ddr3 1066mhz SODIMM for $13

To upgrade the RAM was really simple:

1 – Disconnect the battery

The first thing I did was power off the computer and disconnect the battery

2 – Open the case

Usually the RAM is located right in the middle of the laptop.

3 – Switch the RAM

The first time I switched the RAM it didn’t work. I didn’t push the RAM far enough on the slot. It is important to push until you hear the “click” sound.
After plugin in the RAM in the slot, you need to push down until the two clips on the side are holding it right. Again, you don’t want to push with to much strength, but it is important that the RAM is properly placed in the slot.

Now I can enjoy running a VM on my laptop without having the memory being paged everytime I open more than 5 tabs on a browser :D


Follow

Get every new post delivered to your Inbox.