-14

Are there any ways in C and C++ to control the memory ram and registers according to our need? Eg. movement of data in ram from one location to other, changing values in registers, etc?

Is it possible in Java???

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • 7
    What is your "need"? Are you programming for a minimal device? – Jongware Aug 03 '13 at 19:03
  • I guess he is referring to something which is done on embedded devices and also with processor registers. – Uchia Itachi Aug 03 '13 at 19:04
  • General purpose registers depend on their availability on a processor(don't have memory)..variables that you use in a c/c++ program refer to memory locations in RAM – boxed__l Aug 03 '13 at 19:05
  • moving data can be done with most languages but having exact control is platform specific (hardware and OS). using registers is platform specific and standard C++ does not specify this. Some compilers allow inline assembly in C++ code or give intrinsics to expose registers and platform specific functions – a.lasram Aug 03 '13 at 19:05
  • 4
    C does have a "register" keyword. In the mid 1980's, I could get significant performance improvements in many functions by using it carefully. More recently, compilers have got so good at using registers effectively that I stopped being able to get any benefit. – Patricia Shanahan Aug 03 '13 at 19:05
  • 1
    If you're programming on a desktop or server, the OS and compiler can do a million times better job of this than you can. Even on a smart phone/tablet OS, it's unlikely you need to do this. If you're programming an embedded system without an operating system, then you can and need to do all this stuff. – Paul Tomblin Aug 03 '13 at 19:07
  • why on the earth you would want to move data on RAM? – Ankit Aug 03 '13 at 19:10
  • 2
    @PatriciaShanahan - the other thing to keep in mind is that the Intel architecture has a completely pitiful number of registers compared to a Vax or SPARC or other popular architecture. On Intel, there are barely enough registers for the compiler's own housekeeping. – Paul Tomblin Aug 03 '13 at 19:11
  • What does this have to do with Java? You didn't mention JNI/JNA. – nanofarad Aug 03 '13 at 19:12
  • If anything, Java gives you less control on the arrangement on the assumption that the JVM will do a better job and you don't need to worry about such issue (true or otherwise) – Peter Lawrey Aug 03 '13 at 19:22
  • 1
    Yes, the tool that helps you is called a compiler. – brian beuning Aug 03 '13 at 19:36
  • The conventional way to move data from one location to another, as well as modifying values, in C++ is called "assignment". It's probably mentioned in your favourite book. – molbdnilo Aug 03 '13 at 20:05
  • Thanx every1 for their comments.. but i want to design a simpler os which can do a bit of memory management etc thats y if eg ram memory gets full then os must virtualize the hard disk and ram must b available.. for virtualizing the hard disk i need to move data of ram from ram to hard disk.and same for registers.so thats the problem plz help.. – Shubham Grover Aug 28 '13 at 18:26

2 Answers2

3

For memory management you should consider using a Memory Pool. Link.

Though you shouldn't be reinventing the wheel. Use a library instead that provides a clean templated interface to memory pools. Avoid malloc and memcpy as much as possible.

If you wan't to play with the registers you can include assembly code. Link.

Community
  • 1
  • 1
gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17
1

I am not sure to understand your question, which is operating system, processor, and compiler specific.

With recent GCC you could do some of it (for instance, reserve registers to avoid them being used). And you could also customize the compiler (e.g. with MELT) to suite more needs. But such a customization means at least weeks of efforts.

You could also make a new backend in GCC (but this means months of work)

And recent standard C++11 library has notably std::allocator and a lot of memory management related stuff.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547