pouët.net

C++ a wicked language

category: code [glöplog]
there is no 'default' values in C, however, it is in C++ on *objects* since they (usually) set its default values in the constructor.
added on the 2003-05-20 21:00:28 by Hatikvah Hatikvah
"mm, duh finally i get back to my original comment, local variables are certainly not initialized to 0"
huh, ok, not sure, i should check that for myself ;)

sometimes you get some bytes extra when using globals (no stackframe setup for functions without locals), but depends much on compiler optimizations.

stefan: hah, but theres no constructor for a void* ;)
added on the 2003-05-21 01:17:49 by faor faor
faor: i never said there where, did i? i even did asterix around *objects* .. doh :)
added on the 2003-05-21 01:38:42 by Hatikvah Hatikvah
>C was developed with performance and portability in mind. So it will not check if you cross the boundaries of an array, because this check costs performance

Yes,. but if so then why doing this only in the debugging mode?
added on the 2003-05-21 09:46:38 by Optimus Optimus
For example, quickbasic does the check when you run in the interface but not in the final compiled EXE for performance too. It's free to write out of bounds there too, but at least you can debug easilly inside the compiler where it prompts you if you are out of bounds..
added on the 2003-05-21 09:48:45 by Optimus Optimus
Correction: Yes,. but if so then why NOT doing this only in the debugging mode?
added on the 2003-05-21 09:49:35 by Optimus Optimus
Ok,. perhaps because quickbasic works as an interpreter inside the environment and so it can check line per line while running. But there must be a helpfull debuggin logic that could work out well in true compilers too!
added on the 2003-05-21 09:51:53 by Optimus Optimus
Hello, Optimus. Remember i gave you a C++ book - it's good, it *will* teach you C *and* C++ - and you will understand it all perfectly. (Thinking in C++) It even teaches you safe programming practice.
I must confess that i am a traditionally Pascal/Delphi programmer. I started coding C because i expected go get more help (oh, yeah... got none!). Recently i learned C++ and explored a number of other languages. Let me sum up my impressions:
- i tried to code some OO in C. It really bit me. You cannot maintain a project like that. I had to learn C++.
- the problem with C++ OOP is that it's very impure. e.g. access classes through explicit pointer or reference, allocation on stack, and so on... C++ deals with any possible case, but it complicates the language a lot. But i think one cannot appreciate OO programming in C++ without having understood it in a "normal" language and tried it in C "the hard way".

For learning and even for demo c0ding, i would recommend Sather, which is a simple language without many limitations. It is based on 100% pure OO. I can prove that it can generate way more efficient code than C++ -- at the cost of some justified bloat. I believe i have to write an article, since the language is not very well markted, however extremely simple and extremely powerful. And documentation is very well written.

The large problem of every language except for C and C++ are libraries - you have to write import units in D Programming Language, Sather, Eiffel, GNU Pascal, ... or even slow wrappers in the case of OCaml.

Last word: OPTIMUS, DON'T GIVE UP!
ASK MORE QUESTIONS! I will glagly explain you anything.
Maybe C++ is a wrong choice for a start.
added on the 2003-05-21 12:54:26 by eye eye
Hi midiclub!

Thank you very much for your comments. They are really very helpfull and motivating. As well as your help and that you gave me the link of the PDF of that book.

Two problems I have are, I don't like to read a lot of stuff, I am bored to get into many languages. It's really really lot for me and I prefer to code some algorithms instead in what I know. One reason why I avoid learning C++ (all the books are very big) and also why I haven't checked other alternative languages than the very common ones, like Sather as you say. I don't know, I would like to check it, but perhaps I will prefer to stick in the book you suggested, which is still a pain in the ass. But that's what might happen in the end..

But very much special thanks for your help and your comments, it makes me really motivated to overpass what keeps me from getting started..

P.S. The animated avatar you have, is the best animated I have ever seen on pouet! Who transfered this one?
added on the 2003-05-21 13:14:17 by Optimus Optimus
Optimus: C/C++ is freedom but that freedom is a double edged razor (dikopo mahairi :-) It does not check array boundaries and other similar stuff. This leaves the programmer the freedom to cross array boundaries if they wish to, and C will not even complain about it. Some programmers like that and in fact they find it very usefull. On the other hand, if you cross array boundaries by mistake, you pay the price for misusing your freedom :-) That is a core philosophy of the C language, and either you love it or you hate it.

I, like you, also like to be helped / protected by the compiler. I want the compiler to tell me that I've done a mistake and I want the compiler to instruct me how to write clean and well structured code. That's why I like Pascal/Delphi so much. Most people in here, though, want to be completely free and write however they like. Maybe that's why recent demos crash so much :-P Anyway, take a good look at the tools available, find your favourite one and use it.

