杂记 (4) —— linux and coding

网友投稿 299 2022-09-16

杂记 (4) —— linux and coding

记录

linux文本界面下的计算器,bc. 可进行的操作:

+ 加法 - 減法 ###乘法 / 除法 ^ 指数 %

启动: bc 离开: quit

awk编程,非法引用数组

$ ./split.shawk: line 5: illegal reference to array elements$ cat split.sh

#!/bin/bashawk 'BEGIN{info="I love linux and shell.";split(info,elements," ");print length(elements);for(i in elements){ print i,elements[i];}}'

解释:length(array)的写法是使用GNU awk支持的。

检测编译器是32位或64位的方法

​​printf("%d", sizeof(long)) //4 or 8​​

争夺socket资源发生错误:

int ret = 0; while(1){ buff = str[i]; ret = sendto(s,buff,strlen(buff),0,(struct sockaddr *)&serv,sizeof(serv)); if(ret == -1){ perror("send to"); } //sleep(1); ret = sendto(s,buff,strlen(buff),0,(struct sockaddr *)&serv,sizeof(serv)); if(ret == -1){ perror("send to"); } //sleep(1);

直接运行,出现错误,Program received signal SIGSEGV, Segmentation fault. 如果我们加上sleep(1),进行调度控制。那么资源争夺现象将会被控制。

eclipse使用记录

常用快捷键: ctrl+Q // 回到前一个编辑处 F3 //查找定义处 ctrl+L // 快速行 ctrl+H //快速查找函数 ctrl+shift+R //快速打开文件 ctrl+left //上一个光标停留处 ctrl+F11 //Run F11 //Debug

函数调用关系: 选中方法->右键->references->project

eclipse显示行号

Window —— Preferences —— General —— Editors —— TextEditors —— Show line numbers

linux中查找可执行文件的路径一个好用的命令

​​$ whitch filename​​

安装adobe flash player for linux X86-64

在浏览器中下载tar.gz压缩包, 解压tar -zxvf filename 找到解压后的libpepflashplayer.so,对于firefox, 复制到/usr/lib/mozilla/plugins/中, 对于chromium,复制到/usr/lib/chromium-browser/plugins

left shift count >= width of type

如果出现上述的错误,那么很可能存在着溢出的问题。不过被转换了,编译器仅仅给出了警告。

吓人的.h错误 storage class specified for parameter

/usr/include/x86_64-linux-gnu/bits/sockaddr.h:28:28: error: storage class specified for parameter ‘sa_family_t’ typedef unsigned short int sa_family_t; ^/usr/include/x86_64-linux-gnu/bits/socket.h:151:5: error: expected specifier-qualifier-list before ‘sa_family_t’ __SOCKADDR_COMMON (sa_); /###Common data: address family and length. */ ^/usr/include/x86_64-linux-gnu/bits/socket.h:164:5: error: expected specifier-qualifier-list before ‘sa_family_t’ __SOCKADDR_COMMON (ss_); /###Address family, etc. */ ^In file included from /usr/include/x86_64-linux-gnu/sys/socket.h:38:0, from common.c:4:/usr/include/x86_64-linux-gnu/bits/socket.h:272:24: error: storage class specified for parameter ‘__cmsg_nxthdr’ extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr, ^In file included from common.c:4:0:/usr/include/x86_64-linux-gnu/sys/socket.h:113:12: error: storage class specified for parameter ‘socket’ extern int socket (int __domain, int __type, int

用{}给数组赋值,注意下面的结果

int a[10] = {0};/### 0 0 0 0 0 0 0 0 0 0 */int a1[10] = {1,2,3};/### 1 2 3 0 0 0 0 0 0 0 */int *b[10] = {NULL};/###0 0 0 0 0 0 0 0 0 0 */int *b1[10] = {malloc(4),malloc(4),malloc(4)};/###14983184 14983216 14983248 0 0 0 0 0 0 0 */

在用retext写markdown文件插入图片时不要用相对路径,那样显示不出来结果。

问题

ubuntu有时鼠标会消失

按tab试试。

linux下的cc是指什么?

通过追踪,我们能发现一些到东西:

edemon@ubuntu:~$ ls -al /usr/bin/cclrwxrwxrwx 1 root root 20 9月 14 23:59 /usr/bin/cc -> /etc/alternatives/ccedemon@ubuntu:~$ ls -al /etc/alternatives/cclrwxrwxrwx 1 root root 12 9月 14 23:59 /etc/alternatives/cc ->

这说明cc是一个指向gcc的软链接。 不过在Unix下,cc是c compiler的缩写,和gcc不是同一个东西,gcc是GNU compiler collection的缩写,是一个编译器集合。

makefile:8: **###missing separator (did you mean TAB instead of 8 spaces?). Stop

在makefile中,make识别一个tab(/t),然后执行shell命令。如果tab是4个空格,那么会出现各种错误。 在配置文件~/.vimrc中,设置tab是4个空格是这样的: set softtabstop=4 在vi编辑模式下可以ctrl+v+i输入/t 相关的vimrc设置:

set noexpandtabset softtabstop=4

expandtab设置了用空格代替制表符,所以使用命令​​od -t c makefile​​看到文件中相关位置只有四个空格没有\t。现在将其设置成noexpandtab。

面试记录

lvalue required as left operand of assignment

它的意思是赋值符的左边应该是一个变量。 比如看下面数组扩容的例子

int a[10] = {0}; int *p = a; p+10 = (int *)malloc(8);

第三条语句就有错误。 事实上很简单,再加一条语句即可

int a[10] = {0}; int *p = a; int *p2 = p+10; p2 = (int *)malloc(8);

C的交换

int 数值交换,数组交换,字符串交换。

/* ============================================================================ Name : swap.c Author : theArcticOcean Version : Copyright : Description : C, Ansi-style ============================================================================ */#include #include void intSwap(int *t1,int *t2){ /###int地址传给int指针交换值 */ int t; t = *t1; *t1 = *t2; *t2 = t;}void strSwap(char **s1,char **s2){ /###str地址传给str二重指针交换地址 */ char *t = NULL; t = *s1; *s1 = *s2; *s2 = t;}void arraySwap(int **t1,int **t2){ /###数组名是一个常量,因此不能通过strSwap的方法交换元素 */ int *t = NULL; t = *t1; *t1 = *t2; *t2 = t;}int main(void) { int t1 = 1; int t2 = 2; intSwap(&t1,&t2); char *s1 = "hello"; char *s2 = "haha"; strSwap(&s1,&s2); printf("after swap, int: %d %d, str: %s %s\n",t1,t2,s1,s2);// int a[3] = {1,2,3}; /###数组名是一个常量,因此不能通过strSwap的方法交换元素 */// int b[3] = {4,5,6}; int *a = (int *)malloc(12); int *b = (int *)malloc(12); int i; for(i=0;i<3;i++){ a[i] = i+1; /###1,2,3 */ b[i] = i+4; /###4,5,6 */ } arraySwap(&a,&b); for(i=0;i<3;i++){ printf("%2d",a[i]); } puts(""); for(i=0;i<3;i++){ printf("%2d",b[i]); } puts(""); return

Type function returns address of local variable [-Wreturn-local-addr]

代码:

int *getArray(){ int a[5]; int i; for(i=0;i<5;i++){ a[i]=i+1; } return

函数结束后,函数变量的内存都会被释放, 返回的地址是不安全的。 不要将a放到栈内,我们用堆的内存

int *a = malloc(20);

不借助第三个变量的交换

void swap(int *t1,int *t2){ *t1=*t1^*t2; *t2=*t1^*t2; *t1=*t1^*t2;}

求解有符号64位整数二进制表达式中0的个数

#include #include using namespace std;int main(){ long long int a=-1; bitset<64> bs(a); int i; for(i=bs.size(); i>=0; i--){ cout<

下面的方法是有问题的(这是我写过的最烂的代码):

while(a){ if(a&1) ans++; a>>=1;}return ans;

怎样使用gdb切换到不同的线程

attach PID #连接到进程并进行调试thread tid #切换到相应的线程

求面积问题——多态

定义一个基类,并计算三角形,正方形,圆形的面积。 java版本,抽象类集成则重载,父类继承则重写

public class shape public double area(){ return 0; }}public class circle extends shape private double r; public circle(double R){ r = R; } public double area() { return Math.PI*r*r; }}public class triangle extends shape private double h,l; triangle(double H,double L){ h=H; l=L; } public double area() { return 0.5*h*l; }}public class square extends shape private double d; square(double D){ d = D; } public double area() { return d*d; }}public class Main public static void main(String[] args) { System.out.println("circle: "+new circle(1).area()); System.out.println("square: "+new square(1).area()); System.out.println("triangle: "+new triangle(1,1).area()); }}

C++版本,虚函数实现多态

#include using namespace std;class Base{ public: virtual double area(){ return 0; }};class triangle: public Base{ double h,l; public: triangle(double H,double L){ h = H; l = L; } virtual double area(){ return 0.5*h*l; }};class Square: public Base{ double d; public: Square(double D){ d = D; } virtual double area(){ return d*d; }};class Circle: public Base{ double r; public: Circle(double R){ r = R; } virtual double area(){ return 3.1415926*r*r; }};int main(){ Circle cr(1); Square sq(1); triangle tr(1,1); Base *p = &cr; cout<<"circle: "<area()<area()<area()<

circle: 3.14159 square: 1 triangle: 0.5

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:知识产权行业邮件营销方案
下一篇:linux C —— 文件操作函数
相关文章

 发表评论

暂时没有评论,来抢沙发吧~