Author Topic: Shockwave's pathetic learning attempts thread...  (Read 18862 times)

0 Members and 1 Guest are viewing this topic.

Offline Paul

  • Pentium
  • *****
  • Posts: 1490
  • Karma: 47
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #20 on: March 14, 2007 »
Quote
know that there are a lot of people here who use various basics that would like to use C++,

Me for one :)
I will bite you - http://s5.bitefight.se/c.php?uid=31059
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #21 on: March 15, 2007 »
Well then Paul, you'd be pleased to know that I have another ropey listing for you then :)

This one is a simple example of the use of arrays in C++
It defines an array, puts some values into it and then loops through them displaying the contents..

Code: [Select]
//
// This is just an excercise I did to see if I could define a simple
// array of integers and print the contents. By Schhlockkwav3 Aemon Holmes
// is boring.
//------------------------------------------------------------------------------

#include <windows.h>
#include <stdio.h>

//
// Every program should have a main.
//

int main()
{
    //
    // Create an array of integers and slap fome values in there.
    //
    int myarray[5] = {100,200,300,200,100};
    //
    // Loop counter;
    //
    int lp;
    //
    // Loop throught each element of the array;
    //
    for (lp = 0 ; lp<5 ; lp++)
   
    {
        //
        // Print  The element No. (Nb. Arrays start at 0 in C++) and the value
        //
        printf("contents of the array Element No.%d Value %d ! J00 Fuxxorz!!!\n",lp,myarray[lp]);
    }
    //
    // All done so wait for the click before leaving.
    //
    MessageBox (0, "J00 FuXX0rz.\nContents Of Array Displayed In Window." ,"This Program Has Finished.", 0);   
    return 0;
}

Apologies again for the basic nature of these sources, but I am afraid it's all I know how to do at the moment :D
Shockwave ^ Codigos
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #22 on: March 15, 2007 »
Hey Shocky.

Well done and congratulations. Arrays are an important step. Same with loops.
I guess in the beginning you are satisfied with the fact that the code runs. But
may I point out a little thing I would do differently.

You define the loopcounter lp before the actual loop. I would do define it
within the for - instruction like this :

for ( int lp = 0 ; lp<5 ; lp++ ) ...

This has the following reason...

First, I think it looks smarter. But second - and this is more important - when you
define the variable lp before the actual loop - it still exists after the loop.
That means, you still allocate the memory for it - and this may be wasty ;)

You can check this by adding the following line right after your loop has finished :

Code: [Select]
printf("lp: %d", lp );
There you see, that the lp variable is still in use and the output would be 5.

Using my way by defining the loopcounter within the for-statement, the language
C automatically frees the memory for it as soon as the loop has ended. You can
easily test that while trying to print out the value of lp after the loop when you
define it within the for-statement. You will get an error - that this variable hasnt
defined concerning the scope.

Here some pseudocode that hopefully makes it clearer ....

//Scope One

int lp;                                      // lp is defined for whole Scope One

     for ( lp = 0 ; lp < 5 ; lp ++ ) { // Scope Two
         int i = 0;                         // i is only defined for Scope Two
     ...
     }

// Scope One
« Last Edit: March 15, 2007 by benny! »
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #23 on: March 15, 2007 »
Got it :D Thanks mate I never knew that trick.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Shockwave's pathetic learning attempts thread...
« Reply #24 on: March 15, 2007 »

nice to see you shocky , learnning C/C++ language , step by step is your successefull way.

so you are doing better.

enjoy c++ coding.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #25 on: March 15, 2007 »
Thanks Emil :) I am finding it pretty tedious going at the moment, not difficult but I know I am going to find it boring until I get my first stuff moving around on screen.

But I know it's a  process I need to go through as some of the help I've had here proves, so I am going slowly and listening carefully :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Shockwave's pathetic learning attempts thread...
« Reply #26 on: March 15, 2007 »

That means, you still allocate the memory for it - and this may be wasty ;)


@benny

I am afraid , this is not correct,  putting the variable inside the for scope is good and will prevent access of it by accident, but will not  deallocate the memory for it as you said.

Look  each C/C++ function will allocate all auto variables in the stack at one time , I.E in the begging and free this memory when exit and return to it’s caller. 

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #27 on: March 15, 2007 »
Also, if you try to build that with most current C compilers, the variable will still be in scope after the for loop anyway.  (C99 and C++ compilers behave the way you say they do).

Jim
Challenge Trophies Won:

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #28 on: March 15, 2007 »
Well even if its not 100% correct, it answers something that I once wondered about.

