小编nam*_*o00的帖子

为什么std :: vector使用std :: allocator而不是operator new和delete?

我正在阅读该语言作者的"使用C++编程原理和实践"一书.

我正在阅读本书基本描述如何实现std :: vector的部分.这是本书中的一段代码:

template<typename T, typename A = std::allocator<T>> class vector {
    A alloc;
    int space, size;
    T* elem;
    //...some code
    reserve(int newalloc) {
        if (newalloc <= space) return;               // never decrease allocation       
        T* p =  alloc.allocate(newalloc);          // allocate new space          
        for (int i = 0; i < sz; ++i) alloc.construct(&p[i], elem[i]);       // copy         
        for (int i = 0; i < sz; ++i) alloc.destroy(&elem[i]);                 // destroy         
        alloc.deallocate(elem, space);             // deallocate old space         
        elem = p;
        space = newalloc;
    } …
Run Code Online (Sandbox Code Playgroud)

c++

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

无法将条件中的操作数与始终构造的封闭事件控件中的相应边相匹配

此代码无法编译并给出“if(overflow)”行标题中的错误。

always @(posedge clk or negedge overflow) begin
    if(overflow)
        count_posedge = count_posedge + 1;
    else
        count_posedge = 0;
end
Run Code Online (Sandbox Code Playgroud)

我在互联网上的某个地方必须像这样更改它:

always @(posedge clk or negedge overflow) begin
    if(~overflow)
        count_posedge = 0;
    else
        count_posedge = count_posedge + 1;
end
Run Code Online (Sandbox Code Playgroud)

...而且效果很好。

根据我的理解,这两个代码应该表现相同。第一个有什么问题吗?

verilog

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

除了避免错误之外的const参数的目的

我正在使用keil C51 C编译器编程8051微控制器,它只支持C90.

我想通过UART发送一个字节的数据,函数如下所示:

void Uart_send(char data){
   //...
}
Run Code Online (Sandbox Code Playgroud)

使用1:

char a = 123;
Uart_send(a); //work great
Run Code Online (Sandbox Code Playgroud)

使用2:

Uart_send(123);//doesn't work, but no error or warning from compiler
Run Code Online (Sandbox Code Playgroud)

在后一种情况下,发送1个字节,但不是正确的值(123).

如果我将我的代码更改为:

void Uart_send(const char data){
   //...
}
Run Code Online (Sandbox Code Playgroud)

一切都很完美.

根据我的理解和互联网上的几个来源,"const"的唯一目的是防止函数改变其参数(换句话说,防止程序员错误地改变参数).那为什么我的第二种方法不起作用?

编辑:这是我的完整代码:

UART.h

typedef struct {
    char Buffer[10];
    char *p;
    char Ready;
    void (*Load)(char Data);
    void (*Transmit)();
    void (*Flush)();
} _uart;
extern _uart UART;
Run Code Online (Sandbox Code Playgroud)

UART.c

#include "UART.h"
#include <reg51.h>

#define EOF 0x00
/*
0000_0000: EOF
0000_0001: countdown
1xxx_xxxx: primary time set
1xxx_xxxx: secondary time …
Run Code Online (Sandbox Code Playgroud)

c keil c51

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

标签 统计

c ×1

c++ ×1

c51 ×1

keil ×1

verilog ×1