malloc of a two-dimensional array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 23
Reputation: AbberLine is an unknown quantity at this point 
Solved Threads: 0
AbberLine's Avatar
AbberLine AbberLine is offline Offline
Newbie Poster

malloc of a two-dimensional array

 
1
  #1
Dec 7th, 2007
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".

  1. void teken_veld() {
  2. char* tab_veld[80][80];
  3. int y, x;
  4.  
  5. for(y=0;y<lengte;y++){
  6. for(x=0;x<breedte;x++){
  7. tab_veld[y][x]=malloc(lengte*sizeof(char));
  8. strcpy(tab_veld[y][x], " ");
  9. }
  10. }
  11. free(tab_veld[y][x]);
  12.  
  13. strcpy(tab_veld[0][posvisser], "X");
  14. strcpy(tab_veld[posvishaak][posvisser], "J");
  15.  
  16. ...
  17. }
I would realy appreciate if somewanne could help me out.

Greetings
Kevin
Last edited by AbberLine; Dec 7th, 2007 at 9:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: malloc of a two-dimensional array

 
0
  #2
Dec 7th, 2007
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 lengte 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';
Last edited by Ancient Dragon; Dec 7th, 2007 at 10:13 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 23
Reputation: AbberLine is an unknown quantity at this point 
Solved Threads: 0
AbberLine's Avatar
AbberLine AbberLine is offline Offline
Newbie Poster

Re: malloc of a two-dimensional array

 
0
  #3
Dec 8th, 2007
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] = {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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 23
Reputation: AbberLine is an unknown quantity at this point 
Solved Threads: 0
AbberLine's Avatar
AbberLine AbberLine is offline Offline
Newbie Poster

Re: malloc of a two-dimensional array

 
0
  #4
Dec 8th, 2007
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.

  1. for(y=breedte; y<80; ++y){
  2. free(tab_veld[y]);
  3. }
Last edited by AbberLine; Dec 8th, 2007 at 8:41 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: malloc of a two-dimensional array

 
0
  #5
Dec 8th, 2007
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.
  1. free(tab_veld[y]);
  2. 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.
Last edited by Ancient Dragon; Dec 8th, 2007 at 8:51 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 23
Reputation: AbberLine is an unknown quantity at this point 
Solved Threads: 0
AbberLine's Avatar
AbberLine AbberLine is offline Offline
Newbie Poster

Re: malloc of a two-dimensional array

 
0
  #6
Dec 8th, 2007
Oh so the rows that do not hold more then one cell are free cause they where not filled.
  1. 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!
QUOTE lang=dutch ~ De moeilijkheid zit hem in de eenvoud ~ /QUOTE

Translated:
~ Simplicity is the best solution for a difficult problem, the problem is to keep it simple. ~
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: malloc of a two-dimensional array

 
0
  #7
Dec 8th, 2007
>>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};
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 23
Reputation: AbberLine is an unknown quantity at this point 
Solved Threads: 0
AbberLine's Avatar
AbberLine AbberLine is offline Offline
Newbie Poster

Re: malloc of a two-dimensional array

 
0
  #8
Dec 8th, 2007
Ok. It would indeed be not soo nice if some random (big) value would fill up that space.
Thanks alot Ancient Dragon!
QUOTE lang=dutch ~ De moeilijkheid zit hem in de eenvoud ~ /QUOTE

Translated:
~ Simplicity is the best solution for a difficult problem, the problem is to keep it simple. ~
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC