• Navigation
Results 1 to 3 of 3
  1. #1
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,806
    BG Level
    8

    dynamic memory with pointers and 2d arrays

    Hi I am having a massive difficulty setting up a 2d array with pointers and dynamic memory. I can create it but I can't access it. If I try then I get a segfault.

    Code:
        void createArray ( char** &arrayPntr, int lenX, int lenY )
        {
            arrayPntr = new char*[lenY];
            for ( int x = 0; x < lenY; x++ )
            {
                *arrayPntr = new char[lenX];
                arrayPntr++;
            }
        }
    This creates an array of pointers and each location then points to a character array. I want to initialize the entire array to contain O so I tried doing this:

    Code:
         void initializeArray ( char** arrayPtr, int lenX, int lenY )
        {
            for ( int y = 0; y < lenY; y++ )
            {
                for ( int x = 0; x < lenX; x++ )
                {
                    **arrayPtr = 'O';
                }
                arrayPtr++;
            }
        }
    Any ideas why I am getting a segfault? How do I access the character arrays at the bottom of my 2d array? I am at a loss here.

    Thanks for the help.

  2. #2
    United States of Smash!
    Join Date
    Jun 2006
    Posts
    8,806
    BG Level
    8

    Figured out what I was doing wrong. Forgot to pass the first pointer by reference. Got it all fixed now.

  3. #3
    Science Fiction Super Fan
    Join Date
    Jul 2006
    Posts
    3,053
    BG Level
    7
    FFXI Server
    Cerberus

    fuck i hated pointers in my advanced C++ class back in 2000, glad i don't have to program

Similar Threads

  1. connection with ps2 and verizon fios
    By Sehi in forum Tech
    Replies: 0
    Last Post: 2007-12-20, 18:34
  2. Replies: 2
    Last Post: 2007-10-17, 12:28
  3. Replies: 19
    Last Post: 2007-03-12, 18:18