C
Published: 13:08, Thursday 14 July 2011
Notes
What's this? See my article about Notes.
compiler
gcc
warnings: -Wall <filename>
profiling:
-pg, generates gmon.out
run once ./a.out
gprof a.out
make, generate a.out: gcc hello.c
output file: -o file
don't run linker, assemble, generate .o: -c
preprocess, generate .i: -E
compilate, generate .s/assembly only: -S
32/64 bits: -m32/-m64
generate .i,.s,.out: gcc -save-temps hello.c
debugging info for gdb: -g (creates .debug and .line and nmakes inline c dumping possible)
multiple files: need to give them all as arg or "undefined reference to"
preprocessor
how to use includes:
#include <stdio.h>
#define N 16
headers:
#ifndef __CNAME_H__
#define __CNAME_H__
all needed includes
#endif // __CNAME_H__
code files:
no ifndef
include header
extension: hello.c
main: int main(int argc, char *[]~~argv~~) {
position of std:
[[/usr/include/stdio.h]]
print string:
printf=fprintf(stdout,...)
h=short,l=long,L=long double
i=int,o=int in octal,x=int in hexa
i=uint,c=single
s=char*,f=double,e,E=double,g,G=double
p=void*
n= number of chars
%
see symbol table .symtab:
readelf (more detail than objdump) -a/-s a.out/main.o
nm
sizes and names of sections: size
list shared libraries needed at run time: ldd
objdump
full contents (incl rodata): -s
disassemble -d hello.o > dump.txt
file headers: -f
assembly interleaved with c source:
gcc -c -g hello.c
objdump -S hello.o
cc -c -g hello.c; objdump -S hello.o > dump.s; vi dump.s
strings: strings
arrays
T a[N];
size of array = N*sizeofT
a points to array beginning xa
pointer arithm: a+i=xa+i*sizeofT
a[i] is equiv to *(a+i)
a[1] is equiv to *a
nested arrays
int a[m][n]
is equiv to:
typedef int tn[n];
tn a[m];
i-th row: a[i]. a[rows][cols]
row major: i-th row compact
fixed-sized (optim):
#define N 16;
typedef int fix[N][N];
variable-sized as argument:
func_name(int a[m][n])
structs
struct rect {
int a;
int b;
}
struct rect r;
or
struct {
int a;
int b;
} r;
r.a = r.b = 1;
struct rect r = { 1, 1 };
structs and functions:
func(struct rect *rp) {
rp->a or (*rp).a
}
call: func(&r);
comments: only [[/*]] */
read from input
fgets, don't use gets nor scanf!
fgets(buffer, sizeof(buffer), stdin); (preserves \n)
fscanf(File *stream, const char *format, ...)
in the format use %1023s or similar to limit string size (rest stays in buffer)
scanf
use fgets
uses same rules as printf
buffer overflow:
char b[4];
scanf("%s", b);
make:
Makefile:
target
component (target depends on component)
commands to generate target
target:
commands
include file.mk
macros: CC = gcc, CFLAGS = -Wall -pedantic -g
concat commands:
cmd1; \
cmd2
internal macros:
$?: younger than target
$@: target name
suffixes:
.SUFFIXES: .txt .html
.html.txt:
cmd to convert
run first targets: make
show all targets
egrep "^# target:" [Mm]akefile
needs comments before targets like "# target: tname - What"
make a release:
PACKAGE = package
VERSION = ` date "+%Y.%m%d" `
RELEASE_DIR = ..
RELEASE_FILE = $(PACKAGE)-$(VERSION)
dist:
tar -cf $(RELEASE_DIR)/$(RELEASE_FILE) && \
gzip -9 $(RELEASE_DIR)/$(RELEASE_FILE).tar
compile:
CC = gcc
CFLAGS = -g
all : helloworld
helloworld
helloworld.o
$(CC) $(LDflagS) -o $@ $^
helloworld.o
helloworld.c
$(CC) $(CFLAGS) -c -o $@ $<
clean
rm -f helloworld helloworld.o
.PHONY: clean
sockets:
tcp:
create socket:
int s = socket(AF_INET, SOCK_STREAM, 0); // udp SOCK_DGRAM
bind:
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = PF_INET;
sa.sin_port = htons(0);
sa.sin_addr = htonl(INADDR_ANY);
if (bind (s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("binding to local address");
close(s);
return -1;
}
resolve host name:
struct hostent *h;
h = gethostbyname(host);
if (!h || h->h_length != sizeof(struct in_addr)) {
fprintf(stderr, "%s: no such host\n", host);
return -1;
}
connect:
struct sockaddr_in sa;
sa.sin_port = htons(port);
sa.sin_addr = *(struct sockaddr *)h->h_addr;
if (connect (s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror(host);
close(s);
return -1;
}
send:
ssize_t send(int s, const void *buf, size_t len, int flags);
ssize_t recv(int s, void "buf, size_t len, int flags);
ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen);
ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
server:
listen:
int listen(int sockfd, int backlog);
backlog: number of outstanding connection attempts
accept:
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
pattern:
create server socket, bind to local addr, listen, loop:
call accept, get new connection/socket
read client's request from connection socket
write response back to connection socket
close
asynchronous/event driven:
nonblocking:
if ((n = fcntl (s, F_GETFL)) < 0 || fcntl(s, F_SETFL, n | O_NONBLOCK) < 0) {
perror("O_NONBLOCK");
}
makes funcs nonblocking with errno:
read/recv/write/send -> EAGAIN
connect -> EINPROGRESS
accept -> EWOULDBLOCK
select:
int select(int nfds, fd_set "readfds, fd_set *writefds, fd_set *exceptfds, truct timeval *timeout);
void FD_CLR(int fd, fd_set *set);
int FD_ISSET(int fd, fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_ZERO(fd_set *set);
getopt
Write a Comment
Name:
*
Email:
Website:
If you are human write 'm':
*
Title:
*
Your comment:
*
* These fields are mandatory.
© Copyright 2009-2011 Nicola Marcacci Rossi