Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

access voilation

Posted on 1999-01-29
24
Medium Priority
?
323 Views
Last Modified: 2011-10-03
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
Comment
Question by:mantish
  • 7
  • 6
  • 5
  • +3
24 Comments
 

Expert Comment

by:_serega
ID: 1185474
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
 
LVL 22

Expert Comment

by:nietod
ID: 1185475
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
 
LVL 86

Expert Comment

by:jkr
ID: 1185476
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:chensu
ID: 1185477
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
 
LVL 23

Expert Comment

by:chensu
ID: 1185478
Oops, jkr already mentioned the KB article. Sorry.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1185479
Now I'm confused.  What is the significance of it being a first chance exception?
0
 

Author Comment

by:mantish
ID: 1185480
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
 
LVL 22

Expert Comment

by:nietod
ID: 1185481
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
 
LVL 86

Expert Comment

by:jkr
ID: 1185482
>>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
 
LVL 22

Expert Comment

by:nietod
ID: 1185483
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
 
LVL 86

Expert Comment

by:jkr
ID: 1185484
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
 
LVL 22

Expert Comment

by:nietod
ID: 1185485
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
 

Author Comment

by:mantish
ID: 1185486
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
 
LVL 22

Expert Comment

by:nietod
ID: 1185487
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
 

Expert Comment

by:Extreme
ID: 1185488
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
 

Author Comment

by:mantish
ID: 1185489
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
 
LVL 86

Expert Comment

by:jkr
ID: 1185490
nietod - there is no crash ;-)  ... just a debugger message
0
 
LVL 86

Expert Comment

by:jkr
ID: 1185491
mantish - changing your code to consider the size of memory allocations is always a good idea - regerdless of debugger complaints...
0
 

Author Comment

by:mantish
ID: 1185492
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
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1185493
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
 

Expert Comment

by:Extreme
ID: 1185494
shouldn't you be deleting 'style' not brand in the destructor?
0
 

Expert Comment

by:Extreme
ID: 1185495
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
 
LVL 22

Expert Comment

by:nietod
ID: 1185496
There is no need to test for NULL.  It is legal to delete a NULL pointer.
0
 

Author Comment

by:mantish
ID: 1185497
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question