[Performance] Memory allocation and array structure

In most of the modern programming language I have learnt, such as C++ and Python, as well as Javascript, the memory allocation for array data structure is like this:

/* (e.g.) */ var a = [["a", "b", 'c"], [1, 2, 3], ["甲", "乙", "丙"]] //Java-script
// /* Memory allocation in order  */ "a" -->  'b" --> "c" --> 1 --> 2 --> 3 --> "甲" --> "乙" --> "丙"

Little thought

Suppose I am gonna store the following sets of data into an array, what’s the most best way to make it accessible most efficiently?

Name No Index












Given that the items can be accessed by knowing the index, I have thought of 2 ways of storage

Method 1: The traditional way

var data = [["Peter", 1], ["Mary", 5], ["Lily", 30], ["Jonathan", 25]];
// /* Memory storage: */ "Peter" --> 1 --> "Mary" --> 5 --> "Lily" --> 30 --> "Jonathan" --> 25

This way is very clear and easy to understand, but may not be quite efficient to access the {No} of each element. For instance, the computer has to read pass all {Name}s of each item, and the memory access footprint is long.

Method 2: The “Split" way

var data = [["Peter", "Mary", "Lily", "Jonathan"], [1, 5, 30, 25]];
// /* Memory storage: */ "Peter" --> "Mary" --> "Lily" --> "Jonathan" --> 1 --> 5 --> 30 --> 25

To loop through all {No}s of all data items, just loop within data[1], and the memory access footprint is limited to number of data items.

To access {Name}s and {No}s, just need data[0][x] and data[1][x], where “x" is the index of the item.

There may be a problem associated with this kind of array structure, such as deleting a datum element, it has to loop through different parts of the data (can be understood as “columns" of data in a table). Nonetheless, for storing rare-updated data, I prefer the “split" way.

Compile with custom library II

2 months ago, I mentioned compiling C projects with custom library, such as genlib, simpio and strlib by including the .h header files and .c implementation files in the compilation folder. However, I made a mistake (not mistake actually, but a boob). It’s obviously not an efficient way to copy all .h and .c files in the compilation folder for each project, unless you wanna make it portable.

Fortunately, I found a better way to have fun with custom library.

In the meantime, I am still using GUI IDE for C programming (Code::Blocks) instead of using command line and scripts. Therefore, this “better way" is to tell the compiler (of Code::Blocks) (Not writing a script) where is the library located.

To include a directory, where the library files (headers and implementation files) are located, in the compiler’s usable library list.

Just 3 steps and it’s done. You can now and ever use the common custom library for all C projects and do not need to copying them into each project folder every time.

C Programming – Compile programmes with custom library (#include)

I have been afflicted by the way to include a custom library since I started learning C language. I had no way to use custom libraries, such as strlib.h (which is not a standard ANSI C library nor extended library), in a program source. I have been googling the way for several days but still can’t find any answer (I finally know the way after thousands of trials).

The reason for why I can’t find any answer may be just because it’s too fundamental, which is assumed to be “common sense". Then, how did I know the way to include the library?

As custom libraries are included in the program source, when the compiler compiles it and sees the line #include “testing.h", it would certainly (obviously?) search for the library in the same folder of the program source file. Therefore, if I put the library package in the same folder, the compiler will find it. Though this seems to be very easy to understand, but the wrong methods sounds reasonable too.

The wrong method:

For a C language noob, like me, I searched the folder, in which the stdio.h library is located (In exact, it’s located in the \include folder inside the folder of the compiler (such as MinGW)). And, it seems to be quite reasonable to put a custom library header file there too. Then, how about the file of library implementation? In the same folder, I can’t find any c source file (implementation) of stdio.h.

Thus, this way just sounds reasonable, but indeed, not.

The correct method:

As I have said in the introduction part, the library package (implementation file (.c) and header file (.h)) and the programme source file has to be put in the same folder.

In addition:

You use double quotes for custom libraries (I think this is the way to tell the compiler that, the library not located in the \include folder). For example: #include “strlib.h"