i am trying to make as part of a C program, a function that will create a dynamic 2d array (as in a 2d array with no initial set limits on row/column sizes, they will be input when the program is run). This array does not need to be square, but its limits will be 100x100 of chars.
I made some code, and it works fine, except when the column size (col) is bigger than the rowsize (row). Then it gives me an error : segmentation fault (core dumped). Here it the code -
nah, new and delete come up as errors on gcc (which is what I have to use).
A = (char **)malloc(row * sizeof(char *));
for(i=0; i < col ; i++)
*(A+i) = (char *)malloc(col * sizeof(char));
Is there an error here! In the initial line you allocate "row" number of "slots", then your for loop tries to create "col" number of entries in the "row" number of slots.
But you only have a "row" number of slots to allocate and hence you get memory access errors when col > row!
cool it works now
thanks alot
this damn thing was driving me crazy, now i can continue my project to create wumpus world ugh,,...
thx
Originally posted by maes
I don't know if gcc can compile C++ code?
Sure it can,just make sure that your file has .cpp as extension.Heres an example command for compiling a C++ app:
gcc -o test test.cpp