Thread: Access Violation I dont understand.

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Access Violation I dont understand.

    The bold line below causes an access violation, I dont understand why... The second copy is what I did to fix it, can someone explain why the first one dosen't work?

    Code:
    	char* t = new char[10];
    	//load texture paths
    	for(int i = 0; i < 5; i++) {
    		//get the texture level by id
    		string texLevel = "TextureLevel";
    		texLevel += _itoa(i+1,t,10);
    		t = "      ";
    		temp->numTextures[i] = GetIntFromINI(texLevel,"Count",file,0);
    
    		//load all the paths on that level
    		for(int j = 0; j < temp->numTextures[i]; j++) {
    			string path = "Path";
    			path += _itoa(j+1,t,10);
    			t = "      ";
    			temp->textureList[i].push_front(new string(GetFromINI(texLevel,path,file,"")));
    		}
    	}
    	delete [] t;

    Code:
    	char* t;
    	//load texture paths
    	for(int i = 0; i < 5; i++) {
    		//get the texture level by id
    		string texLevel = "TextureLevel";
    		t = new char[10];
    		texLevel += _itoa(i+1,t,10);
    		delete [] t;
    		temp->numTextures[i] = GetIntFromINI(texLevel,"Count",file,0);
    
    		//load all the paths on that level
    		for(int j = 0; j < temp->numTextures[i]; j++) {
    			string path = "Path";
    			t = new char[10];
    			path += _itoa(j+1,t,10);
    			delete [] t;
    			temp->textureList[i].push_front(new string(GetFromINI(texLevel,path,file,"")));
    		}
    	}
    	delete [] t;

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here you assign a constant string pointer to t:
    Code:
    			t = "      ";
    Here you try to write into that const string:
    Code:
    			path += _itoa(j+1,t,10);
    Here you try to delete that constant string pointer:
    Code:
    	delete [] t;
    If you want to copy a string into a char * buffer you must use strcpy():
    Code:
    strcpy(t, "      ");

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I only had

    Code:
    t = "     ";
    because I was worried about overlapping numbers from the 'i' and 'j' using the same temporary array. 'i' will allways be a single digit, but 'j' could have multiple digits.

    I dont even think that is a problem with _itoa is it, im not sure. Am I making an sence? Its really late...

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The problem is that your compiler marks the " " as read-only which means when itoa() tries to write to that memory location it causes a fault. Its not really a problem with one particular function or another as it is a side affect of the compiler marking it as read only. Most compilers I've seen do this but there are a couple that don't.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You don't need to use new/delete - a simple
    char t[10];
    will suffice.

    Nor do you need to keep setting it to spaces each time around the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    I have allways considered it as though I was assigning a value, read-only never occured to me. Good to know, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  3. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 10:25 AM
  4. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 10:22 AM
  5. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM