Summary of several techniques in C Development in Linux

Source: Internet
Author: User

Recently, I have been committed to C Development in Linux, because the boss came out. Therefore, the main technologies used are the basic background architecture of a certain message.

During this time, I learned a lot and then admired the technology of a certain news.

Therefore, I feel like I have developed our project from scratch, and now I have come to many things with my great master University.

I am responsible for the guild system, task system, email system, MAP, mall, and many other systems in the game.

The following is just a few things I have summarized recently. More will be available in the future.

1. Time

Linux systems have many things in time. In the game, time is a very important variable, involving a series of functions such as front-end time synchronization, game business countdown, heartbeat, and so on.

Wait, if you can use the time variable flexibly, At least you need to know the following functions or variables:

time_t

This variable is actually a long type, indicating the number of seconds from a time point (usually January 1, 1970 00:00:00) to that time. Yes, the metric is in seconds.

In the time. h file, there are some other functions with their parameters.

In time. in the H header file, we can also see some functions that are of the time_t parameter type or return value type: Double difftime (time_t time1, time_t time0 ); time_t mktime (struct TM * timeptr); time_t time (time_t * timer); char * asctime (const struct TM * timeptr); char * ctime (const time_t * timer );

Here is a small example.

/* time example */#include <stdio.h>      /* printf */#include <time.h>       /* time_t, struct tm, difftime, time, mktime */int main (){  time_t timer;  struct tm y2k;  double seconds;  y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;  y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;  time(&timer);  /* get current time; same as: timer = time(NULL)  */  seconds = difftime(timer,mktime(&y2k));  printf ("%.f seconds since January 1, 2000 in the current timezone", seconds);  return 0;}

The following is a more accurate

The following two types are also frequently used

struct timeval *a_pstTv, struct timezone *a_pstTz

When we call

gettimeofday(&pstCtx->stCurr, NULL);

Get current time


2. Random Number

Random numbers are also frequently used in programs. Random Numbers make some things seem random.

The C language currently provides the rand function. Then, let's look at the macro below.

#define RAND1(range)  ((int)((double)(range)*rand()/(RAND_MAX+1.0)))

This macro randomly generates a number ranging from 0 to rang-1. But when we want to use an array to disrupt the order of an array, We can randomly produce its subscript and then use it with a specific

For example, array [0.


3. arrays and pointers

This topic is a permanent topic in C language. Currently, the level-1 and level-2 pointers I commonly use are not very difficult. Data operations are performed by taking the address of the specific data memory.

Mutual conversion between arrays and pointers

At present, it is important to note that three functions

void * memcpy ( void * destination, const void * source, size_t num );void * memmove ( void * destination, const void * source, size_t num );void * memset ( void * ptr, int value, size_t num );

When the third and third parameters are sizeof, pay attention to the size of the data block and the starting position of the data.

Warning

Make sure that the pointer is not null before using the pointer.

Before using an array, you must pay attention to the array out-of-bounds behavior.


4. Clever Use of callback Functions

When our system needs to call a function of the same type, it is best to use a callback function to register it, And then perform a specific call based on the function associated with the command, similar to the C ++ polymorphism. It is worth using.


5. algorithm or algorithm.

When writing code, you need to think more. If you think it is not good to write code like this, will it be better to use specific algorithms, improve efficiency, and save space.

6. The background program remembers that the database is a bottleneck, so when it is handed over to the database, what should the program do if it wants to use Asynchronous ideas to throw data to the database,

If you know whether it is successful, you must make sure that a data mark is returned to tell us the program.


Today I think so much, so much writing. Continue to work hard.


For more blog posts, visit: http://blog.csdn.net/wallwind

Reprinted with the source



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.