Hi

Recently I started programming C (not C++) in borland 4.5 at school. Now my teammate and I have to make a small game. Therefor I will use an two-dimensional array, the field (a table, rows and colmnes). This two-dimensional array holds chars ("X", "|", "J", "-" and " ").
I would like to have an option where the player can change the lengte and width of the field. Thats where dynamic allocation comes in.

Here is the part of the function in my code that gives me troubles, its about malloc and it compiles but gives an error when it runs: "Stack Fault".

void teken_veld() {
 char* tab_veld[80][80];
 int y, x;

 for(y=0;y<lengte;y++){
   for(x=0;x<breedte;x++){
         tab_veld[y][x]=malloc(lengte*sizeof(char)); 
         strcpy(tab_veld[y][x], " ");
   }
 }
 free(tab_veld[y][x]);

 strcpy(tab_veld[0][posvisser], "X");
 strcpy(tab_veld[posvishaak][posvisser], "J");

...
}

I would realy appreciate if somewanne could help me out.

Greetings
Kevin

Comments
Thanks for using code tags correctly

If all you want them to hold is a single character then you don't need them to hold strings. You can simplify the table to this: char* tab_veld[80]; which is an array of 80 pointers (or 80 rows of unspecified number of columns). If you want each row to have 100 columns, then tab_veld[0] = malloc(100); Each column can hold one of the 5characters you previously specified, that is 'X', '|', 'J', '-', or the space ' '. Note that this are NOT strings, but single characters.

char*tab_veld[80 = {0};
// assuming [b]lengte[/b] is the number of columns in each row
 for(y=0;y< 80; ++y){
       tab_veld[y] = malloc(lengte); 
       memset(tab_veld[y], ' ', lengte); // set each column in this row to a space
 }
// I don't know what posvisser means so I can't tell you if the next two lines are right or wrong.  But what you want to do is just set a sincle cell to a single character.
 tab_veld[0][posvisser] = 'X';
 tab_veld[posvishaak][posvisser] = 'J';

Thank you very much, I'll try that as soon as I'm finished.
You forgot a bracket, not the end of the world but mayby this solution might be handy for ouhter to.

char*tab_veld[80[B]][/B] = {0};

Note that this are NOT strings, but single characters.

Yes indeed, I forgot to metion that.

// assuming lengte is the number of columns in each row

I don't know what posvisser means so I can't tell you if the next two lines are right or wrong. But what you want to do is just set a sincle cell to a single character.

Those last 2 lines are not to important it just to illustrate what I want to do with that array.
Sorry for those weird variable names, i'm from belgium and those are in dutch.

Okee, works like a charm! (if you include the declaration of int y) thanks a lot.

Alright just one small problem.
Do I still need to free() the last "rows" of the array? I tryed the following, but i get an general protection error.

for(y=breedte; y<80; ++y){
  free(tab_veld[y]);
}

Only free the rows that are malloc()'ed. But if you declare/initialize tab_veld as previously posted then malloc() will not complain about giving it a NULL pointer. So if you are getting general protection error then those pointers have either not been initialized to 0 or had already been free'ed. It's always good to reinitialize pointers to 0 after freeing them to avoid that problem.

free(tab_veld[y]);
tab_vels[y] = 0;

>>Sorry for those weird variable names, i'm from belgium and those are in dutch.
No need for apology, we don't all live in English-speaking countries.

Oh so the rows that do not hold more then one cell are free cause they where not filled.

char*tab_veld[80];

After filling the variable tab_veld from the code above there is nothing to free() because it hold no value. ... Nice :-D

Thanks!

>>Oh so the rows that do not hold more then one cell are free cause they where not filled.
No. they are free because they were never allocated.

>>because it hold no value.
Not quite -- they do hold a value but the code you just posted that value is some random and unpredictable value. It contains whatever was previously in that memory location, and that's why it is very important to initialize variables to a known value, usually 0

char*tab_veld[80] = {0};

Ok. It would indeed be not soo nice if some random (big) value would fill up that space.
Thanks alot Ancient Dragon!

This question has already been answered. Start a new discussion instead.