Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 24 25 [26] 27 28 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100727 times)

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #375 on: January 17, 2012, 10:49:33 pm »

That was it! Thanks, that was confusing the microline out of me.
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #376 on: January 20, 2012, 12:21:31 am »

c++

Does anyone know any good resources to help with passing pointers around from different structures and classes involving multiple different header-files? I'm getting some data corruption issues(or at least I was until I decided "Frak it, I'll just put the tile map display offsets in with the globals!")

I lost the code(I stupidly backspaced instead of just commenting it out), so I cant show it to you right now.
Logged
Signature goes here.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #377 on: January 20, 2012, 03:56:42 am »

c++

Does anyone know any good resources to help with passing pointers around from different structures and classes involving multiple different header-files? I'm getting some data corruption issues(or at least I was until I decided "Frak it, I'll just put the tile map display offsets in with the globals!")

I lost the code(I stupidly backspaced instead of just commenting it out), so I cant show it to you right now.
It's mostly just an issue of 'you're doing it wrong.' Passing pointers around won't cause problems on its own, so long as you have the right stuff included. With issues involving data corruption, it's due to either forgetting how variable scope works or forgetting how pointers work, and usually without realizing you overlooked that particular aspect of how things work.
Scope issues commonly result from creating an object in a function or other class without using manual memory allocation. For example:
Code: [Select]
int main()
{
int* a = foo();
}

int* foo()
{
int aNum = 5;
int* value = &aNum;
return value;
}
The above code will result in a pointing to memory which is de-allocated when the function foo() ends, due to function scope. foo() ends, aNum's memory is deallocated, and the returned pointer points to deallocated memory. To fix this, either return a copy (usually sub-optimal) or use dynamic memory allocation. Just be sure to use a delete for every new you use, or you end up with memory leaks.

Issues coming from pointers themselves usually involves something you happened to overlook while planning your code. Things like:
Code: [Select]
int main()
{
int* a = new int[5];
for(int i=0; i<5; i++)
a[i] = 1;
ChangeInt(a);
}

void ChangeInt(int* a)
{
delete[] a;
a = new int[5];
for(int i=0; i<5; i++)
a[i] = 2;
}
The end result of the above code will be all the values within array a in main deleted, followed by the creation of a new set of values which won't be pointed at by the array a in main, thus creating a memory leak on top of not setting the values. As a pointer is essentially an unsigned integer of some sort, it can help to think of the pointer itself being data; albeit data which is being used for a special purpose. In this case, changing it to the following would work (assuming delete[] is called on a in main at some point before it exits):
Code: [Select]
int main()
{
int* a = new int[5];
for(int i=0; i<5; i++)
a[i] = 1;
ChangeInt(&a);
}

void ChangeInt(int** a)
{
delete[] *a;
a* = new int[5];
for(int i=0; i<5; i++)
(*a)[i] = 2;
}

Those 2 examples are some of the more common problems involving so-called data corruption with pointers. If you aren't using arrays in such a way, your problem is almost certain scope related, in which case you need to go through and figure out when and why your data is being deallocated; and it's usually because of treating scoped data like it will always exist. Pointers are nice because they let you send a small handful of bytes telling the program where data is stored; but they can be not-so-nice when you accidentally de-allocated the memory the data is in, either with delete or from leaving scope.

When dealing with classes/structs (in C++ they are exactly the same thing; struct is just public by default whereas class is private by default), pointers behave the same way, with the addition being class data members having the scope of that instance of the class.
Code: [Select]
struct a
{
a(){aNum=5};
int aNum;
};

int main()
{
a* instance = new a();
int* ptr = instance->aNum;
delete instance;
//ptr is now pointing to de-allocated memory
}
Also keep in mind that de-allocated memory may not change immediately. When it is de-allocated, the value will stay the same for a while, then at some random point in the future will change.
« Last Edit: January 20, 2012, 04:19:07 am by alway »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #378 on: January 20, 2012, 05:16:05 am »

c++

