trouble initializing a dynamic array of pointers to NULL

This is a discussion on trouble initializing a dynamic array of pointers to NULL within the C++ Programming forums, part of the General Programming Boards category; I have created a dynamic array of pointers to structures as shown below. I am trying to initialize them all ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    8

    trouble initializing a dynamic array of pointers to NULL

    I have created a dynamic array of pointers to structures as shown below. I am trying to initialize them all to NULL but I keep getting errors. Can anyone tell me what I'm doing wrong? Thanks.
    Code:
    #include <iostream>
    
    using namespace std; // So the program can see cout and endl
    
    struct Window{
           int handle;
           bool test;
    };
    
    Window * Array = new Window[260];
    
    int main()
    {
      
      for ( int x = 0; x < 260; x++ ) {
          Array[x] = NULL;  
          cout<< x <<endl;
      }
      cin.get();
    }
    The error I get is: 16 no match for 'operator=' in '*((+(((unsigned int)x) * 8u)) + Array) = 0'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The problem is that you do not have a dynamic array of pointers. You have a pointer to the first element of a dynamic array of Window objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    8
    Quote Originally Posted by laserlight View Post
    The problem is that you do not have a dynamic array of pointers. You have a pointer to the first element of a dynamic array of Window objects.
    Ahhhh! You're right. I should have done

    Window ** Array = new Window*[260];

    Sheesh, I've been staring at my computer too long. Now the variable Array is a pointer to a dynamic array of pointers to structs of Windows, correct? Does this mean that if I want to get a struct data member at a particular index I have to call Array[index]->test?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by supernater
    I should have done

    Window ** Array = new Window*[260];
    It could well be even better to #include <vector> and make Array a local variable in the global main function with:
    Code:
    std::vector<Window*> Array(260, 0);
    Quote Originally Posted by supernater
    Now the variable Array is a pointer to a dynamic array of pointers to structs of Windows, correct?
    Yes, though more accurately it is a pointer to the first pointer of a dynamic array of pointers to Window objects.

    Quote Originally Posted by supernater
    Does this mean that if I want to get a struct data member at a particular index I have to call Array[index]->test?
    Yes, if test is what you want to access, and assuming that Array[index] is a valid Window object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by supernater View Post
    Ahhhh! You're right. I should have done

    Window ** Array = new Window*[260];

    Sheesh, I've been staring at my computer too long. Now the variable Array is a pointer to a dynamic array of pointers to structs of Windows, correct? Does this mean that if I want to get a struct data member at a particular index I have to call Array[index]->test?
    Yes, that is correct. Of course you'd need to allocate an item to position 'index' before that code could be run.

    Don't forget to use delete Array[index] for all allocated items, and delete[] Array; afterwards too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    I believe to declare a dynamic array, we'd do something like this:

    Code:
        ...
        Window * dynamicArray;
        dynamicArray = new Window[some int here];
        //Be sure to check that the array could be created
        ...
    Then, if we were to access the test member at index i, I believe it would be dynamicArray[i].test.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I would use a std::vector as laserlight suggested, but if you still feel like using raw pointers, you can use std::fill() to set them all to NULL.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Vectors are very good for dynamic arrays. It's basically what they were built for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    Quote Originally Posted by Zach_the_Lizard View Post
    I believe to declare a dynamic array, we'd do something like this:

    Code:
        ...
        Window * dynamicArray;
        dynamicArray = new Window[some int here];
        //Be sure to check that the array could be created
        ...
    There is no need to check if the array is created, unless there is some special low memory action you wish to take. And if you do check, then you need a try catch block, which you do not show. When fails to allocate memory it throwx an exception, which will cause a clean exit if it is never caught. There is an alternate version of new that does not throw, but has a special syntax.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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