Thread: Array of Global Classes

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    10

    Array of Global Classes

    I was wondering if someone can tell me if I can do the following, maybe even give a very basic code example for it:

    Create a Class, with an array of integers as members.

    Then declare a global array of that class.

    And if it's not too much trouble, show an example line of changing the value of the class in each iteration of the class.

    If it's asking too much, I understand. The book I am reading from has good text, but no example.

  2. #2
    Registered User lsctt's Avatar
    Join Date
    Jun 2006
    Posts
    30
    what book is it?
    I've Found that every obsticle can improve your situation...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    I figured it out. I figured if I made my structure a class, that it would fix my problem. But it didn't. This is my problem:

    I have an array global variable:

    Code:
    BOARD_INFO*      gBoardInfo;
    And the definition of BOARD_INFO is:

    Code:
    class BOARD_INFO {
    public:
    	char		name[BUFF_LEN];
    	int*		pChDev;
    	FEATURE_TABLE*	pFT;
    	ct_devinfo*	pDevInfo;
    	char		nameB[BUFF_LEN];
    	char		nameC[BUFF_LEN];
    	char		buff[BUFF_LEN];
    	int	                dev;
    	int		nr;
    	int		err;
    	int		line;
    	DX_IOTT* 	plott;
    	char		nmC[1024];
    	int		bd, ch;
    	int		bdV, chV;
    	char*		pChannel[BUFF_LEN];
    	long		devBRD;
    	LINEDEV*	                pLineDev;
    	int		nrChannels;
    };

    My code compiles fine, no errors what so ever. However, during runtime I get a error:

    "The instruction at "0x00401614" referenced memory at "0x00000000". The memory could not be "written".

    After putting in some debug code, to find out exactly what line the error happens, I found the violating line of code:

    Code:
    		for (ch = 0; ch < nr; ch++)
    		{
    			wsprintf (name,"dxxxB%dC%d", bd+1, ch+1);
    			gBoardInfo[bd].pChannel[ch] = _strdup (name);
    			plott = &gBoardInfo[bd].plott[ch];
    			plott->io_type = 0;  // <- Error Popup
    			plott->rfu = 0;
    			plott->io_fhandle = -1;
    			plott->io_bufp = NULL;
    			plott->io_offset = 0;
    			plott->io_length = 0;
    			plott->io_nextp = NULL;
    			plott->io_prevp = NULL;
    		}
    The error happens right when I use plott to assign values.

    The declaration of plott is:

    Code:
    	DX_IOTT*  	plott;
    The declaration of DX_IOTT is:

    Code:
    typedef struct dx_iott DX_IOTT;
    
    
    /*
     * DX_IOTT: I/O Transfer Table structure
     */
    struct dx_iott {
       unsigned short  io_type;      /* Transfer type */
       unsigned short  rfu;          /* reserved */
       int             io_fhandle;   /* File descriptor */
       char            *io_bufp;     /* Pointer to base memory */
       unsigned long   io_offset;    /* File/Buffer offset */
       long int        io_length;    /* Length of data */
       DX_IOTT         *io_nextp;    /* Pointer to next DX_IOTT if IO_LINK */
       DX_IOTT         *io_prevp;    /* (optional) Pointer to previous DX_IOTT */ 
    };
    I am fresh out of ideas of how to resolve this. Can someone tell me what it is that I am doing wrong? I've been dabbling with this error for weeks now, trying to overcome it on my own.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Where is gBoardInfo initialized.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    plott = &gBoardInfo[bd].plott[ch];
    Let's take a look at what plott is exactly:
    Code:
    DX_IOTT* 	plott;
    Have you allocated space for plott? From the code you posted, I have no way to tell if plott actually points to a valid position in memory.

    By the way, why is BOARD_INFO a class? If all its members are public, it might as well be a struct.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    To be honest, I dont think I have allotted the memory for the variable. How do I go about doing this? I used calloc to allocate the memory for multiple BOARD_INFO's... I thought maybe since plott is a child, that it would have been enumerated with it as well.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    gBoardInfo is initialized outside of all functions. I am assuming that it is global when I do that. I can already assign values to properties of gBoardInfo, so I'm good to go with it. My problem solely exists with the plott inside gBoardInfo

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    AH HA! Got it. Whew, I feel like a winner right now. My problem was, that I was not allocating the memory space for plott. Thanks for guiding me to the right direction. Seriously, thank you.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    plott is a pointer. C++ will treat it as a pointer, now and forever. C++ will do exactly as you say -- no more, and no less. Which includes not allocating memory for pointers. C++ does not assume that it knows what you want, and you certainly must not assume that it does.

    By the way, you can use the simpler memory allocation functions new and delete.
    Last edited by jafet; 06-05-2006 at 12:51 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  4. Array of classes within class definition
    By subdene in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2005, 10:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM