Oddworld Forums > Zulag Two > Off-Topic Discussion


 
Thread Tools
 
  #1  
01-17-2014, 11:13 PM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)
Code!

Ok so this thread is predominantly because I have a problem that may-or-may-not be more complicated than a blog, but it's really open to any discussion about computer science-y things.

I've been working in C for a little while, and I'm currently playing around with SDL (media library, primarily target at game development). I'm kind of teaching myself how to do a few things as I go, using the Quake 3 source code and Stachexchange as a general points of reference. I've just written a memory manager of sorts, and spent the last 3 days debugging it. I discovered that I'm totally fucking stupid, but I almost have it working.

I think I'm still having a few issues with it but I'm not really sure why.

(Sorry in advance for the images, I need to find a way to turn off C++ syntax highlighting for my pure-C)

Basically, I have a struct "new" with a field uint32_t pixels. New represents an image to be drawn to the screen, and pixels is its raw image data. At the moment I am trying to fill pixels with a colour uint32_t col, in this case 0xFF00FFFF.



Width and height are both equal to 40, so the loop runs for i < 1600. Whenever I try to run this I get a segmentation fault when i = 3. All of the memory in this example is preallocated into a "block" when the program starts, and I check to make sure that the section allocated for pixels isn't NULL. At this point it's probably worth mentioning that the block is allocated as a void pointer, then cast to a uint32_t*, which might have something to do with the problem somewhere up the line.

I tested to make sure that nothing had gone wrong in the block, and that there was enough space actually there for 1600 uint32_t's.



If it can be iterated through and read every value in pixels then I should be able to write to them using that same method, right?



This freaked me out at first, but from the look of the dissasembly it appears that the second printf() gets shuffled down to after the allocation. Is it possible that could be significant?

Basically I'm thinking this has to be a memory alignment error from casting out of a void pointer. Am I correct? Given that pixels comes from the start of a block I don't really understand how this happened, but is it possible that, because the data was allocated to a void pointer it started life out of alignment for a uint_32?

Can anyone give me a blanket-idea of what could have happened here? Alternatively, you could all ignore me. I could probably post this somewhere better but I'm too lazy to make a new account anywhere.

*Implicit casting is still casting.
Reply With Quote
  #2  
01-17-2014, 11:48 PM
Varrok's Avatar
Varrok
Wolvark Grenadier
 
: Jun 2009
: Beartopia
: 7,301
Blog Entries: 52
Rep Power: 25
Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)

:
This freaked me out at first, but from the look of the dissasembly it appears that the second printf() gets shuffled down to after the allocation. Is it possible that could be significant?
I don't think so.

What type of variable is "pixels"? (Was the struct defined by you or came with SDL or sth?). Can it FIT such numbers as "0xFF00FFFF" ( 4278255615 in decimal, much more than this 70m you got in the output) there? Also, shouldn't the second printf() be put after allocation anyway? what's the point of showing the unchanged value?
Reply With Quote
  #3  
01-17-2014, 11:57 PM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)

The struct is defined by me. Pixels is an unsigned 32bit integer, big enough to hold up to 0xFFFFFFFF.

I just tacked the assignment back on without really thinking. The second printf() should have been down below that. The arrow in the dissasembly is stopped where the program segfaulted, at the assignment. You can clearly see that it's sandwiched between 2 calls to printf.

I'm pretty sure this is a byte alignment issue, but that's not something I've ever had to deal with before.

e: And yeah I realsied the output number of ~70,000,000 was way too small. That's why I'm thinking it might be alignment?

Last edited by Phylum; 01-18-2014 at 12:01 AM..
Reply With Quote
  #4  
01-18-2014, 12:14 AM
Varrok's Avatar
Varrok
Wolvark Grenadier
 
: Jun 2009
: Beartopia
: 7,301
Blog Entries: 52
Rep Power: 25
Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)

Oh, uint32 is short from "unsigned 32bit integer". I'm a dummy, I haven't noticed.

