C Programming Question Thread

The reason it is causing the seg fault when I get alt = (*m).hardness[tempy-1][tempx] tempy = -1638448821 and tempx = -1218392681.

But I dont know how thats possible because all the tempx and tempy values are created here

 int xPre=0;
    int yPre=0;

    for(xPre=0;xPre<80;xPre++){
        for(yPre=0;yPre<21;yPre++){
            distanceCell pass;
            pass.distance=1000;
            pass.yloc=yPre;/* 1000 will represent infinity */
            pass.xloc=xPre; 
            (*m).distanceGrid[yPre][xPre]=pass; 
        }
    }

The mem location of the bad distanceCell is 0x6d0208

its also random when it occurs after watching the debugger, one time my heap makes it to size 210 and others to 273.

when your loops are done it completes a total of 1680 interations. Did you mean to do that?

yep, I have a matrix that has a height of 21 and width of 80.

I think he might be right, I dont understand it but I got rid of the error by not going over the border cells.

Late to the party, and its the end of the weekend for me.

This is a list of things I do to address similar problems when coding:

  1. I write the C code in Java. Then compare and port back if necessary. Because Java is more verbose, likes to be written cleanly, and doesn't let you make same mistakes. I also get forward faster. C# should also work.

  2. I write a function that will print out all the relevant binary state, and then I call that printing function at each point the state changes to visually assert the state (white box testing). Alternatively, I use a debugger with break points to inspect the relevant binary state of the program, but then it is more difficult to compare over time.

  3. I write a small test case. A really small grid matrix (4x4, or something) would be easiest to test with.

thanks for the advice, Its almost complete now but I will take that into consideration for the future.

I have another question though, how can I create two instances of a library in C? For example how would I create two separate random number generators?

Not really sure what you mean by two instances of a library?

If you have an implementation with global and/or static variables, you would need to refactor your code to use pointers to malloced structs instead of global data. This would be similar to making it OO, such that every function that operates on a random number generator takes a pointer to the struct as a (possibly first) parameter, and then operate on that struct.

EDIT: if it is a library (so/dll) that has a global implementation, then this will be difficult.

Sure the result doesn't break the boundries of an integer?
If the numbers are in the millions and are multiplied, it can get ugly dependant if your using signed or unsigned ints.
try googling the max of the signed and unsigned values, forgot em right off the bat, but they can cause trouble, and you may have to round down just a tad.

   #include "monster.h"
#include "mapInit.h"
/*Public Class Monster */
struct Monster{
    int intelligence;
    int telepathy;
    int tunneling;
    int erratic;
    int xloc;
    int yloc;
    Map *m;
    void (* moveUp)();
}
/*Constructor*/
Monster MonsterInit(Map *map,){
    Monster *monsters;
    monster = malloc(sizeof(Monster));
    monsters->m=map
    monsters->moveUp=$moveUp;

}

/*Public Functions*/
void moveUp(){

}

I have started on the next part of this program so I want to try and go OO in C just for fun but is there a way of making the moveUP function have access to the struct that holds the pointer without have to make the struct an explicit paramater?

Can you elaborate on that?

For 4-bytes signed int, the range goes from -2^31 to (2^31)-1 approximately from -2bln to +2bln. A 4 byte long unsigned int goes up to (2^32)-1 about 4bln.

The post above is what I mean.

You just declared a struct, you can't access it, it's not in memory until you define a variable of that type. Also I'm pretty sure that code won't compile.

this c file will be turned into a .o file and then a .a file and then its header will be referenced meaning I will access the function MonsterInit that will return a pointer to a struct with pointers to functions.

In other words I am wondering if in the function I am using if I can get the struct of where the pointer is like get &itself and then somehow use that address to get the address of where it points which would be the struct that contains the function it is called from.

so.....

void foo(){

/*How do I get address of function foo from inside a function right here and then deference that reference to get the struct?  */

}

You can't. You must pass a pointer to a struct instance to the function. Try making a typedef of struct if you need brevity.

You can use the function identifier as a pointer to a function, but you can not get pointer to the struct which contains a reference to the function from the said pointer to the function.

I changed your code to explain:

If you want OO in C, you may want to add a level of indirection to the actual class methods/functions. The way you are doing it is inheritance by prototyping, like JavaScript. If you want inheritance like Java/C#/C++ virtual/Objective C etc., you would separate the instance methods like above. You only need one set of functions per class.

That said, if you have a deadline, or need to be done with a project with other goals than learning this specific problematics, you may not wish to spend your time OO-ing C.

Thanks, I knew I could do it that way but I had a crazy plan to make it more OO by hashing the pointers from the functions and then accessing an array that the index corresponds to the Monster via hash in a .a file. I don't think I cam do it though.