Jump to content

  • Log In with Google      Sign In   
  • Create Account


HelloSkitty

Member Since 27 Jun 2011
Offline Last Active Apr 09 2013 04:54 PM
-----

Topics I've Started

Printf acting weird

09 April 2013 - 01:38 PM

I am trying to debug a program in order to find the cause of a segmentation fault.  So I commented all of the code and slowly uncommented from the start to find the location of the segmentation fault.  This was successful and I have discovered the function that is causing it.  Then, I decided to add a printf to the start of that function, and oddly enough nothing printed.  I then added printf to the calling function, and nothing printed there either.  So in the same manner of finding the location of the segmentation fault, I set about finding the place my program stopped printing.  And I did.  It was the line:

 

retVal->maxSize=10;

 

And I have no idea why.  All of the relevant code is here:

 

circular.h

typedef struct Quote {
	unsigned int time;
	double rate;
} quote;

typedef struct CBuf {
	unsigned int maxSize;
	unsigned int size;
	unsigned int first;
	unsigned int last;
	quote* arrayPointer;
} cbuf;

cbuf* cbuf_init();

void cbuf_delete(cbuf* cb_ptr);

void cbuf_update(cbuf* cb_ptr,unsigned int time,double rate);

double cbuf_average(cbuf* cb_ptr);

quote* cbuf_start(cbuf* cb_ptr);

quote* cbuf_end(cbuf* cb_ptr);

void cbuf_dump(cbuf* cb_ptr);

void cbuf_stats(cbuf* cb_ptr);

 

circular.c

#include "circular.h"
#include <stdlib.h>
#include <stdio.h>

cbuf* cbuf_init() {
	printf("cbufinit\n");
	cbuf* retVal;
	printf("makingretval\n");
	printf("%d\n",(*retVal).maxSize);
	retVal->maxSize=10;
	printf("9\n");
	retVal->size=0;
	retVal->first=0;
	retVal->last=0;
	quote* quotes=malloc(10*sizeof(quote));
	retVal->arrayPointer=quotes;
	return retVal;
}

// More code below, but this is the only function that is called
// makingretval and -72537468 are printed
// 9 is not printed

 

main6a.c

#include <stdio.h>
#include <stdlib.h>
#include "circular.h"

int main() { 
	printf("start\n");
	printf("1\n");
   cbuf *cb1 ;
	printf("2\n");
   cb1 = cbuf_init() ;
	printf("3\n");
   return 0 ;
}

// 1 and 2 are printed
// 3 is not printed

 

 

Everything compiles fine, it is simply that printf does not seem to work at all following the retVal->maxSize=10; line.  In a separate run, I added a getchar call after the printf("3\n") which ran fine.  So the program is still getting to the end with no issue.  I just want to know why it is not printing.

 

Thanks!


AES Encrypting and Decrypting

24 September 2012 - 09:32 PM

I am trying to use the javax.crypto java library to encrypt and decrypt strings of text using the Advanced Encryption Standard. Unfortunately, I seem to be doing something very wrong, as I get the error "Given Final Block Not Properly Padded". And I do not know what this means.

A code snippet is here:

public String encode(String s) {
  String encodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(encodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.ENCRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE ENCODING");}
  return result;
}

public String decode(String s) {
  String decodingKey="sixteencharacter";
  String result="DEFAULT STRING TO SEND";
  try {SecretKeySpec key = new SecretKeySpec(decodingKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
  cipher.init(Cipher.DECRYPT_MODE, key);
  result = new String(cipher.doFinal(s.getBytes()));
  } catch (Exception e) {System.out.println("ERROR WHILE DECODING");e.printStackTrace();}
  return result;
}

And when I try to call a line like, say,

String s=decode(encode("WELCOME:BOB"));

I get the error.

Other Strings I have tried that do not work are "WELCOME:ALICE", "WELCOME:NEMO". However, I have also found strings that do decode properly, such as "WELCOME:HI", "WELCOME:DAVE", "WELCOME:SQUARE", "WELCOME:NOBODY".

I do not know why some strings work and some don't. I modeled the decoding method to mirror the encoding, so I am at a loss to explain any of these results.

Any help is appreciated!

Checking if a Connection Socket Is Dead

05 August 2012 - 03:30 PM

I'm currently have a Server program running with multiple sockets, which I'm testing with netcat. So I run the server, which makes a bunch of client handlers -- one for each client, and associates a socket with each. I have an infinite while loop running through and updating these handlers, and in the update code, I have this:

if (!socket.isConnected()) {
System.out.println("CONNECTION DROPPED");
server.handlers.remove(this);
}
if (socket.isInputShutdown()) {
System.out.println("INPUT SHUTDOWN");
}
if (socket.isOutputShutdown()) {
System.out.println("OUTPUT SHUTDOWN");
}
if (!socket.isBound()) {
System.out.println("NOT BOUND");
}  //4 methods I found in the java doc for the Socket clas

So I have 4 if statements that presumably check if a socket is shut down. Unfortunately, when I kill one of my netcat test client instances, none of these if statements return a positive. Am I using these functions wrong, or is there some other function that achieves the purpose I seek?

I know that it must be possible to detect a dropped connection, as when I kill the server, all of my netcat clients know this and cease their function. I just can't seem to detect when a Client dies.

For those who aren't familiar with netcat, all it is is a simple program that can send messages across ports (at least, I think that's all it is).

Select Pixel Transparency

13 May 2012 - 11:26 AM

So I looked for ways to make individual pixels transparent rather than an entire window, and found this:

jPanel1 = new javax.swing.JPanel() {
	  protected void paintComponent(Graphics g) {
		  if (g instanceof Graphics2D) {
			  final int R = 240;
			  final int G = 240;
			  final int B = 240;

			 Paint p =
			  new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
				  getWidth(), getHeight(), new Color(R, G, B, 255), true);
			  Graphics2D g2d = (Graphics2D)g;
			  g2d.setPaint(p);
			  g2d.fillRect(0, 0, getWidth(), getHeight());
	   } else {
	  super.paintComponent(g);
	   }
	 }
}

from here: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/#Enabling-Per-Pixel-Translucency


Now the problem I have is that this is drawing the transparency directly to the JFrame. This is a problem when using this with a game, because there are serious flickering issues.

I tried double-buffering the transparency in the image I loop-draw, but it was no avail.

public void interpolatedDensity(int i,int j) {
//stuff
return 150<<24 | rCol<<16 | gCol<<8 | bCol;
//Partial transparency
}
public void paint() {
  for (int i=0;i<800;i++) {
   for (int j=0;j<800;j++) {
    int c=field.interpolatedDensity(i,j);
    dbPlasma.setElem(i+j*800,c); //The DataBuffer for BetterDoubleBuffer
   }
  }
 //Stuff
 g.drawImage(betterDoubleBuffer,0,0,null);
}
//Transparency ignored when painting

Is there a way around this, or is there no way for a java-based (action, at least 10 FPS) game to make use of window transparency?

Kill The NPC

12 May 2012 - 12:10 PM

For some reason.... you rarely see the ability to kill an NPC in a game (I've yet to experience even one).

Now, NPC is a rather vague term that seems to include teammates in team-based battle games (like Halo coop mode)

For the sake of this, I am limiting NPC to only the characters in an rpg game that give quests or exchange helpful dialogue. And a bunch of games have ways to deal with players trying to attack an NPC.

Dragon Ball Z GBA 2 - The A for melee attack is also the "talk to them" button. Energy blasts pass right through NPCs.
Runescape - There is simply no option.
Pokemon: You can only call out pokemon in battle mode where there are no NPCs to be found.
Sonic DX: The NPCs are just walls that talk. Spins go around them, homing attack doesn't target them.

Now part of this thought came from a video about "If Pokemon Were Real" where there is a weak electric pokemon fighting a strong cocoon, and the protagonist realizes, why don't I attack the actual enemy instead of his pokemon?"

Is there a reason many games are so adamant about not being able to kill the NPCs (excluding killing them as part of the story as I hear how it is on God of War. I mean to deviate from the obvious plot by killing the ones who are supposed to give you the quests) Are most devs too lazy to accommodate for story impacts, is a plotline of that degree of nonlinearity just not realistic, or what?

The closest that any game has come to killing NPCs as far as I know is Pokemon Mystery Dungeon Explorers of Time/Darkness where you can try to kill a shopkeeper, but the shopkeeper turns out to be almost impossible to kill (if you can kill him, you may take all items from the shop). But outside of dungeons, you can't attack anyone. Are there any better examples, or is killing NPCs just taboo?

PARTNERS