本帖最后由 yanghelovehuang 于 2018-8-10 09:38 编辑
问了你们这么多结构体问题正在练习结构体,可是遇到一个问题不明白,在编写应用程序时这种嵌套是非法的:
struct st{
struct mn lk;
};
可是为什么内核就可以呢?而且内核里很多结构体都是这样嵌套的。
内核:
struct atmel_nand_host {
struct nand_chip nand_chip;
void __iomem *io_base;
dma_addr_t io_phys;
struct atmel_nand_data board;
struct device *dev;
void __iomem *ecc;
struct completion comp;
struct dma_chan *dma_chan;
struct atmel_nfc *nfc;
};
他们是怎么做的?还有:static int atmel_nand_probe(struct platform_device *pdev){
struct atmel_nand_host *host;
struct mtd_info *mtd;
struct nand_chip *nand_chip;
struct resource *mem;
int res, irq;
/* Allocate memory for the device structure (and zero it) */
host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
if (!host)
return -ENOMEM;
res = platform_driver_register(&atmel_nand_nfc_driver);
if (res)
dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
host->io_base = devm_ioremap_resource(&pdev->dev, mem);
if (IS_ERR(host->io_base)) {
res = PTR_ERR(host->io_base);
goto err_nand_ioremap;
}
host->io_phys = (dma_addr_t)mem->start;
nand_chip = &host->nand_chip;
mtd = nand_to_mtd(nand_chip);
host->dev = &pdev->dev;
我们使用结构体时如果函数内声明结构体指针变量,如果不给该变量分配空间的话就会出现段错误,可是在内核里,像这段代码,除了host指针变量被分配空间其他的都没分配空间,可是直接赋值也没报错,这是为什么啊?
|