added on the 2003-05-21 13:30:21 by moT moT
..except that programs which cross array boundaries are not usually(*) conforming C programs.

char *p=malloc(5)+6; p-=6; // WRONGLIHOOD!!1
char buf[5][5]; char *p=buf[0]; p+=6; // (*) this is ok though
added on the 2003-05-21 18:37:19 by 216 216
Optimus:
You don't need everything that is written in the books. Read the basics, then browse the book, read into the remaining chapters and decide if you need it. Use online tutorials to get a different (inexpensive) view from other people.
added on the 2003-05-21 19:01:10 by ie ie
And if you write char *c = (char *)random(); than you are on an adress somewhere in memory.. But the clue is that you do not have to use this pointer.
joking around.. Don't try this at home kids: *c = 1..
added on the 2003-05-21 19:10:53 by mad mad
char *vidmem = 0xA000;
void init() {
__asm {
mov ax,13h
int 10h
}
}
void putpix(int x, int y, char color) {
vidmem[ 320*y + x ] = color;
}

mod13 could be an easy start into C, maybe ?
added on the 2003-05-22 00:17:22 by faor faor
I've tried the code above for an console application and the int call forced an error... And why it is 0xa000 thats for segment (probably an segment adress) offset calls like turboc or turbopascal.. But wait in .net there are functions to use int10 with fullscreen but they seem to be sick to implement.. Look for int10_BIOS_ARGUMENTS and related.. Perhaps Ive written faxenquatsch...
added on the 2003-05-22 11:56:38 by mad mad
hi y'all!
dunno if it really is the correct place but wtf; just wanted to let you know in case someone
wants to try a new tool :)
i've been working on a (JIT accelerated) script engine for some time now; it's still work in progress but already
usuable.
it's rather close to "c" (and you can even find traces of OOP support) and it uses OpenGL as the primary graphics API.
it has pointers but in a more simple way than the rather cryptic *,-> c notation
it also comes with a set of API classes that help you with the regular (boring) stuff like key/mouse/window handling,
textures,samples.. the API was written in C++ (resp. wraps C libraries) but I'll include "c" function tables
for frequently used classes (Vector,Matrix..) in future releases
(which means that api calls will then be as fast as a real c compiler)
it's freely available for windows+linux (in terms of binaries for now, source release is planned).
it uses the SDL HAL.

it can be used to write portable (and very compact in terms of file size) intros/demos (think of diskmags,
musicdisks, (small)games and stuff like that). experienced "c" programmers can use it to prototype
new effects/algorithms which can be easily ported (back) to c.
(on the other hand, GL calls are really fast (vc6 needs an additional call (gl + fixesp))..)

by concept it doesn't include a (3d) engine but it gives you some building blocks needed to write a (3d) engine.
there currently is a small 3DS loader and a (very very basic) scenegraph but they will be removed
from the API soon (to be replaced by scripted classes)

maybe some of you want to try this one; constructive feedback is always welcome!