So, the output window is from the exact code there? (With assignment not sandwiched) So... the shown value of pixels[i] should always be the same, right? But it breaks at the third attempt, so the second allocation must have overriten the bytes for pixels[3] amirite?

I'm not an expert of neither C nor Assembly, but isn't the "arrow line" missing something:
MOV *here*(&eax),&eax ? Unless it really wants to allocate the same value to itself (kind of?) , which is kind of pointless
Reply With Quote
  #5  
01-18-2014, 12:19 AM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)

Yeah I don't know what the brackets mean. I just kind of look at the opcodes and fake the rest.

It was an alignment issue. I was stepping through my void pointers stupidly. Now I'm casting them to an unsigned, 8-bit integer pointer and adding i * the size of the item I'm allocating space for in bytes. I forgot the multiplication bit before, so it was possibly ending up halfway through a 32 bit integer as far as the CPU is concerned. Maybe.
Reply With Quote
  #6  
01-18-2014, 04:07 AM
Nate's Avatar
Nate
Oddworld Administrator
Rainbow of Flavour
 
: Apr 2002
: Seattle (woo!)
: 16,311
Blog Entries: 176
Rep Power: 42
Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)

:
It was an alignment issue. I was stepping through my void pointers stupidly. Now I'm casting them to an unsigned, 8-bit integer pointer and adding i * the size of the item I'm allocating space for in bytes. I forgot the multiplication bit before, so it was possibly ending up halfway through a 32 bit integer as far as the CPU is concerned. Maybe.
Is it solved now?
__________________
:
Spending as long as I do here, it's easy to forget that Oddworld has actual fans.

Reply With Quote
  #7  
01-18-2014, 04:21 AM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)

Yes. For some reason posting threads/blogs about coding issues is some kind of magical way to make me come up with a solution all by my self.
Reply With Quote
  #8  
01-18-2014, 04:57 AM
Varrok's Avatar
Varrok
Wolvark Grenadier
 
: Jun 2009
: Beartopia
: 7,301
Blog Entries: 52
Rep Power: 25
Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)

Does that work with social issues too?
Reply With Quote
  #9  
01-18-2014, 05:56 AM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)

No
Reply With Quote
  #10  
01-18-2014, 06:11 AM
Steamer_KING's Avatar
Steamer_KING
Outlaw Cutter
 
: Feb 2011
: Portugal
: 1,204
Rep Power: 15
Steamer_KING  (637)Steamer_KING  (637)Steamer_KING  (637)Steamer_KING  (637)Steamer_KING  (637)Steamer_KING  (637)

Well, good luck on that then. Show us the result later. And if you encounter bugs, just return to this, maybe we can help. Or not, but you know, better safe than sorry.
__________________

Reply With Quote
  #11  
01-18-2014, 03:25 PM
Nate's Avatar
Nate
Oddworld Administrator
Rainbow of Flavour
 
: Apr 2002
: Seattle (woo!)
: 16,311
Blog Entries: 176
Rep Power: 42
Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)

:
Yes. For some reason posting threads/blogs about coding issues is some kind of magical way to make me come up with a solution all by my self.
S'not just posting to threads that does it. Quite often I find that I come up with the answer to a coding problem just as I'm describing it to someone. Then I have to not and smile when they explain to me the solution I just thought of.
__________________
:
Spending as long as I do here, it's easy to forget that Oddworld has actual fans.

Reply With Quote
  #12  
01-18-2014, 04:18 PM
Havoc's Avatar
Havoc
Cheesecake Apocalypse
 
: May 2003
: Netherlands
: 9,976
Blog Entries: 71
Rep Power: 30
Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)Havoc  (4126)

Have you tried turning it off and on again?
__________________
The Oddworld Wiki

When one person suffers from a delusion it is called insanity. When many people suffer from a delusion it is called religion.

Reply With Quote
  #13  
01-19-2014, 01:06 AM
Varrok's Avatar
Varrok
Wolvark Grenadier
 
