2D Dynamically allocated pointer arrays

This is a discussion on 2D Dynamically allocated pointer arrays within the C Programming forums, part of the General Programming Boards category; Does that code set up a 2D array correctly? You're right, I don't really understand dynamic memory very well and ...

  1. #31
    Registered User
    Join Date
    May 2005
    Posts
    207
    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

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #33
    Registered User
    Join Date
    May 2005
    Posts
    207
    Could you explain that example?

    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 );
    I don't understand the use of 2 asterisks in the first line.

    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.

  4. #34
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Lionmane
    I don't understand the use of 2 asterisks in the first line.
    It is a pointer to a pointer, used to allocate an array of pointers.
    Quote Originally Posted by Dave_Sinkula
    It 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:
    Code:
    #include <stdlib.h>
    
    	int **array1 = (int **)malloc(nrows * sizeof(int *));
    	for(i = 0; i < nrows; i++)
    		array1[i] = (int *)malloc(ncolumns * sizeof(int));
    (In real code, of course, all of malloc's return values would be checked.)
    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.*

  5. #35
    Registered User
    Join Date
    May 2005
    Posts
    207
    Thanks Dave. I looked at that website, but I didn't follow what it was trying to say.

    mw

  6. #36
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Lionmane
    Thanks Dave. I looked at that website, but I didn't follow what it was trying to say.
    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.
    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.*

  7. #37
    Registered User
    Join Date
    May 2005
    Posts
    207
    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

  8. #38
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You have now successfully manage to annoy two very knowledgable people who have tried to help you. This thread is closed.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2007, 08:21 PM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 05:53 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21