杂记 (4) —— linux and coding

本文介绍了Linux下实用的计算器bc及常见编程问题解决方法,包括awk编程、数组操作、内存管理和多态实现等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录

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

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

启动: bc
离开: quit

awk编程,非法引用数组

$ ./split.sh
awk: line 5: illegal reference to array elements
$  cat split.sh 
#!/bin/bash
awk '
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);
             i++;
      }

直接运行,出现错误,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 __protocol) __THROW;

原因是自己在头文件中声明的函数没有以;结尾。

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

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/cc
lrwxrwxrwx 1 root root 20  914 23:59 /usr/bin/cc -> /etc/alternatives/cc
edemon@ubuntu:~$ ls -al /etc/alternatives/cc
lrwxrwxrwx 1 root root 12  914 23:59 /etc/alternatives/cc -> /usr/bin/gcc

这说明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 noexpandtab
set 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 <stdio.h>
#include <stdlib.h>
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 EXIT_SUCCESS;
}

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;
}

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

int *a = malloc(20);

不借助第三个变量的交换

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

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

#include <iostream>
#include <bitset>
using namespace std;

int main(){
    long long int a=-1;
    bitset<64> bs(a);
    int i;
    for(i=bs.size(); i>=0; i--){
        cout<<bs[i];
    }
    cout<<endl;
    int ans=0;
    for(i=0;i<64;i++){
        if(a&(1LL<<i)) ans++;
    }
    cout<<ans<<endl;
    return 0;
}

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

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 <iostream>
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: "<<p->area()<<endl;
    p = &sq;
    cout<<"square: "<<p->area()<<endl;
    p = &tr;
    cout<<"triangle: "<<p->area()<<endl;
    return 0;
}

circle: 3.14159
square: 1
triangle: 0.5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值