• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

access voilation

I'm a c++ beginner & I'm using VC++ enetrprise edition v5.0
I'm trying to run a pgm & it is giving me an access violation error. the message is as follows
oaded 'C:\WINDOWS\SYSTEM\KERNEL32.DLL', no matching symbolic information found.
First-chance exception in Port.exe: 0xC0000005: Access Violation.
The thread 0xFFE36615 has exited with code -1 (0xFFFFFFFF).
The program 'C:\Program Files\DevStudio\MyProjects\Port\Debug\Port.exe' has exited with code -1 (0xFFFFFFFF).
I've no clue where to look for the error.
& heres the code
Port::Port(const char * br ,
               const char * st,
               int b){
   
     
        style = new char[20];  
            strcpy(style, br);
        
     
      bottles = b;
   cout<<" the style is :"<<style<<"\n";
   cout<<" the num of bottles is : "<<b<<"\n";

}
& style is declared char * style;
I'm guessing the problem is with the strcpy part
should be an easy 50pts

                       THANXS IN ADVANCE


     
0
mantish
Asked:
mantish
  • 7
  • 6
  • 5
  • +3
1 Solution
 
_seregaCommented:
this code is correct
exception throws in other part of program .
maybe - when you assign style to br and br contains more then 20 characters - anywhere else you may have an exception
0
 
nietodCommented:
serega has it backwards in a place.   (and misssed a possilbility)

The problem probably is :
A) that br points to a string that is more than 19 charactes long,  "Style" can only hold 19 characters and a NUL terminator.

B) the string br points to was intended to be no more than 19 characters, but is no terminated with a NUL character.  Thus it appears to be more than 19 charcters long.

C)  br is just an invalid pointer.  That is, it doesn't point to a valid string at all.  It might very well be NULL.  

Look at "br" and see what "you've got"  Look at the code that calls this constructor and see what it is passing.
0
 
jkrCommented:
nietod - it's the first time for me to say you're not absolutely correct ;-)
The debug output states that it's a 'first chance' exception, which is (opposite to a 2nd chance excpetion) handle successfully in the current SEH frame; it's only reported when a debugger is executing the program - you can think of it being the same as if you use code like this:

long l;

__try // set up current SEH frame
{
CopyMemory ( &l, 0, sizeof ( long)); // read from 0x00000000
}
__except( EXCEPTION_EXECUTE_HANDLER) // handler for current frame
{
puts ( "We knew that this would go wrong...");
}

(Additional info: MS KB Article Q105675)

mantish - this only occurs when running the program in a debugger isn't it? Anyway, the point about the char array is still true, better change

style = new char[20];  

to read:

style = new char[strlen(br) + 1];  

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
chensuCommented:
The following KB article explains it.

First and Second Chance Exception Handling
http://support.microsoft.com/support/kb/articles/q105/6/75.asp

You may ignore the first chance exceptions.
0
 
chensuCommented:
Oops, jkr already mentioned the KB article. Sorry.
0
 
nietodCommented:
Now I'm confused.  What is the significance of it being a first chance exception?
0
 
mantishAuthor Commented:
Sorry for not providing the complete code.Anyway my code doesnt pass a string longer
than 15 chars.Heres the calling part.
 
   int main(){
             Port("hey you","not you", 26);
             return 0;
            }
0
 
nietodCommented:
I don't see a problem. with the constructor.  Perhaps it is the destructor (called at the end of main()).  What does the destructor do?
0
 
jkrCommented:
>>Now I'm confused.  What is the significance of it being a
>>first chance exception?
A first chance exception is called so as it is passed to a debugger before the application 'sees' it. This is done by sending a 'EXCEPTION_DEBUG_EVENT' to the debugger, which can now decide whether it is passed to the apllication to handle it or 'ignore' it (e.g. like an 'EXCEPTION_BREAKPOINT' aka 'int 3')

Does the exception occur when starting the program from the command line?

0
 
nietodCommented:
I still don't understand.  What does the fact that it is a 1st chance exception imply?  Does it mean it was caught by the RTL before the error actually occured, or something?  i.e does the RTL detect it and "pass" it to the debugger..  Or am I off base?
0
 
jkrCommented:
nietod - you seem to be a little 'off base'. '1st chance' simply means that an exception was _detected_, but it was handled _successfully_ inside the handler (see the code example above). If the exception handler decided that it is too severe to handle it (by signalling 'EXCEPTION_CONTINUE_SEARCH' or 'EXECEPTION_EXECUTE_HANDLER' instead of 'EXCEPTION_CONTINUE_EXECUTION'), it becomes a '2nd chance' exception, the debugger 'sees' it the 2nd time and will usually terminate the program (without using a debugger, these exceptions end up at 'UnhandledExceptionFilter()' which will also signal the exception to the user with one of these 'nice' message boxes and terminate the program, also...)
0
 