: Jun 2009
: Beartopia
: 7,301
Blog Entries: 52
Rep Power: 25
Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)

Does that work with tigers?
Reply With Quote
  #14  
01-19-2014, 01:58 PM
MA's Avatar
MA
DOES NOT COMPUTE
 
: Nov 2007
: shit creek
: 5,106
Blog Entries: 10
Rep Power: 27
MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)MA  (9593)

what are the cheats? hold L1 and R1 i've got that already, then?
Reply With Quote
  #15  
01-19-2014, 03:52 PM
Nate's Avatar
Nate
Oddworld Administrator
Rainbow of Flavour
 
: Apr 2002
: Seattle (woo!)
: 16,311
Blog Entries: 176
Rep Power: 42
Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)Nate  (13497)

To hopefully sidetrack this thread back on to a topic, I thought I'd mention a few of the programming challenges I've been given in job interviews lately.
  • Write a function that receives two numbers, then returns an array containing all the primes in between them. Do it efficiently.
  • Given an unsorted, even-numbered array of integers, divide the array into two lists of equal size such that their sum is as close as possible.
  • Given a solved Sudoku puzzle, validate that it has been solved correctly.
  • Come up with classes (including lists of attributes and operations) for a computer version of Connect Four. Then write the function that checks whether the game is in a winning state.
  • Write a function that receives two sets (i.e. unordered lists that contain no duplicates). Return all the values in Set 1 that aren't in Set 2.
  • Say you have a base-10 number stored as a string. That's a waste of space, as the character values can take 255 values (not including 0/null). Write a function that would convert this string to one with base-255 values stored in each character.
__________________
:
Spending as long as I do here, it's easy to forget that Oddworld has actual fans.

Reply With Quote
  #16  
01-19-2014, 03:58 PM
Paul's Avatar
Paul
Outlaw Sniper
 
: Jun 2007
: MilkyWay
: 1,535
Rep Power: 19
Paul  (718)Paul  (718)Paul  (718)Paul  (718)Paul  (718)Paul  (718)Paul  (718)

:
Yes. For some reason posting threads/blogs about coding issues is some kind of magical way to make me come up with a solution all by my self.
I think you've just discovered "rubber ducking".

http://en.wikipedia.org/wiki/Rubber_duck_debugging
__________________
[ http://www.paulsapps.com ]

Crawling sligs will shout "Mommy!" while running around and then the slig mommy will appear and help them put their pants on.

Reply With Quote
  #17  
01-20-2014, 06:46 AM
OANST's Avatar
OANST
Necrum Burial Grounds Moderator
Our worst member ever
 
: Jun 2003
: Them dark fucking woods
: 12,320
Blog Entries: 134
Rep Power: 40
OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)OANST  (16390)

Have you tried invoking the spirit of Varrok? Much meditation and prayer are required to become all of code, and not some, but all. Just so.

That's the only advice I have, and I only posted here because I'm bored right now.
__________________


My bowels hurt.

Reply With Quote
  #18  
01-20-2014, 07:53 AM
Varrok's Avatar
Varrok
Wolvark Grenadier
 
: Jun 2009
: Beartopia
: 7,301
Blog Entries: 52
Rep Power: 25
Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)Varrok  (7896)

:
I think you've just discovered "rubber ducking".
Oh god I read that totally wrong
Reply With Quote
  #19  
01-20-2014, 02:50 PM
Phylum's Avatar
Phylum
No Artificial Colours
 
: Sep 2008
: Rock bottom
: 4,911
Blog Entries: 94
Rep Power: 23
Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)Phylum  (5748)

:
  • Say you have a base-10 number stored as a string. That's a waste of space, as the character values can take 255 values (not including 0/null). Write a function that would convert this string to one with base-255 values stored in each character.
Fuck I had to read that about 10 times before I realised what it was actually asking.

Might have a crack at some of these later if I'm bored enough. Exercises like this are the kind of thing I've missed out on a lot being self-taught. Thanks
Reply With Quote


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 








 
 
- Oddworld Forums - -