小编cad*_*luk的帖子

在编译/链接时如何使用地址进行计算?

我编写了一些初始化IDT的代码,该IDT将32位地址存储在两个不相邻的16位半部分中。IDT可以存储在任何地方,您可以通过运行LIDT指令告诉CPU在哪里。

这是初始化表的代码:

void idt_init(void) {
    /* Unfortunately, we can't write this as loops. The first option,
     * initializing the IDT with the addresses, here looping over it, and
     * reinitializing the descriptors didn't work because assigning a
     * a uintptr_t (from (uintptr_t) handler_func) to a descr (a.k.a.
     * uint64_t), according to the compiler, "isn't computable at load
     * time."
     * The second option, storing the addresses as a local array, simply is
     * inefficient …
Run Code Online (Sandbox Code Playgroud)

c x86 linker interrupt osdev

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

页框编号与页表条目

那么页框号和页表项到底有什么区别呢?

以下哪一项是正确的:

physical_address = PTE * page_size + offset
Run Code Online (Sandbox Code Playgroud)
physical_address = PFN * page_size + offset
Run Code Online (Sandbox Code Playgroud)

paging operating-system memory-address

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

CPU乱序效果的测试程序

我写了一个多线程程序来演示英特尔处理器的乱序效果.该计划附在本文末尾.预期的结果应该是当handler1将x打印为42或0时.但是,实际结果总是为42,这意味着不会发生乱序效应.

我使用命令"gcc -pthread -O0 out-of-order-test.c"编译了程序.我在Intel IvyBridge处理器Intel(R)上运行Ubuntu 12.04 LTS(Linux内核3.8.0-29-通用)上的编译程序)Xeon(R)CPU E5-1650 v2.

有谁知道我应该怎么做才能看到乱序效果?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int f = 0, x = 0;

void* handler1(void *data)
{
    while (f == 0);
    // Memory fence required here
    printf("%d\n", x);
}

void* handler2(void *data)
{
    x = 42;
    // Memory fence required here
    f = 1;
}

int main(int argc, char argv[])
{
    pthread_t tid1, tid2;

    pthread_create(&tid1, NULL, handler1, NULL);
    pthread_create(&tid2, NULL, handler2, NULL);

    sleep(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c x86 synchronization memory-barriers barrier

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

苦苦挣扎着编写一个简单的计算器

我正在尝试编写一个简单的计算器.
这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void) { 

char operator = 0;
float num1 = 0.0;
float num2 = 0.0;
float sol = 0.0;


while (operator != 'q') {

    printf("Operator: ");
    scanf("%c", &operator);
    printf("First Number: ");
    scanf("%f", &num1);
    printf("Second Number: ");
    scanf("%f", &num2);

    switch (operator)
    {
    case '+': sol = num1 + num2; break;
    case '-': sol = num1 - num2; break;
    case '*': sol = num1 * num2; break;
    case '/': sol = num1 / num2; break;
    case …
Run Code Online (Sandbox Code Playgroud)

c calculator break switch-statement

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

如何将矩阵行的元素传递给函数?

假设我在C中有一个多维矩阵matrix[2][3],其元素如下所示:

1 3 5
2 3 4 
Run Code Online (Sandbox Code Playgroud)

我想将矩阵的第二行(作为数组)传递给函数.我是这样做的:

void myFunction(int array[]) {
}

int main() {
    int matrix[2][3];
    myFunction(matrix[2]);
}
Run Code Online (Sandbox Code Playgroud)

虽然,当我打印元素array[]内部的值时,myFunction看起来都设置为零:

0 0 0
Run Code Online (Sandbox Code Playgroud)

如何正确地将矩阵行的元素传递给函数?

c matrix

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

返回奇怪值的类中的C++指针

所以我在IDE CodeBlocks 13.12中创建了一个带有两个私有指针,两个setter,两个getter,一个构造函数和一个析构函数的简单类.对于普通变量,getter返回精确值,但使用指针返回一个奇怪的值,另一个返回精确值.

  1. 这是我班级的标题:

    #ifndef COMPLEXE_H_INCLUDED
    #define COMPLEXE_H_INCLUDED
    
    #include <iostream>
    
    class Complexe{
    public:
        Complexe(const double re = 0.0, const double im = 0.0);
        ~Complexe();
    
        double getReel() const;
        double getImag() const;
    
        void setReel(double);
        void setImag(double);
    
    private:
        double* re;
        double* im;
    
    };
    
    #endif // COMPLEXE_H_INCLUDED
    
    Run Code Online (Sandbox Code Playgroud)
  2. 这是我班级的来源

    #include "complexe.h"
    
    Complexe::Complexe(const double re, const double im)
    {
        double a = re;
        double b = im;
    
        this->re = &a;
        this->im = &b;
    }
    
    Complexe::~Complexe()
    {
        delete re;
        delete im;
    }
    
    double Complexe::getReel() const
    {
        return …
    Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

删除了未使用的类

我有以下代码:

namespace rm {
  namespace lib {
    class Object {
    public:
      Object() {printf("Hi\n");}
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我想用g++ -Wall -Wno-unused -Werror -fPIC -g -Iinclude/ -c -O0 -o object.o object.cpp和编译一个共享库g++ --shared -o librm.so object.o.编译工作没有问题,但在检查结果库后nm librm.so | c++filt,rm::lib::Object::Object()无处可寻.即使启用-Wextra,GCC也不会发出警告.

G ++版本是(Raspbian 4.9.2-10)4.9.2

c++ gcc g++4.9

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

usr/bin/ld: 找不到 -lncurses

我正在尝试链接 ncurses 库。但这是行不通的。这些是错误消息:

/usr/bin/ld: cannot find -lcurses
/usr/bin/ld: cannot find -lncurses

collect2: error: ld returned 1 exit status

Makefile:102: recipe for target 'ch_lab' failed

make[1]: *** [ch_lab] Error 1
Run Code Online (Sandbox Code Playgroud)

我的问题是,我应该安装哪个软件包才能在 Ubuntu 上运行?

c++ gcc ubuntu-15.04

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

Scanf没有读取双倍

我试图double从用户不断读取值scanf.

码:

printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
Run Code Online (Sandbox Code Playgroud)

输出是

INPUT: 0.000
Run Code Online (Sandbox Code Playgroud)

c floating-point double scanf

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

继承和删除复制和移动构造函数

我想要一个具有第三级继承的类.我不会移动我的课程或复制它们.实际上,它们只会创建一次.我想测试是否可以删除移动和复制构造函数和赋值,如下所示:

MyClass ( MyClass && ) = delete;
MyClass ( const MyClass & ) = delete;
MyClass & MyClass :: operator= ( const MyClass & ) = delete;
MyClass & MyClass :: operator= ( MyClass && ) = delete;
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我在基类构造函数和基类的基础上得到一个错误:

initializing argument 1 of ‘Base::Base(const int&)’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

我的构造函数是这样的:

Base(const string & name):BaseOfBase(name){

    };
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 是否可以删除所有移动和复制构造函数和赋值运算符?
  2. 如果有可能,为什么我会收到错误?
  3. 如果可能的话,这是一个不错的选择吗?

c++ copy-constructor move-constructor c++11

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

如何在masm代码中编写字母启动的十六进制数字?

我目前正在编辑几个由MASM代码组成的宏.它们看起来都像这样:

Primary MACRO
Key 0Bh,'0'
Key 29h,15h
Key 03h,'2'
Key 06h,'5'
Key 0Ch,'+'
Key 0Dh,'´'
Key 1Bh,'¨'
Key 2Bh,27h
Key 35h,'-'
Key 34h,'.'
Key 33h,','
Key 56h,'<'
ENDM
Run Code Online (Sandbox Code Playgroud)

我注意到,我可以写一个由发起十六进制数字(首先)字符0-9的格式如下:02h,12h,5Ah,等等.但是,如果我尝试以同样的方式来写一封信引发的十六进制数字(即像ABh,CAh,DFh等等),我得到一个错误.我曾尝试格式0xBA,0xFE等等,但它也不管用.

在这种情况下,有人能告诉我用于写入字母启动的十六进制数字的正确格式吗?

assembly hex masm32

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

strcpy给c中的分段错误

嗨,请帮帮我

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int classmatesize=0;
char **classmate1;
char **classmate2;    

void checkclassmates(){
        int i,j;
        for(i=0;i<classmatesize;i++){
            for(j=i+1;j<classmatesize;j++){
                if(strcmp(classmate1[i],classmate1[j])==0){
                    strcpy(classmate1[classmatesize],classmate2[i]);
                    strcpy(classmate2[classmatesize],classmate2[j]);
                    classmatesize++;
                }else if(strcmp(classmate1[i],classmate2[j])==0){
                    strcpy(classmate1[classmatesize],classmate2[i]);
                    strcpy(classmate2[classmatesize],classmate1[j]);
                    classmatesize++;                
                }else if(strcmp(classmate2[i],classmate2[j])==0){
                    strcpy(classmate1[classmatesize],classmate1[i]);
                    strcpy(classmate2[classmatesize],classmate1[j]);
                    classmatesize++;
                }else if(strcmp(classmate2[i],classmate1[j])==0){
                    strcpy(classmate1[classmatesize],classmate1[i]);
                    strcpy(classmate2[classmatesize],classmate2[j]);
                    classmatesize++;
                }
            }
        }
    }
int main(void) {
    int i;

    classmate1 = malloc(1000 * sizeof(char*));
    for ( i = 0; i < 1000 ; i++)
    classmate1[i] = malloc((1000) * sizeof(char));

    classmate2 = malloc(1000 * sizeof(char*));
    for ( i = 0; i < 1000 …
Run Code Online (Sandbox Code Playgroud)

c strcpy

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

如何释放字符串文字所消耗的内存?

如何释放a char*后使用的所有内存不再有用?

我有一些 struct

struct information
{
/* code */
char * fileName;
}
Run Code Online (Sandbox Code Playgroud)

我显然要在其中保存一个文件名char*,但在使用一段时间之后,我想释放以前的内存,我该怎么做?

E:我并不是要释放指针,而是指向的空间fileName,这很可能是一个字符串文字.

c memory-management string-literals

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