nietodCommented:
getting clearer.  If it was handled, why the crash? Or was the termination someones idea of handling the problem?  And who handled it, the RTL?
0
 
mantishAuthor Commented:
I'm actually using the default destructor.I mean I dont define my own.
U think that create a problem.
            So I guess my my destuctor should look something like.

    ~port(){
        delete style[] ;
}
lemme see if this works.
 
                                                                                      Mantish
0
 
nietodCommented:
I was hoping you had a destructor that did something wrong.  If you had no destructor, there is no opportunity (in this class) for a problem in destruction.  So your try won't fix anything (but is not in vain as I assume you want that destructor eventually anyways.)

I don't see a problem.  There's go to be be more to this program. and there's got to be an error in it.

Can you use the debugger to see where it is dieing?
0
 
ExtremeCommented:
nietod is right, the program works fine, however, since no one has said it yet, that's most likely a memory error you're receiving there and it's probably being thrown when something that has been deleted is deleted (well, attempted) again.
0
 
mantishAuthor Commented:
OK guys I seem to have mislead u , coz al this action is actually taking place in the debugger itself . I mean all these
messages belong to the debugger So I  guess JKR was in the right direction when he said:
mantish - this only occurs when running the program in a debugger isn't it? Anyway, the point about the char array is still true, better change
Hope this bit of info helps.
0
 
jkrCommented:
nietod - there is no crash ;-)  ... just a debugger message
0
 
jkrCommented:
mantish - changing your code to consider the size of memory allocations is always a good idea - regerdless of debugger complaints...
0
 
mantishAuthor Commented:
I jus did that butit still didnt work.This time I did not run it
in the debug mode but it still gave me errors.this time it was the following
  exp: BLOCK_TYPE_IS _VALID(pHead->nBlockUse)
 for documentation on asserts failure see vc++ documentation.
In case u wanted the complete code again :
/*********************************/
#include <iostream.h>
#include <string.h>
#include "Port.h"




Port::Port(const char * br ,
               const char * st,
               int b){
   
     
        style = new char[50];  
            strcpy(style, br);
                
          bottles = b;
   cout<<" the style is :"<<style<<"\n";
   cout<<" the num of bottles is : "<<b<<"\n";

}
void Port:: show() const{
     
     cout<<" the num of bottles is: "<<bottles;
}
int main(){
     Port prt = Port("heythere","heyou", 26);
      
         cout<<"hey satty";
      return 0;
}
here is port.h
/*****************/
class Port {
private:
      char * brand ;
    char * style;
      int bottles;
public :
      Port(const char * br = "none", const char * st = "none",
             int b = 0 );
      Port(const Port & p);
      virtual ~Port(){ delete [] brand;}
      Port & operator=(const Port & p);
      Port & operator+=(int b);
      Port & operator-=(int b);
      //int BottleCount() const { return bo
      virtual void show() const;
      friend ostream & operator<<(ostream & os, const Port & p);
};
0
 
jkrCommented:
I see the problem:
--> virtual ~Port(){ delete [] brand;}

You're using the 'delete[]' operator on the uninitialized member 'char* brand'...

To remedy this:

Port::Port(const char * br ,
   const char * st,
   int b){
   
// initialize the member to 'NULL'

brand = NULL; // <--
      
        style = new char[50];  
      strcpy(style, br);
           
          bottles = b;
   cout<<" the style is :"<<style<<"\n";
   cout<<" the num of bottles is : "<<b<<"\n";

}

Then, change the destructor to
virtual ~Port(){ if ( brand) delete [] brand;}

so it will only delete 'brand' when it points to a valid memory area...

0
 
ExtremeCommented:
shouldn't you be deleting 'style' not brand in the destructor?
0
 
ExtremeCommented:
also, it's a good idea to initiate your pointers to NULL or 0, then when deleting, ask
if(variable == NULL) then delete variable
and set the variable back to null
variable = NULL

0
 
nietodCommented:
There is no need to test for NULL.  It is legal to delete a NULL pointer.
0
 
mantishAuthor Commented:
OK guys problem solved. it sure was the destructor.I kept changing the code & forgot to change the code in destructor &
as pointed out by JKR I was trying to delete an uninitialized ptr.
     Thanxs alot for ur help
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now