Thread: trouble initializing a dynamic array of pointers to NULL

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,149
    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, 12:25 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM