[Access Violation]: Debug Help

This is a discussion on [Access Violation]: Debug Help within the C++ Programming forums, part of the General Programming Boards category; Just trying to put null characters into an array. Might sound easy.. but even this little snippet of code makes ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question [Access Violation]: Debug Help

    Just trying to put null characters into an array. Might sound easy.. but even this little snippet of code makes my pc go crazy.

    This will compile, but will hang up at runtime.. and will cause an, "access violation" when ran through the debugger:

    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int main()
    {
    	int iCount = 3;
    
    	
            char **Lines = new char*[3];
    
    	Lines[0] = "H1007, 1-24\0", 
    	Lines[1] = "A, 25-36\0",
    	Lines[2] = "H1007, 37-48\0";
    
    	//Visual Verification:  everything works fine 
           for(int i=0; i<3; i++)	
    		
    		cout << Lines[i] << endl;
    
    
        char **Name = new char*[iCount];
        char **FiberCount1 = new char*[iCount];
        char **FiberCount2 = new char*[iCount];;
        int /*size,*/ word_index;
        bool cont = true;
    
        i=0;
        for(int j=0; i<iCount; i++, j=0)
        {   
            
            word_index = 0;
    	cont = true;
            
            do{        
             
                  //Extract Fiber Name
                  while(Lines[i][j]!=',')
                  {
                       j++;
                  }
             
                  //At this point, i==0 and j==5
                  Lines[i][j]='\0'; //This line causes "access violation"
                  Name[i] = new char[j-word_index+1];
                  strcpy(Name[i], Lines[i]+word_index);
                  //Name[i][j-word_index] = '\0';
    
    ...
    ...
    ...

    any ideas/suggestions on what to do would be cool

    using MVSC++ 6.0 (mostly for testing/debugging)
    Last edited by The Brain; 11-07-2005 at 01:44 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Try putting something else in instead of '\0' and see if you get an access violation.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    tried a few different characters.. same results..

    "unhandled exception.. ..access violation"
    Attached Images Attached Images  
    Last edited by The Brain; 11-07-2005 at 01:44 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Make sure you're not going out of your bounds in your array. If you're reaching for an array element that higher than you declared, then your reaching for a memory address that doesn't belong to your program. Try changing i and j to 1 and 1 and see if it runs.
    Last edited by SlyMaelstrom; 11-07-2005 at 02:17 AM.
    Sent from my iPadŽ

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    changed my array indexes.. same results...


    I hold windows XP personally responsible for not properly allocating my memory.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Can you attach the full source to download? I'd like to mess around with it a bit.
    Sent from my iPadŽ

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    It is because you are trying to modify a constant memory address. eg.
    Code:
    #include <iostream>
    
    int main()
    {
        char *test = "Hello";
        //Oh this works but it is a bad idea!
        test[0] = '\0';
        
        return 0;
    }
    This is ok though.
    Code:
    #include <iostream>
    #include <cstring>
    
    int main()
    {
        //Allocate out
        char *sayHello = new char[strlen("Hello") + 1];
        std::strcpy(sayHello,"Hello");
        
        std::cout<<sayHello<<std::endl;
        
        //Now this is ok because you didn't assign the pointer to a const
        //memory address aka the string
        sayHello[0] = 'p';
        
        std::cout<<sayHello<<std::endl;
        
        std::cin.get();    
            
        return 0;
    }
    Last edited by prog-bman; 11-07-2005 at 03:34 AM.
    Woop?

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    thanks hombre.. sounds like the moral of the story is.. 'thou shalt not attempt to modify a hard-coded statically declared string literal.."




    Second Question:

    Why does the following boolean test fail?
    Code:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int main()
    {
    	int iCount = 3;
    	 
            char **Lines = new char*[3];
    
    
    	//Lines = {{"H1001, "1-24"}, {"A, 25-36"}, {"H1007, 36-45"}};
    
    	Lines[0] = new char[strlen("H1007, 1-24\0")]; 
    	Lines[1] = new char[strlen("A, 25-36\0")];
    	Lines[2] = new char[strlen("H1007, 36-47\0")];
    
    	strcpy(Lines[0], "H1007, 1-24\0");
    	strcpy(Lines[1], "A, 25-36\0");
    	strcpy(Lines[2], "H1007, 36-47\0");
    
    	for(int i=0; i<3; i++)	
    		
    		cout << Lines[i] << endl;
    
    
        char **Name = new char*[iCount];
        char **FiberCount1 = new char*[iCount];
        char **FiberCount2 = new char*[iCount];;
        int /*size,*/ word_index;
    	bool cont = true;
    
        i=0;
        for(int j=0; i<iCount; i++, j=0)
        {   
            
            word_index = 0;
    		cont = true;
            
            do{        
             
                  //Extract Fiber Name
                  while(Lines[i][j]!=',')
                  {
                       j++;
                  }
             
                  Lines[i][j]='\0';
                  Name[i] = new char[j-word_index+1];
                  strcpy(Name[i], Lines[i]+word_index);
                  //Name[i][j-word_index] = '\0';
             
                  word_index = j;
    			  word_index++;
             
                  //Extract Beginning Fiber Count
                  while(Lines[i][j]!='-')
                  {
                       j++;
                  }
             
                  Lines[i][j] = '\0';
                  FiberCount1[i] = new char[j-word_index+1];
                  strcpy(FiberCount1[i], Lines[i]+word_index); 
                  //FiberCount1[i][j-word_index] = '\0';
             
                  word_index = j;
    			  word_index++;
             
                  //Extract Ending Fiber Count - Code works fine up until this point
                  //Even when the null character is reached.. the loop continues infinately
                  while(Lines[i][j]!=',' || Lines[i][j]!='\0')    
                  {
                       j++; 
                  }
    
                   if(Lines[i][j]=='\0')
    
    				  cont = false;
             
                  Lines[i][j] = '\0';
                  FiberCount2[i] = new char[j-word_index+1];
                  strcpy(FiberCount2[i], Lines[i]+word_index);
                  //FiberCount2[i][j-word_index] = '\0';
             
              }while(cont);  
    	}
    
    	return 0;
    
    }

    When viewed through the debugger.. it appears that even when Lines[i][j] equals NULL.. the loop continues on..?!?@

    *just for fun.. I just threw in a comma in place of the null character.. and the debugger saw the comma.. but the loop continued to loop..
    Last edited by The Brain; 11-07-2005 at 03:09 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Thou shalt use and "&&". Remeber || means or. So if(1==1|| 2 == 1) it will go through because one of the conditions is true.
    Code:
    #include <iostream>
    
    int main()
    {
        if(1 == 1 || 2 == 1)
        {
            std::cout<<"It works!!!!!!!! #1"<<std::endl;
        }
        
        if(1 == 1 && 2 == 1)
        {
            std::cout<<"It works!!!!!!!! #2"<<std::endl;
        }
        
        std::cin.get();
        
        return 0;
    }
    And no I will not make out with you.
    Last edited by prog-bman; 11-07-2005 at 03:14 PM.
    Woop?

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    jesus I am an idiot..!@#!@#
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Let's check with the survey.

    Survey Says: You are an idiot .
    Woop?

  12. #12
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think the survey is correct..!@#




    Parsing goodness.. thanks to senor bling


    Code:
    #include<iostream>
    #include<cstring>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
    	int iCount = 3;
    
    	 //Parse User Entered Text
             char **Lines = new char*[3];
    
    
    	//Lines = {{"H1001, "1-24"}, {"A, 25-36"}, {"H1007, 36-45"}};
    
    	Lines[0] = new char[strlen("H1007, 1-24\0")+1]; 
    	Lines[1] = new char[strlen("A, 25-36\0")+1];
    	Lines[2] = new char[strlen("H1007, 36-47\0")+1];
    
    	strcpy(Lines[0], "H1007, 1-24\0");
    	strcpy(Lines[1], "A, 25-36\0");
    	strcpy(Lines[2], "H1007, 36-47\0");
    
    	for(int i=0; i<3; i++)	
    		
    		cout << Lines[i] << endl;
    
    
        char **Name = new char*[iCount];
        char **FiberCount1 = new char*[iCount];
        char **FiberCount2 = new char*[iCount];;
        int /*size,*/ word_index;
    	bool cont = true;
    
        i=0;
        for(int j=0; i<iCount; i++, j=0)
        {   
            
            word_index = 0;
    	cont = true;
            
            do{        
             
                  //Extract Fiber Name
                  while(Lines[i][j]!=',')
                  {
                       j++;
                  }
             
                  Lines[i][j]='\0';
                  Name[i] = new char[j-word_index+1];
                  strcpy(Name[i], Lines[i]+word_index);
                  //Name[i][j-word_index] = '\0';
             
                  word_index = j;
    	      word_index++;
             
                  //Extract Beginning Fiber Count
                  while(Lines[i][j]!='-')
                  {
                       j++;
                  }
             
                  Lines[i][j] = '\0';
                  FiberCount1[i] = new char[j-word_index+1];
                  strcpy(FiberCount1[i], Lines[i]+word_index); 
                  //FiberCount1[i][j-word_index] = '\0';
             
                  word_index = j;
    	      word_index++;
             
                  //Extract Ending Fiber Count
                  while(Lines[i][j]!=',' && Lines[i][j]!='\0')  
                  {
                       j++; 
                  }
    
    	      if(Lines[i][j]=='\0')
    
    	           cont = false;
             
                  Lines[i][j] = '\0';
                  FiberCount2[i] = new char[j-word_index+1];
                  strcpy(FiberCount2[i], Lines[i]+word_index);
                  //FiberCount2[i][j-word_index] = '\0';
    
                  word_index = j;
    	      word_index++;
             
              }while(cont);  
    	}
    
    
    	cout << endl << endl << endl
    	     << setw(20) << "Fiber Name" << setw(20) << "Beginning Count" << setw(20) << "Ending Count"   << endl
    	     << setw(20) << "----------" << setw(20) << "---------------" << setw(21) << "------------\n" << endl
    	     << setw(20) << Name[0]      << setw(20) << FiberCount1[0]    << setw(20) << FiberCount2[0]   << endl
    	     << setw(20) << Name[1]	 << setw(20) << FiberCount1[1]    << setw(20) << FiberCount2[1]   << endl
    	     << setw(20) << Name[2]      << setw(20) << FiberCount1[2]    << setw(20) << FiberCount2[2]   << endl
    	     << endl << endl << endl ;
    
    	return 0;
    
    }
    Attached Images Attached Images  
    Last edited by The Brain; 11-07-2005 at 06:32 PM. Reason: added +1 to the size of all dynamic arrays that used strlen( )
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    That's why they pay me the big bucks.
    Woop?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Any reason you're not using C++ to solve this?

  15. #15
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    cstring character arrays were taught in my cs201 class as part of the c++ curiculum.. walter savitch teaches it in his c++ series books.. petzold's book is entirely in c (using the above algorithm in a windows project) what's so wrong about using cstrings in me' code..?!?!?


    "new" makes my code c++
    Last edited by The Brain; 11-07-2005 at 04:49 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 12:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM

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