| | |
malloc of a two-dimensional array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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".
I would realy appreciate if somewanne could help me out.
Greetings
Kevin
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".
C Syntax (Toggle Plain Text)
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"); ... }
Greetings
Kevin
Last edited by AbberLine; Dec 7th, 2007 at 9:48 pm.
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.
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.
Yes indeed, I forgot to metion that.
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.
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.
•
•
•
•
// 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.
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.
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.
C Syntax (Toggle Plain Text)
for(y=breedte; y<80; ++y){ free(tab_veld[y]); }
Last edited by AbberLine; Dec 8th, 2007 at 8:41 am.
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.
>>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.
C Syntax (Toggle Plain Text)
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.
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.
Oh so the rows that do not hold more then one cell are free cause they where not filled.
After filling the variable tab_veld from the code above there is nothing to free() because it hold no value. ... Nice :-D
Thanks!
C Syntax (Toggle Plain Text)
char*tab_veld[80];
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. ~
Translated:
~ Simplicity is the best solution for a difficult problem, the problem is to keep it simple. ~
>>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
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.
![]() |
Similar Threads
- Passing 2D Array of Pointers into a function (C++)
- Use of double pointer in functions (C)
- Pointers (archived tutorial) (C++)
- Dynamic Mmeory Allocation with 2D Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- dynamic allocation of 2d array (C)
- strtok() function questions <C programming> (C)
- Pointers (C++)
- Pointers (Part II) (C)
- A little Help (C++)
Other Threads in the C Forum
- Previous Thread: some recreational work with strings..anyone got any ideas?
- Next Thread: Help with header files, declaring extern vars
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






