Thread: Problem with a 2D array.

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Problem with a 2D array.

    Hi I'm dynamically creating a 2D array and I'm trying to write somethin to it but I get a run-time error that memory could not be read. I use the exact same code a few lines before it and it works fine, but this one fails. Here's the code:
    Code:
               Vic **mtxVicValsB;
    	mtxVicValsB = new  Vic*[vars.NumScan];
    
    	for(i=0; i<vars.NumScan; i++)
    		mtxVicValsB[i] = new Vic[VicSize3];
    
    	mtxVicValsB[0][0].Name = "name";    <-- This line gives an access violation error
    
    // and Vic is
    
              struct Vic
    	{
    		float Value;
    		CString Name;
    	};
    Any ideas on what's going on and how to get rid of the propblem?
    Everything is relative...

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'm not familiar with the CString class, but I assume that the proper operator= is defined.
    Make sure your sizes for the array are positive.

    And, what compiler/OS?

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I'm using MS 6.0. and I I passed it a wrong variable to allocate the size. That's why it was getting confused. It wrks llike a charm now.

    thanks though.
    Everything is relative...

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    in making my simple chess game, i used this to make a 1d array a 2d array


    Code:
    int fun(int x, int y)// najes 1d array 2d array
    {
    	return (8*y+x);
    }

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Word of caution. MSVC 6 is a pre-standard compiler, and tends to do some things in "funny" ways. Might want to consider upgrading to a standards compliant compiler at some point: gcc (Dev-C++), or .NET, or something similar.

    Glad you got it working.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with 2D array
    By Stlcardinal50 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2009, 03:23 PM
  2. 8 Queens, problem with searching 2D array
    By Sentral in forum C++ Programming
    Replies: 35
    Last Post: 03-11-2009, 04:12 PM
  3. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  4. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM
  5. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 02:23 PM