Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #23495
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.arch.embedded |
| Subject | Re: Use RAM to modify a field |
| Date | 2016-04-05 08:17 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <ndvl2u$bge$1@dont-email.me> (permalink) |
| References | <9b2dncOkiOffiWLLnZ2dnUU7-cnNnZ2d@giganews.com> <Gvydnbux9K3U-J3KnZ2dnUU7-N-dnZ2d@giganews.com> <lbCdnUNhU6t8Ap_KnZ2dnUU7-YPNnZ2d@giganews.com> <mJudnSzjsJI5OJ_KnZ2dnUU7-R-dnZ2d@giganews.com> |
On 04/04/16 19:34, Tim Wescott wrote: > On Mon, 04 Apr 2016 12:09:53 -0500, srao wrote: > >>> On Fri, 01 Apr 2016 19:37:22 -0500, srao wrote: >>> >>>> I need to use Copy and 8K block of data (from a parameter block >> address >>>> in flash) to RAM, modify data at a particular offset and copy the >>>> contents back to flash. The size of the RAM is 256K. I believe >> dynamic >>>> memory allocation with heap (using a pointer)should work for this? >> Would >>>> like to know if this would be the right way togo? >>>> Note: Please notify if any detail seems missing. >>> >>> A general description of what you're doing would be useful. >>> >>> If it's an embedded system and it has to run forever, using the heap is >>> probably a bad idea (Google "heap fragmentation"). Truly paranoid* >>> embedded engineers would allocate the block statically unless that makes >> >>> the system run out of RAM. If they did NOT allocate it statically, then >> >>> they would ask themselves if they have enough memory in the first place. >>> >>> * In embedded, "paranoid" often means "successful" >>> >>> -- >>> >>> Tim Wescott Wescott Design Services http://www.wescottdesign.com >> >> Also, >> >> Would static allocation utilize RAM space? I am using this for an >> embedded application which has to read data out from hardware, store it >> in RAM and modify the field. >> >> The RAM here is not part of processor.A separate flashchip has >> boot+flash+ RAM region which the application uses. C++ borland compiler >> in use. Does static memory allocation use space out of RAM? Or does it >> depend on the compiler? > > I believe that you are asking if using the "static" keyword in some C or C > ++ code would cause the tools to put the space in RAM. > > The answer is that if the tools are working correctly, and if the space > is not declared as "const", then yes. The tools working correctly > depends on both the tools (Borland was still good the last time I used > it) and how they're set up, though, so I can't just say that it WILL work > for you based on the fact that it's Borland. > > HOWEVER -- if you're declaring a variable outside of any struct, > function, or whatever, then "static" is telling the compiler about who > can see the variable, not whether the variable is writable (and thus in > RAM) or not. > > Here's what SHOULD happen in most embedded systems, in C or C++, if you > declare a variable outside of any struct, function, or whatever: > > // "whatever" gets put in read-only memory, and is visible outside of > // the resulting object file > const sometype whatever = <inital value>; > > // "whatever" gets put in read-only memory, and is only visible to > // things within the C file: > const static sometype whatever = <initial value>; > > // "whatever" gets put in RAM, is visible outside of > // the resulting object file, and (if the tools are set > // up correctly) filled with 0 bytes: > sometype whatever; > > // "whatever" gets put in RAM, is only visible to > // things within the C file, and (if the tools are set > // up correctly) filled with 0 bytes: > static sometype whatever; > > HTH > > If David Brown says that I'm wrong, just fill in a resigned sigh from me > and believe what he says. > That's my cue for the nit-pick! Your explanation is a good start, but there are a couple of other points that I think are worth mentioning. It is not clear if the OP is actually using C or C++ (his compiler supports both). But in the case of const sometype whatever = <initial value>; there is a difference. In C, the "whatever" has external linkage by default - it is visible outside of the current object file, assuming there is a matching "extern" declaration. In C++, "whatever" has internal linkage by default - it acts like "static const", unless there is a matching "extern" declaration visible in the defining translation unit. (The external visibility does not affect whether the object is put in read-only or writeable memory, of course.) Personally, I always make my file-level functions and data either explicitly "static" (even in C++, in order to be consistent with my C code), unless it has to be externally visible - in which case there is always an "extern" declaration in the module's header file which is #include'd by both the defining module and other modules that use it. That keeps everything clear and consistent. And I prefer to write "static const" rather than "const static" - the compiler accepts both, of course, but I am more fussy! Also note that when you make something "const", the compiler/linker does not have to put it into read-only memory. It /may/ put it in read-only memory, and any attempt to change it in the program is undefined behaviour, but it could equally well use ram. On a PC, for example, it will go in ram - but it will be put in a different segment from read-write memory, and the segment will be marked read only. On an AVR (which has different cpu instructions for accessing flash and ram), const data is usually put in RAM so that it can be accessed using the same instructions as non-const data. We don't know enough about the OP's system to know where static const data would end up. And of course if the compiler feels it doesn't actually /need/ to put static const data in memory, due to the way it is used (or not used), it will not put it in any memory.
Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-01 19:37 -0500
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-02 19:35 -0500
Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-04 08:55 +0200
Re: Use RAM to modify a field Jack <jack4747@gmail.com> - 2016-04-04 00:52 -0700
Re: Use RAM to modify a field Grant Edwards <invalid@invalid.invalid> - 2016-04-04 14:24 +0000
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:25 -0500
Re: Use RAM to modify a field upsidedown@downunder.com - 2016-04-04 21:42 +0300
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:41 -0500
Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:02 +0200
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 11:42 -0500
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 12:09 -0500
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 12:34 -0500
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 13:41 -0500
Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 18:49 +0000
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 15:46 -0500
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 16:32 -0500
Re: Use RAM to modify a field Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-04 21:48 +0000
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:55 -0500
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-04 17:49 -0500
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 12:39 -0500
Re: Use RAM to modify a field Tim Wescott <seemywebsite@myfooter.really> - 2016-04-05 15:55 -0500
Re: Use RAM to modify a field lasselangwadtchristensen@gmail.com - 2016-04-05 14:45 -0700
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 19:22 -0500
Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:29 +0200
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-04 15:50 -0500
Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:38 +0200
Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:11 -0500
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:46 -0500
Re: Use RAM to modify a field David Brown <david.brown@hesbynett.no> - 2016-04-05 08:17 +0200
Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 09:58 +0300
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 11:54 -0500
Re: Use RAM to modify a field Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2016-04-05 22:46 +0300
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-05 18:21 -0500
Re: Use RAM to modify a field Reinhardt Behm <rbehm@hushmail.com> - 2016-04-06 12:58 +0800
Re: Use RAM to modify a field "srao" <109468@EmbeddedRelated> - 2016-04-06 11:57 -0500
Re: Use RAM to modify a field Dave Nadler <drn@nadler.com> - 2016-04-06 10:20 -0700
Re: Use RAM to modify a field Tim Wescott <tim@seemywebsite.com> - 2016-04-05 11:04 -0500
csiph-web