小编Bec*_*ter的帖子

目标文件重定位表中条目的含义

我在理解从 C 源文件编译的重定位表的条目时遇到了一些问题。我的程序如下:

//a.c
extern int shared;
int main(){
    int a = 100;
    swap(&a, &shared);
    a = 200;
    shared = 1;
    swap(&a, &shared);
}
//b.c
int shared = 1;
void swap(int* a, int* b) {
    if (a != b)
        *b ^= *a ^= *b, *a ^= *b;
}
Run Code Online (Sandbox Code Playgroud)

我使用以下命令gcc -c -fno-stack-protector a.c b.cld a.o b.o -e main -o ab. 然后我objdump -r a.o检查它的重定位表。

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE 
0000000000000014 R_X86_64_32       shared
0000000000000021 R_X86_64_PC32 …
Run Code Online (Sandbox Code Playgroud)

c linker x86-64 relocation

6
推荐指数
1
解决办法
1297
查看次数

x86-64 在寄存器中传递参数的顺序

我对 x86-64 环境中的参数传递过程很好奇,因此我写了一段代码。

//a.c
extern int shared;
int main(){
    int a=100;
    swap(&a, &shared);
}
//b.c
int shared=1;
void swap(int* a, int* b){
    *a ^= *b ^= *a ^= *b;
}
Run Code Online (Sandbox Code Playgroud)

我使用以下命令编译两个文件: gcc -c -fno-stack-protector a.c b.c 然后我objdump -d a.o检查ao的反汇编代码。

Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   c7 45 fc 64 00 00 00    movl   $0x64,-0x4(%rbp)
   f:   48 8d 45 fc             lea …
Run Code Online (Sandbox Code Playgroud)

c parameters assembly x86-64 calling-convention

5
推荐指数
1
解决办法
1万
查看次数

如何使用flex选项-o( - output = FILE)

我在尝试时遇到了一个问题flex abcd.l.我想将输出重定向到一个新文件,而不是lex.yy.c 我在手动查找选项时查找的默认文件,-o(--output=FILE)因此我将命令更改为flex xx.l -o lex.yy.1.c但发生错误.

flex: can't open --outfile=lex.yy.1.c
/usr/bin/m4:stdin:2621: ERROR: end of file in string
Run Code Online (Sandbox Code Playgroud)

我的工作环境是cygwinwindows 7

flex-lexer

3
推荐指数
1
解决办法
2238
查看次数

有关strlen不同实现的性能的问题

我已经实现了strlen()以不同的方式,包括功能SSE2 assemblySSE4.2 assembly并且SSE2 intrinsic,我也产生了一些实验,请用strlen() in <string.h>strlen() in glibc。但是,以毫秒(时间)为单位的性能是出乎意料的。

我的实验环境: CentOS 7.0 + gcc 4.8.5 + Intel Xeon

以下是我的实现:

  1. strlen 使用SSE2程序集

    long strlen_sse2_asm(const char* src){
    long result = 0;
    asm(
        "movl %1, %%edi\n\t"
        "movl $-0x10, %%eax\n\t"
        "pxor %%xmm0, %%xmm0\n\t"
        "lloop:\n\t"
            "addl $0x10, %%eax\n\t"
            "movdqu (%%edi,%%eax), %%xmm1\n\t"
            "pcmpeqb %%xmm0, %%xmm1\n\t"
            "pmovmskb %%xmm1, %%ecx\n\t"
            "test %%ecx, %%ecx\n\t"
            "jz lloop\n\t"
    
        "bsf %%ecx, %%ecx\n\t"
        "addl %%ecx, %%eax\n\t"
        "movl %%eax, %0"
        :"=r"(result)
        :"r"(src)
        :"%eax" …
    Run Code Online (Sandbox Code Playgroud)

performance gcc sse inline-assembly intrinsics

2
推荐指数
1
解决办法
572
查看次数

使用while(scanf("%d",&n)!= EOF)和while(scanf("%d",&n)&& n!= EOF)之间的间隔时间

我的问题不是关于算法,而是关于C编程语言的混乱.我遇到了如下问题:

问题描述

N!(N阶乘)可能非常刺激并且难以计算N的大值.因此,我想知道其中有多少位数,而不是计算N !. (记住N!= N*(N - 1)*(N - 2)*...*2*1)

输入

输入的每一行将在其上具有单个整数N 0 <N <1000000(1百万).输入由文件结束终止.

产量

对于N的每个值,打印出N!中有多少位数.

我的代码是这样的:

 #include<stdio.h>
 int main()
{
    int n ;
    while(scanf("%d",&n)&&n!=EOF)//1
    //while(scanf("%d",&n)!=EOF)//2
    {
        int result=1;
        int i;
        double temp = 1;
        for(i=n;i>0;i--)
        {
            temp*=i;
            while((temp/10)>1)
            {
                result++;
                temp=temp/10;
            }           
        }
        printf("%d\n",result);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我使用时while(scanf("%d",&n) && n!=EOF),我使用的在线评判表明Time Limit Exceeded,当我改变它时while(scanf("%d",&n)!=EOF),每件事情都顺利.我想n!=EOF可能需要太多时间,那么它的本质是EOF什么?

c scanf eof

1
推荐指数
1
解决办法
2906
查看次数

vector <string>为null,而某些字符串已插入

我创造了一个vector<string>持有strings,但是当我cout<<vec[0],我发现其中没有任何东西.为什么?

#include <iostream>
#include <vector>
#include <string>

using namespace std;
int main()
{
vector<string> vec(10);
string s("123123");
vec.push_back(s);
cout<<vec[0]<<endl;
vector<string>::iterator it=vec.begin();
cout<<*it<<endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ string cout vector

0
推荐指数
1
解决办法
283
查看次数