Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / C++ debugging problems

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th May 2011 22:24
I hope this'll turn into a forum game.

Straight to the point, what's wrong with these two programs?

(1)

result of running that code: memory error

(2)

result: "Divison: 5/5= 5"

does anyone else have some good, confusing bugs?


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 25th May 2011 22:29 Edited at: 25th May 2011 22:31
It's because the temporary copy of the object has its destructor called the moment it is no longer of use (ie. after the assignment), which is freeing the memory. If you need to make a copy of an object that allocates memory (as you're doing) you should implement a proper copy constructor. See this link:

http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

An alternative is to declare the variable like this, which avoids the need to copy anything:





Support a charitable indie game project!
budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 25th May 2011 22:37
The second one you need a space between the / and * else it's a comment block.


Math is like a toilet. They both go in the other direction in Australia.
David R
21
Years of Service
User Offline
Joined: 9th Sep 2003
Location: 3.14
Posted: 25th May 2011 22:54 Edited at: 25th May 2011 22:57
This isn't a bug, but the following (annoying) question came up in my systems programming exam:



Hint:


09-f9-11-02-9d-74-e3-5b-d8-41-56-c5-63-56-88-c0
budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 26th May 2011 00:15
Wouldn't it set *kp to 0, then increase kp.


Math is like a toilet. They both go in the other direction in Australia.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th May 2011 00:30
it's the same as (*(kp++))=0 I think, sooo you get three bytes of data of *kp, and one byte of jibberish after that, and set all those bytes to 0


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 26th May 2011 00:51 Edited at: 26th May 2011 00:52
Quote: "three bytes of data of *kp, and one byte of jibberish"

I'm pretty sure that the compiler takes type size into consideration when increasing. if you had int* kp = 0x00000000 and did kp++, it wouldn't be 0x00000001, it would be 0x00000004.

So all of the data that you get would be garbage, not just some.


Math is like a toilet. They both go in the other direction in Australia.
Interplanetary Funk
15
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 26th May 2011 01:18
Has no one realised that in neuros second bug, he's actually trying to divide by the address of a variable, not the variable itself....?


Visit my blog for any and all info on what I do coding wise.
budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 26th May 2011 01:35
Quote: "Has no one realised that in neuros second bug, he's actually trying to divide by the address of a variable, not the variable itself....?"

No he isn't...


Math is like a toilet. They both go in the other direction in Australia.
Interplanetary Funk
15
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 26th May 2011 01:45
Okay, now I'm confused then :S whenever I have a function with a pointer as an argument, you never put the * in front when you want to use it :S

Oh well, back to the books then.


Visit my blog for any and all info on what I do coding wise.
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th May 2011 02:23 Edited at: 26th May 2011 02:41
if that were a + operator instead of a / operator that would be fine!

try putting the code into an IDE with syntax highlighting

[edit]
O_O
I just tried what I thought was correct for DarkCoder's post...

Apparently the ++ operator increases a value, but then returns the value before it was increased.
(so in: n=5; bool s=((n++)==5); t=(n==5) s is true, t is false)
that's tricky...

and you're right about pointers relying on the size of the datatype... apparently my pointer arithmetic is bad


Tell me if there's a broken link to images in a thread I post, and I'll fix 'em.
Interplanetary Funk
15
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 26th May 2011 02:48
n++ will return then increment the value. ++n will increment then return the value.

so in the following for loop, it would actually start counting from 1.



Visit my blog for any and all info on what I do coding wise.
budokaiman
FPSC Tool Maker
15
Years of Service
User Offline
Joined: 24th Jun 2009
Playing: Hard to get
Posted: 26th May 2011 03:12
Quote: "o in the following for loop, it would actually start counting from 1."

No, in for loops it will still always start from the initializing value.


Math is like a toilet. They both go in the other direction in Australia.
Interplanetary Funk
15
Years of Service
User Offline
Joined: 19th Apr 2010
Location: Ipswich, United Kingdom
Posted: 26th May 2011 03:27 Edited at: 26th May 2011 03:31
It didn't when I was using borland in college. mind you it was the ancient one.

It seems my understanding of C++ is being completely rebuilt tonight.

edit: revised using while loops:


output:


in j it starts at 1 because it increments it before outputting it.


Visit my blog for any and all info on what I do coding wise.
AutoBot
15
Years of Service
User Offline
Joined: 25th Sep 2009
Location: Everywhere
Posted: 26th May 2011 04:25 Edited at: 26th May 2011 04:53
Yeah, it's cool seeing how all the operators work.

The 2nd one that Neuro posted was pretty creative, didn't spot that comment mark. I would've seen it in an IDE, though.

EDIT: I actually did have a bug, but it involves code thats extremely big, lol. I was trying to port an OpenGL program to an object model, like this:



From the book I learned this technique from, it spoke of how this applied to games, but this could be a valid object model for any program.

But the bug was just a runtime error that I got at startup (access violation). My problem was that my singleton class (represents the program as a whole) was initializing the OpenGL subsystems before WinMain (I used a constructor), and that screwed things up. I changed it to a Start() member function and put it inside my entrypoint, and with tweaking, it worked.

Login to post a reply

Server time is: 2025-05-21 11:31:47
Your offset time is: 2025-05-21 11:31:47