Author Topic: C/C++ programmers.. need some help.  (Read 20662 times)

Offline reaper

  • Opulent Member
  • *
  • Posts: 2872
  • Nice night for a walk, eh? - Nice night for a walk
    • View Profile
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #45 on: March 28, 2007, 02:34:41 AM »
Quote from: console
Actually, look again at that for-loop in my example:
« Last Edit: March 28, 2007, 02:47:24 AM by reaper »
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
VaeVictus "reaper is a lying sack of shit and ragequit then had, probably slugs, come alias and beat me, wasnt even the same person playing OBVIOUSLY, accuracies basicly doubled, and strategy

Offline console

  • Brobdingnagian Member
  • ***
  • Posts: 4518
  • "Man, this is the way to travel," said my attorney
    • View Profile
    • tastyspleen.net
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #46 on: March 28, 2007, 08:42:14 AM »
Quote from: console
Actually, look again at that for-loop in my example:  for (int i = 99; .............

for (i = 0; i < note->GetTitle ().GetLength (); i++)

the line above  gave the error during compilation, well.. a few lines before.  nowhere did i see "i" declared as a variable, but i figured it was declared somewhere, because i know C isn't like VB, so you have to say what type your variables are.

This is exactly why Microsoft's non-standard "forScope" scoping rule sucked so bad.  Take a look at the whole method:

Quote


void CMainFrame::CreateMinimizedMenu(BCMenu *pMainMenu, BCMenu *pMinimize)
{
// Show the minimized notes on the system tray menu (max 20)
    pMainMenu->EnableMenuItem (6, MF_GRAYED | MF_BYPOSITION);

    for (int i = 0; i < 20; i++)
        pMinimize->RemoveMenu (0, MF_BYPOSITION);

    int iCount = 0;

    POSITION pos = m_pNotes.GetHeadPosition ();

    while (pos) {
        CNote *note = (CNote *) m_pNotes.GetNext (pos);     

        if (!note->GetVisible () && iCount <= 20)   {

    USES_CONVERSION;
    char *pTemp = (char *) malloc (note->GetTitle ().GetLength () + 1);
    for (i = 0; i < note->GetTitle ().GetLength (); i++)
        pTemp[ i ] = note->GetTitle ().GetAt (i);
    pTemp [note->GetTitle ().GetLength () + 1] = '\0';
    TCHAR* szWide = A2T( pTemp );
    delete pTemp;

            pMinimize->AppendMenu (MF_STRING ,ID_MINIMIZEDMENUITEM + iCount, szWide);
            pMainMenu->EnableMenuItem (6, MF_ENABLED | MF_BYPOSITION);

            iCount++;
        }
    }
}

The 'i' that was declared in the top for-loop ends up being visible outside the scope of that loop.  So it's still declared when we get to that second for-loop below.  Thankfully, MS has finally turned that crap off by default in their latest compiler.


i understand C like this (it's been a while):
in C you have to declare your variables, like x is an int, or a float, double, whatever.  you can do this within a function, and they are local to the function, globally in function main, as well as some "extern" way, if you wish to use variables from another linked .o file?

say you have two .c files and you use a variable declared in one .c file in the other .c file,  you have to declare these variables as extern or something (or did this extern thing have to do with function prototypes)? 

you link the files together when you create your .exe,  the .c files can work together, so you could call a function in myfunction.c  from main.c .  you would compile them, and then link the .o files.   if the variable is in function main, every linked file can use the variable, but if it's not in main, it needs to be passed as extern, or the value of the variable passed back from the function.  i don't expect a detailed answer, i was kinda wondering how correct this is.  i'm gonna have to go back and learn most of this stuff, i did ok, but i never really got a good grasp of structures, and i always got the feeling if you wanted to code anything really good, you needed to use those things : P

You're definitely on the right track.  Except for the part about "if the variable is in function main every linked file can use the variable"--that's partly correct but I wanted to point out there's no special case for the main() function or anything.  The general rule is that if a variable is declared on the stack, you could pass the address of that variable for temporary use by any subroutine.  Technically since the entire rest of the program is subroutines from main()'s point of view, then, yes, you can expect variables declared on the stack in main() to be around for the life of the program.  However, subroutines don't have any special means of getting access to such variables.  Main would have to pass the address of its variables to any subroutine that wanted to use them.


i had pointers and all that stuff down, then i think they switched stuff up on me : ( .. like now there are classes, and methods - and i don't know what these things are : )

Classes are kind of like a data structure that you call functions on, instead of accessing its data fields directly.  This allows the data fields to be private to the outside world, and from an outside perspective you simply tell this data structure what to go do with itself  8), by calling its functions (called methods).

Here's an example in both C++ and Ruby.

Quote from: ruby
class BeerBar
  def initialize(num_bottles)
    @num_bottles = num_bottles
  end

  def any_beer_left?
    if @num_bottles >= 1   
      puts "#{@num_bottles} bottles of beer on the wall! #{@num_bottles} bottles of beer!"
      true
    else
      false
    end
  end

  def take_one
    @num_bottles -= 1
    puts "Take one down, pass it around! #{@num_bottles} bottles of beer on the wall."
  end
end

# ok we've defined our BeerBar class, now let's construct an instance of the class and use it:

bar = BeerBar.new(99)
while bar.any_beer_left?
  bar.take_one
end

Quote from: c++
#include <stdio.h>

class BeerBar {
  public:
 
  BeerBar (int num_bottles) :
    m_num_bottles(num_bottles)
  {
  }

  bool any_beer_left ()
  {
    if (m_num_bottles >= 1) {
      printf("%d bottles of beer on the wall! %d bottles of beer!\n",
             m_num_bottles, m_num_bottles);
    }
    return m_num_bottles >= 1;
  }

  void take_one ()
  {
    --m_num_bottles;
    printf("Take one down, pass it around! %d bottles of beer on the wall.\n", m_num_bottles);
  }
 
  protected:
 
  int m_num_bottles;
};

// ok we've defined our BeerBar class, now let's construct an instance of the class and use it:

int main ()
{
  BeerBar bar(99);

  while (bar.any_beer_left())
    bar.take_one();

  return 0;
}

In both languages, we end up with a class called BeerBar.. and we can create as many instances of that class as we like. We could create tons of these bars and pass them all throught the program. Each bar would have its own private data keeping track of how many bottles it has left.

E.g.  BeerBar bar1, bar2, bar3;

The feature of classes we have covered so far is called encapsulation. (Hiding the details of how a class works, providing a set of public methods as an interface.)

You can read more about encapsulation, and other features of classes such as polymorphism and inheritance here:
http://en.wikipedia.org/wiki/Object-oriented_programming


Regards,

quadz
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus

Offline Whirlingdervish

  • Super ShortBus Extravaganza
  • Illimitable Sesquipedalian Member
  • *
  • Posts: 6384
    • View Profile
    • The Dervish Depository
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #47 on: March 28, 2007, 08:57:55 AM »
DAMN YOU QUADZ AND YOUR QUICK FINGERS!

this was my reply to reaper that I was writing as you replied:

classes are just a programmer defined type of object that you can assign any number of functions to, that allow you to hide information from other parts of your program.
they help streamline things, create security of sorts, and are important in all forms of object oriented programming..

say you make a class called "Frog"..

You can then define some functions inside this "Frog" class for different actions that it might perform
such as returning a value of a calculation that is done entirely within the scope of the class, or they could return nothing but do something else useful prior to their return(); line.
Example:
« Last Edit: March 28, 2007, 10:25:18 AM by Whirlingdervish(Q2C) »
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus

Offline [N]auTiCa

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
    • Extreme Online Gaming
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #48 on: March 28, 2007, 09:04:20 AM »
Alright done writing up my paper, now to add the rest of whatever code I have I will submit what I've come up with hopefully within an hour.
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
Clan ^K][L^  co-leader! www.clankil.com
dC member! www.monsterkill.org

Offline reaper

  • Opulent Member
  • *
  • Posts: 2872
  • Nice night for a walk, eh? - Nice night for a walk
    • View Profile
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #49 on: March 29, 2007, 05:38:57 AM »
cool, thanks for the excellent responses. i read both a few times, and took a look at wikipedia.  i'm trying to learn about classes now,and structures; from what i gather their very similar

if i want to create programs that alter packets - the ip/tcp headers and what not, what is the best platform to use? i just want to start messing around with stuff.  i think if i start writing programs from scatch that do these things, i'll be able to change more compliated ones easier

Quote from: console
Main would have to pass the address of its variables to any subroutine that wanted to use them.

excellent information in your post.  I guess you mean, any variable declared in main, can be passed to a function, regardless if the function was linked from a seperate .o file?  when you start talking about the memory address of variables, i get confused, and think you have to actually address the memory location with &.
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
VaeVictus "reaper is a lying sack of shit and ragequit then had, probably slugs, come alias and beat me, wasnt even the same person playing OBVIOUSLY, accuracies basicly doubled, and strategy

Offline Whirlingdervish

  • Super ShortBus Extravaganza
  • Illimitable Sesquipedalian Member
  • *
  • Posts: 6384
    • View Profile
    • The Dervish Depository
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #50 on: March 29, 2007, 09:19:37 AM »
yeah you're exactly right on that reaper.


technically your program sends a copy of the variable to the function or method if you don't use the & on the variable getting passed... so you can't change it's "real" value from within the class.
(you could return a value with the method and make a line in your main function that equates the old variable in your main to the returned value, but this would be completely outside of the classes scope)

If you want your method to be able to screw with variables in your main function just add the &'s and your method will be able to directly reference them, in their original memory locations..
(useful sometimes, but kinda dangerous if you screw up)
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus

Offline reaper

  • Opulent Member
  • *
  • Posts: 2872
  • Nice night for a walk, eh? - Nice night for a walk
    • View Profile
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #51 on: March 29, 2007, 11:08:00 AM »
heh, some of this is coming back now. that makes sense whirling.  you can pass the variable, but the value doesn't change to the rest of the program, unless you refer to the variable by memory location (&), or pass  a pointer to the function.

what is the best platform to do what i wanna do - i wanna work with packets, change the ip headers and things, send packets, receive packets, change my source ip, etc.  i'll worry about manipulating strings and stuff later, i'm assuming the best choice would be C or C++.  i have to go over all the programming concepts basically, but i'm familiar with most things except structures/classes (although i can do crazy things with beer bottles now).

so what is the best way to learn classes/structures, and what platform should i use?  i would like to work with simple programs that send packets, and alter them at the network & transport layer, maybe even write some from scratch, so i will be able to work with some more sophisticated programs i am interested in. all the cool networking programs like, nmap,netcat,snmpwalk,tcpreplay
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
VaeVictus "reaper is a lying sack of shit and ragequit then had, probably slugs, come alias and beat me, wasnt even the same person playing OBVIOUSLY, accuracies basicly doubled, and strategy

Offline Whirlingdervish

  • Super ShortBus Extravaganza
  • Illimitable Sesquipedalian Member
  • *
  • Posts: 6384
    • View Profile
    • The Dervish Depository
  • Rated:
Re: C/C++ programmers.. need some help.
« Reply #52 on: March 29, 2007, 11:14:12 AM »
you might wanna check out the pertinent sections of the q2 source code for some info on basic networking/packet sending routines..

It's readily available and I bet you'll have an easy time finding explanations of parts of it around here!
« Last Edit: March 29, 2007, 11:25:27 AM by Whirlingdervish(Q2C) »
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus

 

El Box de Shoutamente

Last 10 Shouts:

Costigan_Q2

November 11, 2024, 06:41:06 AM
"Stay cozy folks.

Everything is gonna be fine."

There'll be no excuses for having TDS after January 20th, there'll be no excuses AT ALL!!!
 

|iR|Focalor

November 06, 2024, 03:28:50 AM
 

RailWolf

November 05, 2024, 03:13:44 PM
Nice :)

Tom Servo

November 04, 2024, 05:05:24 PM
The Joe Rogan Experience episode 223 that dropped a couple hours ago with Musk, they're talking about Quake lol.

Costigan_Q2

November 04, 2024, 03:37:55 PM
Stay cozy folks.

Everything is gonna be fine.
 

|iR|Focalor

October 31, 2024, 08:56:37 PM

Costigan_Q2

October 17, 2024, 06:31:53 PM
Not activated your account yet?

Activate it now! join in the fun!

Tom Servo

October 11, 2024, 03:35:36 PM
HAHAHAHAHAHA
 

|iR|Focalor

October 10, 2024, 12:19:41 PM
I don't worship the devil. Jesus is Lord, friend. He died for your sins. He will forgive you if you just ask.
 

rikwad

October 09, 2024, 07:57:21 PM
Sorry, I couldn't resist my inner asshole.

Show 50 latest
Welcome, Guest. Please login or register.
November 22, 2024, 01:17:01 PM

Login with username, password and session length