I started declaring ints for for loops inside the brackets cos it saved out a line of code. I never knew there was a difference until I noticed that when you wanted to use the same variable twice for 2 different loops (in dev c++) like this you had to declare it in both instances. Dint know why that was till now.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #29 on: March 15, 2007 »
btw, as for being Pathetic attempts, we all have to start somewhere. And welldone on your progress dude.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #30 on: March 15, 2007 »
@Emil_halim:
Right. That's what I meant. So when you define the looppointer variable in the,
lets call it, global scope you put it on the bottom of the stack (stacks
are organized LIFO AFAIK ). That means it remains on the stack until the program
or function ends.

PROGRAM-START

int x=0;
{
   int y=0;
  {
      int z=0;
  }
}

PROGRAMM-END


At the point where z is initialized the stack would look like that - or ?

1. z
2. y
3. x

After the first closing "}" I thought because of the LIFO organization the stack
decreases because "z" is taken "away", then "y" and then "z". So the more
variables initialized at the PROGRAM-START increases the STACK-SPACE ???

Please correct me if I am wrong - didn't touch C for some time and with Java
Garbage Collector I don't really think about STACK/HEAP/MEMORY and stuff.
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #31 on: March 15, 2007 »
Quote
Please correct me if I am wrong
You're wrong  :P.  The C and C++ standards don't define anything about how automatic variables are implemented, it's up to whoever writes the compiler.  Could be a stack which grows up or down, could be dynamic allocation, could be registers, could be encoded in the positions of the stars.  It probably works how you say, but it doesn't have to.

Jim
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #32 on: March 16, 2007 »
@Jim:

Well, this is at least what I remember from my C-courses from University. Neverthe-
ess, I am no C-guru - but what I found about stack's and so on seems to be correct.

E.g. : http://en.wikipedia.org/wiki/Stack-based_memory_allocation

A good point you mentioned that C / C++ doesn't define any standards about how the compiler
internally handles the allocation for variables. But at this point it would be nice to know
how the big player in C compiler handle this. ( Everyone knows there is always an exception
from the rule ).
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #33 on: March 16, 2007 »
FWIW I think every compiler I've ever used uses a stack that grows down, exactly as you describe, but that doesn't stop a compiler optimiser moving stack variables into registers and hence not using the stack at all.

Jim
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #34 on: March 16, 2007 »
@Jim:

Right. Since stack is very limited memory I guess there is a lot optimising internally
going on. Nevertheless. Let's say defining the loopcounter inside the for-statement
looks at least more pretty ;)
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #35 on: March 16, 2007 »
Saves on space in the Ide at least, but seriously now because I want to learn this the right way, would either of these methods cause code to fail on some machines?
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shockwave's pathetic learning attempts thread...
« Reply #36 on: March 16, 2007 »
It'll only cause a problem if you want to compile your code as C instead of C++.  If you compile it in C and use the same variable name inside a single function then it won't compile.
eg.
Code: [Select]
void ImOKinCPPbutNOTinC(void)
{
 for (int x = 0; x < 5; x++)
 {
 }
 for (int x = 0; x < 5; x++)
 {
 }
}
won't build in C.

C isn't a subset of C++, though a lot of people think it is.  They're different languages, so you need to know which one you're coding for.

Jim

edit:
In fact, even
Code: [Select]
void ImOKinCPPbutNOTinC(void)
{
 for (int x = 0; x < 5; x++)
 {
 }
}
will only compile with C99 which barely any compilers support yet.
« Last Edit: March 16, 2007 by Jim »
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Shockwave's pathetic learning attempts thread...
« Reply #37 on: March 16, 2007 »
@SW:

I just looked through some old plain ANSI C guides and they all do it
like you did it first, meaning initializing the loopcounter before the actual
for-statement.

So, that could be mean, that my way could causes some errors with some old
ANSI C compiler (not tested - just guessing).

Nevertheless, in all C++ guides and from what I learned nowadays (with C++/Java)
it seems to be rather common to initialize the counter within the for-statement.

All in all, it is important as a programmer to know the different scopes of variables
(concerning the compiler you use). And like Emil says it may help you in a cleaner
coding way and maybe in a better structure/encapsulation of your programs.

Guess you can never be sure that every C/C++ code runs without any modifications
on every C/C++ compiler.

//EDIT: oops, jim was faster  :'(
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Shockwave's pathetic learning attempts thread...
« Reply #38 on: March 16, 2007 »
Thank you guys!

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: Shockwave's pathetic learning attempts thread...
« Reply #39 on: March 16, 2007 »
 Just for the sake of clearance ,  for the C++  beginner , ignore that where the C++ compiler will emit the variables in the stack or in the heap memory.

Always any simple variable such as loop counter , declare it inside the usage scope, any other variables such as classes , use the new keyword that will allocate a heap memory for that variable and will automatically free when the program end.