Does that code set up a 2D array correctly?
You're right, I don't really understand dynamic memory very well and I'm trying to learn.
mw
Does that code set up a 2D array correctly?
You're right, I don't really understand dynamic memory very well and I'm trying to learn.
mw
No. It just creates a single dimension array. That's what you were doing, and like I said, free wasn't the problem. Look at the bottom of page one, where someone asked me how to allocate one. You've been shown how already. You're just ignoring what everyone's been telling you.
You will of course have to free the memory in reverse of how you allocate it. (Free each row's data, then free the main pointer.)
Quzah.
Hope is the first step on the road to disappointment.
Could you explain that example?
I don't understand the use of 2 asterisks in the first line.Code:char **twodee; int numofrows = 5, numofcols = 10, x; twodee = malloc( sizeof( char * ) * numofrows ); for( x = 0; x < numofrows; x++ ) twodee[ x ] = malloc( sizeof( char ) * numofcols );
Also, when you use the first malloc you have "char *", and in the second malloc you have "char" (without the asterisk). Why is that?
mw
PS: Just to clarify: the "rows" is the first subscript and the "columns" is the second subscript, right?
Last edited by Lionmane; 06-11-2005 at 12:25 AM.
It is a pointer to a pointer, used to allocate an array of pointers.Originally Posted by LionmaneOriginally Posted by Dave_SinkulaIt is usually best to allocate an array of pointers, and then initialize each pointer to a dynamically-allocated ``row.'' Here is a two-dimensional example:(In real code, of course, all of malloc's return values would be checked.)Code:#include <stdlib.h> int **array1 = (int **)malloc(nrows * sizeof(int *)); for(i = 0; i < nrows; i++) array1[i] = (int *)malloc(ncolumns * sizeof(int));
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Thanks Dave. I looked at that website, but I didn't follow what it was trying to say.
mw
From my first introduction to clc's FAQ to the present there have been and may yet be some things that I don't quite fully follow either. The more I learn, the more I find out that what is there is very succinctly put. These days I heed the advice first, and do my damndest to understand what may be confusing as I progress.Originally Posted by Lionmane
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
What you don't realize is that I've ALREADY heeded the advice twice (ie: I've already created 2 programs that use pointers and work correctly), but I have since discovered that I've been using them wrong.
Now I'm trying my DAMNDEST to find out how they work, but you guys seem to be more interested in insulting me or are too lazy to explain them. Don't get me wrong: I could care less about what you guys THINK of me, I'm just keeping this thread alive until I can find someone who's NOT too lazy or arrogant to actually explain to me in a way I can understand.
Quzah says I've been ignoring him, but he doesn't realize that when I ignore his insulting manner it will inevitably filter out useful information.
mw
You have now successfully manage to annoy two very knowledgable people who have tried to help you. This thread is closed.