微控制器的内存分配 [英] Microcontroller memory allocation

查看:184
本文介绍了微控制器的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思考的每天约以下问题:

I've been thinking for day about the following question:

在一个普通的PC机,当你分配一些内存,你问它是跟踪哪些内存段被占领,哪些不是,不要让周围的你惹其他程序内存等操作系统
但是对于微控制器是什么,我的意思是微控制器没有一个操作系统上运行,所以当你问了一堆的记忆是怎么回事?你不能简单地接取存储器芯片和接取随机的地方,否则可能会被占用......谁跟踪哪些内存部分已被占用,并为您提供一个免费的地方来存储的东西吗?

In a common pc when you allocate some memory, you ask for it to the OS that keeps track of which memory segments are occupied and which ones are not, and don't let you mess around with other programs memory etc. But what about a microcontroller, I mean a microcontroller doesn't have an operating system running so when you ask for a bunch of memory what is going on? you cannot simply acess the memory chip and acess a random place cause it may be occupied... who keeps track of which parts of memory are already occupied, and gives you a free place to store something?

编辑:

我编程用C微控制器...我想,答案可能是语言无关。但让我更清楚:supose我有这个计划对微控制器上运行:

I've programmed microcontrollers in C... and I was thinking that the answer could be "language independent". But let me be more clear: supose i have this program running on a microcontroller:

int i=0;
int d=3;

是什么让确保我的I和D变量没有存储在内存中的同一个地方?

what makes sure that my i and d variables are not stored at the same place in memory?

推荐答案

我觉得意见已经涵盖了这...

I think the comments have already covered this...

要问的内存意味着你有一些操作系统的内存管理,你是从mallocing(使用期限操作系统的一个松散的感觉)。首先你不应该在微控制器作为一般规则来mallocing内存(我可能会火烧的发言)。在案件做到,但你是在你的记忆控制,你自己的系统与应用程序,要求存储装置问自己吧。

To ask for memory means you have some operating system managing memory that you are mallocing from (using a loose sense of the term operating system). First you shouldnt be mallocing memory in a microcontroller as a general rule (I may get flamed for that statement). Can be done in cases but you are in control of your memory, you own the system with your application, asking for memory means asking yourself for it.

除非你有原因,你不能静态分配的结构或数组或使用工会是否有可能都希望大部分或全部的空闲内存的互斥code的路径,你可以尝试动态和自由分配但它是一个困难的系统工程解决的问题。

Unless you have reasons why you cannot statically allocate your structures or arrays or use a union if there are mutually exclusive code paths that might both want much or all of the spare memory, you can try to allocate dynamically and free but it is a harder system engineering problem to solve.

有是存储器运行时分配和编译时间之间的差。你的例子无关的问题的其余部分。

There is a difference between runtime allocation of memory and compile time. your example has nothing to do with the rest of the question

INT I = 0;
INT D = 3;

int i=0; int d=3;

在编译时编译器分配的。数据为每个这些项目中的两个位置。链接器和/或脚本管理。数据在哪里生活和工作有什么局限性的大小,如果是。数据比你应该得到的是可用连接器警告较大,如果没有的话,你需要修复链接器的命令或脚本,以配合您系统。

the compiler at compile time allocates two locations in .data one for each of those items. the linker and/or script manages where .data lives and what its limitations are on size, if .data is bigger than what is available you should get a linker warning, if not then you need to fix your linker commands or script to match your system.

运行时的分配是在运行时管理和地点及如何管理内存是由该库决定的,即使你有足够的内存错误或不当的书面库可以重叠的.text,.data和.bss和/或堆叠并引起很多问题。

runtime allocation is managed at runtime and where and how it manages the memory is determined by that library, even if you have plenty of memory a bad or improperly written library could overlap .text, .data, .bss and/or the stack and cause a lot of problems.

过度使用栈也从非嵌入式系统未来的一个pretty严重的系统工程问题是这些天常常被忽视,因为有这么多的内存。与微控制器的嵌入式code打交道时,这是一个很现实的问题。你需要知道你的最坏情况下的堆栈使用,留有余地,至少有那么多的内存,如果你将有一个堆动态分配,或者即使你静态分配。

excessive use of the stack is also a pretty serious system engineering problem which coming from non-embedded systems is these days often overlooked because there is so much memory. It is a very real problem when dealing with embedded code on a microcontroller. You need to know your worst case stack usage, leave room for at least that much memory if you are going to have a heap to dynamically allocate, or even if you statically allocate.

这篇关于微控制器的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