Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1166
  • Last Modified:

Limitations of C++/high level programming

While C++ includes a lot of useful classes, templates and functions, the actual operation of any given algorithm basically comes down to a few keywords, like if, else, for, while, new, delete, etc,.  

Now, with those basic keywords, it seems that almost any imaginable algorithm or procedure is possible.  But isn't there one big exception?  Isn't it basically impossible, using C++ or C, to write code that directly manipulates specific computer hardware such as video cards, ethernet cards, etc?  

I realize there are libraries to do just that, like the C socket library and such, but aren't those libraries written in assembly with machine-specific code?  Wouldn't it be impossible to write a program from scratch, in C++, using ONLY basic C++ keywords and without including inline assembly code, that accesses specific hardware?
0
chsalvia
Asked:
chsalvia
5 Solutions
 
Infinity08Commented:
It depends on the operating system below, but in general you're right. Inline assambly is however commonly used to access hardware on the lowest level, and is a valid solution to this "problem".

To elaborate a bit on my first remark : some operating systems provide an API that gives low-level hardware access, or some other means (file descriptors eg.). As an example, take a look at the DDK for Windows (a quick intro : http://www.adp-gmbh.ch/win/misc/writing_devicedriver.html).
0
 
WelkinMazeCommented:
chsalvia, you are right. In fact even the basic keywords are transalted to assembly commands (binary code) when they are compiled.
And to access any resources of the computer (monitor, keyboasrd, so on) you have to use (although indirectly in the most cases) machine specific instructions (assembly).
0
 
grg99Commented:
>that accesses specific hardware?

Yes and no.

Some hardware is accessed through using "special" CPU instructions like IN and OUT.  Very few high-level languages have the ability to generate these instructions right off.  But most compilers come with a  library of routines that implement IN and OUT as simple function calls.

Some other peripherals are accessed through accessing certain special addresses that are not backed by RAM, but by the peripherals themselves.
For these you can access them through most any language that lets you set pointers to point to arbitray addresses, like C and C++.

It's also helpful to have a compiler with something like a "volatile" and "interrupt" keywords, so you can be sure the addresses do get twiddled, and so you can set interrupt handlers if needed.

As an example, most of the device drivers in Windows and Linux are written in C and C++.  No major problems there.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chsalviaAuthor Commented:
grg99,

>Some other peripherals are accessed through accessing certain special addresses that are not backed by RAM, but by the >peripherals themselves. For these you can access them through most any language that lets you set pointers to point to >arbitray addresses, like C and C++.

So, are you saying that machine-specific hardware can be accessed directly via C/C++ by assigning a pointer to a memory address that is used by the hardware?

Some questions I have about this:

1. Why would that not cause a segmentation fault, given that you would be accessing an address that is out of the scope of your program?

2. How would you know the address to set the pointer to?
0
 
WelkinMazeCommented:
Yeap, there are such memory addresses. I've used them about ten years ago in DOS programming.
Look here for example http://www.techadvice.com/tech/M/mem-adr.htm
But anyway everything is finally transalting to machine dependant assembly commands. There is no other way to execute any code on a particular machine.
0
 
List244Commented:
Chsalvia, to know the address of something you are going to need some sort of guide to that hardware.
Just like in assembly you have to sort of know what you are doing.  As far as accessing memory outside
of the scope of your program, this is legal.  There is no problem with doing this, that is the reason we are
able to do it.  The problem is again, you have to know what you are doing, if you start randomly changing
things in memory and such without knowing what you are doing, you are likely to cause a crash.
0
 
imladrisCommented:
C/C++ is eminently capable of such things. Kernighan and Ritchie originally created C, in part, in order to demonstrate that it was possible to (largely) write an operating system in a high level language.

On the flip side, unless you are an operating system or driver developer, it is usually not a good idea to attempt such things. The machines hardware is "wrapped"/managed by the operating system for many very good reasons.

1. Why would that not cause a segmentation fault, given that you would be accessing an address that is out of the scope of your program?

Because the running code has kernel priviledges, for instance.

2. How would you know the address to set the pointer to?

By reading the hardware documentation.
0
 
chsalviaAuthor Commented:
Okay, thanks everyone for your comments and insight about this issue.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now