'new' cannot allocate object of variably modified type 'int[this->Vers]'
时间: 2023-12-18 11:03:02 浏览: 26
这个错误通常是因为你在定义一个动态数组时没有使用正确的语法。你可能正在使用一个变量来指定数组的大小,但是这是不允许的。因为在编译时,C++需要知道数组的大小,以便为其分配内存。
为了解决这个问题,你可以使用动态内存分配函数,如`new`和`delete`,来在运行时分配内存。下面是一个示例代码,可以帮助你理解如何使用动态内存分配来解决这个问题:
```
class MyClass {
public:
MyClass(int vers) {
this->Vers = vers;
this->data = new int[vers];
}
~MyClass() {
delete[] this->data;
}
private:
int Vers;
int* data;
};
```
在这个示例中,我们使用了`new`来动态分配`int`类型的数组,并在析构函数中使用`delete`释放内存。这样,你就可以避免使用变量来指定数组大小,从而解决了这个错误。
相关问题
static int sbsa_uart_probe(struct platform_device *pdev) { struct uart_amba_port *uap; struct resource r; int portnr, ret; int baudrate; / * Check the mandatory baud rate parameter in the DT node early * so that we can easily exit with the error. */ if (pdev->dev.of_node) { struct device_node *np = pdev->dev.of_node; ret = of_property_read_u32(np, "current-speed", &baudrate); if (ret) return ret; } else { baudrate = 115200; } portnr = pl011_find_free_port(); if (portnr < 0) return portnr; uap = devm_kzalloc(&pdev->dev, sizeof(struct uart_amba_port), GFP_KERNEL); if (!uap) return -ENOMEM; ret = platform_get_irq(pdev, 0); if (ret < 0) { if (ret != -EPROBE_DEFER) dev_err(&pdev->dev, "cannot obtain irq\n"); return ret; } uap->port.irq = ret; #ifdef CONFIG_ACPI_SPCR_TABLE if (qdf2400_e44_present) { dev_info(&pdev->dev, "working around QDF2400 SoC erratum 44\n"); uap->vendor = &vendor_qdt_qdf2400_e44; } else #endif uap->vendor = &vendor_sbsa; uap->reg_offset = uap->vendor->reg_offset; uap->fifosize = 32; uap->port.iotype = uap->vendor->access_32b ? UPIO_MEM32 : UPIO_MEM; uap->port.ops = &sbsa_uart_pops; uap->fixed_baud = baudrate; snprintf(uap->type, sizeof(uap->type), "SBSA"); r = platform_get_resource(pdev, IORESOURCE_MEM, 0); ret = pl011_setup_port(&pdev->dev, uap, r, portnr); if (ret) return ret; platform_set_drvdata(pdev, uap); return pl011_register_port(uap); }在上述代码中,我需要添加一个功能:在以uefi方式启动系统时,uart驱动读取acpi表内有关波特率的设置值,并以这个值进行串口波特率设置,请根据我的要求,在原代码中添加这一功能,并给出acpi表的描述
为了实现在以UEFI方式启动系统时,UART驱动能够读取ACPI表内有关波特率的设置值,并以这个值进行串口波特率设置,可以在sbsa_uart_probe函数中添加以下代码:
```
#ifdef CONFIG_ACPI
struct acpi_table_header *hdr;
acpi_status status;
struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
status = acpi_get_table(ACPI_SIG_SPCR, 0, &buf);
if (ACPI_SUCCESS(status)) {
hdr = buf.pointer;
if (hdr->length >= sizeof(struct acpi_spcr)) {
struct acpi_spcr *spcr = (struct acpi_spcr *)hdr;
uap->fixed_baud = spcr->baud_rate;
}
}
kfree(buf.pointer);
#endif
```
这段代码会先通过acpi_get_table函数获取ACPI表,然后判断是否是SPCR表,如果是,则从中读取波特率的设置值,并把这个值赋给uap->fixed_baud。
ACPI表的描述:
ACPI(Advanced Configuration and Power Interface)是一种开放标准,用于操作系统与计算机硬件进行交互。其中SPCR(Serial Port Console Redirection)是一种ACPI表,用于描述串口控制台重定向相关的信息,包括波特率、数据位、停止位、校验位等参数。在Linux内核中,可以使用acpi_get_table函数获取SPCR表的内容。
#include <stdio.h> #include <string.h> #include <malloc.h> #define ENDKEY -1 typedef struct node { int key; struct node *lchild, *rchild; } BSTNode, *BSTree; // 二叉排序树插入 void InsertBST(BSTree *bst, int key) { BSTree s, p, q; s = (BSTree)malloc(sizeof(BSTNode)); s->key = key; s->lchild = s->rchild = NULL; if (*bst == NULL) { *bst = s; } else { p = *bst; while (p != NULL) { q = p; if (key < p->key) { p = p->lchild; } else { p = p->rchild; } } if (key < q->key) { q->lchild = s; } else { q->rchild = s; } } } // 创建二叉排序树 void CreatBST(BSTree *bst) { int key; *bst = NULL; scanf("%d", &key); while (key != ENDKEY) { InsertBST(bst, key); scanf("%d", &key); } } // 中序遍历二叉树 void InOrder(BSTree bst) { if (bst != NULL) { InOrder(bst->lchild); printf("%d ", bst->key); InOrder(bst->rchild); } } int main() { BSTree bst; CreatBST(&bst); InOrder(bst); return 0; }改正该代码的错误之处
该代码没有明确指出错误的具体位置,以下是我发现的可能的错误和对应的解决方法:
1. 在 `InsertBST` 函数中,没有处理当插入节点的键值与已有节点的键值相同时的情况。解决方法是,在遍历搜索树时,如果找到了一个键值与插入节点相同的节点,则不插入新节点,直接返回。
2. 在 `CreatBST` 函数中,使用了一个未定义的宏 `ENDKEY`。解决方法是,在程序开头定义宏 `ENDKEY`,例如 `#define ENDKEY -1`。
3. 在 `main` 函数中,没有检查动态内存分配是否成功。解决方法是,在调用 `malloc` 分配内存后,检查返回的指针是否为 NULL,如果是则说明分配失败,应该结束程序或进行相应的错误处理。
4. 在 `main` 函数中,没有释放动态分配的内存。解决方法是,在程序结束前,通过调用 `free` 函数释放动态分配的内存。
以下是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ENDKEY -1
typedef struct node {
int key;
struct node *lchild, *rchild;
} BSTNode, *BSTree;
// 二叉排序树插入
void InsertBST(BSTree *bst, int key) {
BSTree s, p, q;
s = (BSTree)malloc(sizeof(BSTNode));
if (s == NULL) {
fprintf(stderr, "Error: failed to allocate memory\n");
exit(EXIT_FAILURE);
}
s->key = key;
s->lchild = s->rchild = NULL;
if (*bst == NULL) {
*bst = s;
} else {
p = *bst;
while (p != NULL) {
q = p;
if (key < p->key) {
p = p->lchild;
} else if (key > p->key) {
p = p->rchild;
} else {
free(s); // 释放已分配的节点内存
return; // 不插入新节点
}
}
if (key < q->key) {
q->lchild = s;
} else {
q->rchild = s;
}
}
}
// 创建二叉排序树
void CreatBST(BSTree *bst) {
int key;
*bst = NULL;
scanf("%d", &key);
while (key != ENDKEY) {
InsertBST(bst, key);
scanf("%d", &key);
}
}
// 中序遍历二叉树
void InOrder(BSTree bst) {
if (bst != NULL) {
InOrder(bst->lchild);
printf("%d ", bst->key);
InOrder(bst->rchild);
}
}
int main() {
BSTree bst;
CreatBST(&bst);
InOrder(bst);
free(bst); // 释放二叉树的内存
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)