小编raw*_*rex的帖子

为什么 unsigned char 具有与其他数据类型不同的默认初始化行为?

我正在阅读有关默认初始化的 cppreference 页面,我注意到一个部分说明了以下内容:

//UB
int x;
int y = x;        
   
//Defined and ok
unsigned char c;
unsigned char d = c;
Run Code Online (Sandbox Code Playgroud)

无符号字符的相同规则也适用于 std::byte 。

我的问题是,如果您在分配之前尝试使用该值(如上例),而不是 unsigned char,那么为什么所有其他非类变量(int、bool、char 等)都会导致 UB?为什么 unsigned char 很特别?

我正在阅读的页面以供参考

c++ initialization char undefined-behavior

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

如何在主函数中分配指针?

我刚开始学习编程语言 C,在解决下面的 1 个练习时遇到了问题。

编写一个程序,为一个指针动态分配 10 个字节的内存,提供的函数模板如下: bool allocate10Bytes(uint8_t *outPtr);

  • outPtr:输出指针
  • return: true: 如果分配成功。false:如果分配失败。

我尝试了以下代码,但主函数中的指针仍未分配。

bool allocate10Bytes(uint8_t* outPtr)
{
    outPtr = (uint8_t*)malloc(10 * sizeof(uint8_t));

    if (outPtr == NULL) {
        return false;
    }

    return true;
}

int main(void)
{
    uint8_t* p = NULL;

    bool success = allocate10Bytes(p);
    
    if (success) free(p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请帮我。非常感谢!

c

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

比较 (struct &a, struct &b) 与 (const struct &a, const struct &b) 的 == 运算符

Point在头文件中定义了一个结构类,如下所示 -

namespace global_planner {
    class GlobalPlanner : public nav_core::BaseGlobalPlanner {
        struct Point {
            __uint32_t x, y; 
            bool operator==(const Point &p1 ) {
                return ((p1.x == x) && (p1.y == y));  
            }
            bool operator<(const Point &p1 ) const {
                return ((p1.x < x) || (p1.x == x && p1.y < y) ) ; 
            }   
        };
    public:
        ///
    private: 
        ////               
    };
    
};
Run Code Online (Sandbox Code Playgroud)

在我的源文件(名为global_planner.cpp)中,我有一个名为的函数,generate_straight_path定义如下 -

bool GlobalPlanner::generate_straight_path(const Point &p1, const Point &p2){        
    if(costmap_ros_->getCost(p1.x, p1.y) == costmap_2d::LETHAL_OBSTACLE) …
Run Code Online (Sandbox Code Playgroud)

c++ struct constants comparator

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

我是 stl c++ 的新手,有人可以解释一下 [](int x) 吗?

is_partitioned(vect.begin(), vect.end(), [](int x)
    { return x%2==0;})? cout << "Vector is partitioned": cout << "Vector is not partitioned";
    cout << e
Run Code Online (Sandbox Code Playgroud)

ndl;

由于[](int x). 请帮忙

c++ stl

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

将第 k 个元素打印到链表中的最后一个元素的算法

我正在阅读 Cracking the Coding Interview 并做练习题,但我一直坚持这个问题:

“实现一种算法来查找单向链表的第 k 个到最后一个元素。”

我的递归函数没有返回任何东西,我不知道为什么。在递归函数中,我采用了 3 个参数。K将是我们想要找出的最后一个元素的位置。Node* temp将是头节点, int i 将保留最后一个节点的元素计数。

#include<bits/stdc++.h>
using namespace std;

class Node
{
    int data;
    Node* next;
    public:
    Node(){
        data=0;
        next=NULL;
    }

    friend class LinkedList;
};

class LinkedList
{
    public:
    Node* head;
    public:
    LinkedList(){
        head=NULL;
    }

    void append(int data){
        Node* temp= new Node();
        temp->data=data;
        temp->next=NULL;
    
        if (head==NULL){
            head=temp;
            
        }
        else{
        Node* ptr=head;
        while(ptr->next!=NULL){
            ptr=ptr->next;
        }
        ptr->next=temp;
        }

    }

    void display(){
        Node* ptr=head;
        if(head==NULL){
            cout<<"List is empty"<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list singly-linked-list

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