Does anyone know any good resources to help with passing pointers around from different structures and classes involving multiple different header-files? I'm getting some data corruption issues(or at least I was until I decided "Frak it, I'll just put the tile map display offsets in with the globals!")
I'm guessing that somewhere you supply a pointer where you expect an int (or vice-versa), and since pointers behave as integers, it won't throw an error but continue with faulty data. Another common thing with "data corruption" (By which I think you mean weird values) is forgetting to initialise a value( int a; usually means that a now has a semi-random value), or a reference to a deallocated memory location (as mentioned in alway's very good comment).
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Rgamer

  • Bay Watcher
  • [RELIGION: JEWISH]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #379 on: January 20, 2012, 10:16:05 pm »

This is probably a REALLY stupid question, so..... how do I make this:

Spoiler (click to show/hide)

stay open after cout << chChar << "has ASCII code << (int)chChar << endl; ? I'd like to see what the ASCII code for various characters is.(for example, &,>,<,@,etc.), and it's a bit hard to see it in 10 milliseconds.
Logged
Rgamer likes dwarves for their industriousness. He dislikes small dogs for their annoying barking, enjoys a good run, and prefers to consume steak, potatoes, or apples whenever possible. He likes native silver, granite, marble, and ebony.

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: Programming Help Thread (For Dummies)
« Reply #380 on: January 20, 2012, 10:19:21 pm »

This is probably a REALLY stupid question, so..... how do I make this:

Spoiler (click to show/hide)

stay open after cout << chChar << "has ASCII code << (int)chChar << endl; ? I'd like to see what the ASCII code for various characters is.(for example, &,>,<,@,etc.), and it's a bit hard to see it in 10 milliseconds.

I'd just add an extra cin prompt at the end so that the program doesn't end until you give it a second round of input.
Logged

Rgamer

  • Bay Watcher
  • [RELIGION: JEWISH]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #381 on: January 20, 2012, 10:36:36 pm »

This is probably a REALLY stupid question, so..... how do I make this:

Spoiler (click to show/hide)

stay open after cout << chChar << "has ASCII code << (int)chChar << endl; ? I'd like to see what the ASCII code for various characters is.(for example, &,>,<,@,etc.), and it's a bit hard to see it in 10 milliseconds.

I'd just add an extra cin prompt at the end so that the program doesn't end until you give it a second round of input.

So, like this?

    int nLeave();
    cout << "Press any button to exit";
    cin >> nLeave;
    cout << "Bye!" << endl;
    return 0;

(Note: It's telling me that there's an ambiguous overload on "cin >> nLeave;
Logged
Rgamer likes dwarves for their industriousness. He dislikes small dogs for their annoying barking, enjoys a good run, and prefers to consume steak, potatoes, or apples whenever possible. He likes native silver, granite, marble, and ebony.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #382 on: January 20, 2012, 10:38:09 pm »

If it's C++, couldn't you just tack on a getchar(); at the end?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Rgamer

  • Bay Watcher
  • [RELIGION: JEWISH]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #383 on: January 20, 2012, 10:44:50 pm »

Problem solved  :). Also, what's a getchar? I pretty much know how to use int, char, bool, if, and else. I know a few other things, but that's it.
Logged
Rgamer likes dwarves for their industriousness. He dislikes small dogs for their annoying barking, enjoys a good run, and prefers to consume steak, potatoes, or apples whenever possible. He likes native silver, granite, marble, and ebony.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Rgamer

  • Bay Watcher
  • [RELIGION: JEWISH]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #385 on: January 20, 2012, 10:50:54 pm »

http://www.cplusplus.com/reference/clibrary/cstdio/getchar/
I get a feeling I'm going to be making use of that link an awful lot... Thanks!
Logged
Rgamer likes dwarves for their industriousness. He dislikes small dogs for their annoying barking, enjoys a good run, and prefers to consume steak, potatoes, or apples whenever possible. He likes native silver, granite, marble, and ebony.

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #386 on: January 21, 2012, 01:02:15 am »

It's a nice website. And thanks for the help on pointers. I'll report back if it works.
« Last Edit: January 21, 2012, 01:04:35 am by thobal »
Logged
Signature goes here.

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #387 on: January 21, 2012, 10:32:06 am »

This is probably a REALLY stupid question, so..... how do I make this:

Spoiler (click to show/hide)

stay open after cout << chChar << "has ASCII code << (int)chChar << endl; ? I'd like to see what the ASCII code for various characters is.(for example, &,>,<,@,etc.), and it's a bit hard to see it in 10 milliseconds.

I'd just add an extra cin prompt at the end so that the program doesn't end until you give it a second round of input.

So, like this?

    int nLeave();
    cout << "Press any button to exit";
    cin >> nLeave;
    cout << "Bye!" << endl;
    return 0;

(Note: It's telling me that there's an ambiguous overload on "cin >> nLeave;

I know you solved it, but I feel the need to point out that
Code: [Select]
int nLeave();
is a method stub for a method that returns an integer, not an integer variable.
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #388 on: January 21, 2012, 08:14:33 pm »

Actually, int nLeave(); is a declaration of an integer using the default constructor. For example, Object myObject(myInt); declares an object myObject and initializes it with a constructor from Object that takes an integer.
« Last Edit: January 21, 2012, 08:16:40 pm by Normandy »
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #389 on: January 21, 2012, 08:31:55 pm »

My bad. I admit, C++ isn't my strong suit. I assumed my knowledge of Java would apply.
Logged
Pages: 1 ... 24 25 [26] 27 28 ... 91