p.s.: IMHO, OO is a matter of concept, not of implementation. OO in "C" _is_ possible (the drawback is that a C compiler doesn't help you with OO programming at all (in terms of warnings, errors..))

p.p.s: here's a small example so you can see how my language looks like:

// ---- simple example that demonstrates how to load and fade in/out a 2D image
// ---- (the classes Application, ClampedFloat, Configuration, Texture and Viewport
// ---- are part of the language API)
Texture tex;
ClampedFloat cf; cf.init(0.0, 1.0/45.0, 0.0, 1.0); /** fade counter **/
int exitnow=false;
function onReopen() {
tex.loadLocalImage("pixel.png", 256, 256, 4); /** load and scale to 256x256x32 bits (4x8) **/
tex.flags=TEX_MODULATE|TEX_MAGFILTERLINEAR|TEX_MINFILTERLINEAR;
}
function main() {
Application.initSubsys(SUBSYS_VIDEO);
Viewport.openWindow(320, 200);
Configuration.tickInterval=1000.0/50.0; // controls fps.precision
onReopen(); // (re)load texture
bindkey "escape" { exitnow=30; cf.add=-1.0/30.0; } ; // key binding press + release statements
use callbacks; // autobind onReopen,onDraw callbacks
Application.exec(); // enter application event loop
}
function onDraw() compile {
/** render gouraud shaded and textured quad...**/
float sx=Viewport.width; // set quad width
float sy=Viewport.height; // set quad height
Viewport.init2d(sx, sy); // set up coordinate system
glEnable(GL_TEXTURE_2D); // enable 2D texture mapping
tex.bind(); // activate Texture
int c8=cf.value*255; // set brightness
glQuads(); // begin drawing of "quad" primitive
glColor4ub(c8,c8,c8,c8); // set foreground color to fade in picture
glTexCoord2f(0, 0); // set point1 u,v coordinates (left/top)
glVertex2f(0, 0); // set point1 x,y coordinates
glTexCoord2f(1.0, 0); // set point2 u,v coordinates (right/top
glVertex2f(sx, 0); // set point2 x,y coordinates
int c8r2=c8>>2;
glColor4ub(c8r2, c8r2, c8>>1, c8r2); // change foreground color to create color spread
glTexCoord2f(1, 1); // set point3 u,v coordinates (right/bottom)
glVertex2f(sx, sy); // set point3 x,y coordinates
glTexCoord2f(0, 1); // set point4 u,v coordinates (left/bottom)
glVertex2f(0, sy); // set point4 x,y coordinates
glEnd(); // end primitive drawing
Viewport.waitVBlank();
cf.tickPrecise(FPS.precision); // smooooth animation pls (measure frame time)
if(exitnow) if(!--exitnow) // if esc was pressed then count down to 0; then exit
Application.exit();
}

p.p.p.s: ..and it has an x86/x87 FPU inline assembler (no kidding) (and no complex addressations possible..)

p.p.p.p.s: ..the boundary checks give you hints in the debug mode (interpreted mode, -fi option) about crossed boundaries

p.p.p.p.p.s: ..the boundary checks can be disabled using the -nbc command line option

p.p.p.p.p.p.s: this is _not_ a commercial project so I hope you don't mind the "advertisement"

p.p.p.p.p.p.p.s: now i gotta pee
added on the 2003-05-22 14:46:09 by fli fli
fli: i will definitely try that! sounds interesting. but why havent you used an existing language like python? it has easy to use extension interfaces (embedding/extending), and lots of libs.

"I've tried the code above for an console application and the int call forced an error..."

argh should have said that: you cant do that in protected mode (windows) or use an older compiler, that creates dos code, or djgpp ( http://www.delorie.com/djgpp/doc/brennan/brennan_access_vga.html )

.net and int10h? dunno, but strange ;)
added on the 2003-05-22 19:27:19 by faor faor
faor: can you do 60.000 vertices/frame @85 fps on a geforce2 (on a 1ghz thunderbird) with python? :)

added on the 2003-05-22 21:11:01 by fli fli
what i've said.. think it's strange to.... and it musn't be ever the vertex/polycount that makes good code... but nice to tell.. hoho ho..
added on the 2003-05-22 21:23:55 by mad mad
ho ho
added on the 2003-05-22 21:24:38 by mad mad
fli: haha, dunno :) but 'should' be possible, if one does it right.
added on the 2003-05-23 11:23:53 by faor faor
"C/C++ is freedom but that freedom is a double edged razor (dikopo mahairi :-) It does not check array boundaries and other similar stuff."

Well, it does in debug builds, just not in release builds. So what's your point?
added on the 2003-05-23 13:25:10 by sagacity sagacity
Faor: tell.... Me.. that..
added on the 2003-05-23 18:18:02 by mad mad
sagacity: I am merely saying that, by design, C/C++ does less checking on what code you write and how well you write it. This gives the programmer more freedom to write as he pleases and sometimes a little more performance as well. Unfortunately, it also makes it possible for the programmer to screw up the code alot easier than, say, pascal or java. Those other two are more picky on how you write, enfocing the programmer to use specific styles, like classes, strong type definitions and logical control flow (goto and break are strongly discouraged in pascal/delphi for example).

Now, depending on what type of programmer you are, you may wish more freedom than guidance, or the opposite. Since Optimus likes to be protected by the framework/compiler and warned when he writes something wrong, I just suggested whether he should check something other than C/C++. The final desicion of course is up to him. :-)
added on the 2003-05-23 18:48:24 by moT moT
Mad: DirectDraw in DirectX 8 and DirectX 9 has no difference from DirectX 7, so Microsoft didn't documented it. However it's still there because DirectX has to be compatible with earlier version because it's COM (...they say...).

I don't know why they didn't documented it, probably they want to push programmers to code using Direct3D. I actually think -but never checked since i prefer DX7 docs for this jub- that DirectDraw docs are mixed with Direct3D docs.
added on the 2003-05-25 19:45:10 by BadSector BadSector

login