Will this code generates a segmentation fault???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 29
Reputation: ermithun is an unknown quantity at this point 
Solved Threads: 0
ermithun ermithun is offline Offline
Light Poster

Will this code generates a segmentation fault???

 
0
  #1
Jul 2nd, 2009
I have written a sample test code as,
  1. Class A
  2. {
  3. main ()
  4. {
  5. A *a;
  6. B *ptr;
  7.  
  8. B* A::func() const {
  9.  
  10. [COLOR="Green"]
  11. B* ptr = new B;
  12. return B;
  13. [/COLOR]
  14. }
  15. }
and
  1. Class B
  2. {
  3. [COLOR="Green"]
  4. // some code accessing the func() in class A
  5. [/COLOR][/COLOR]
  6. }

what i understand,
- everytime the func() is accessed from class B, a new memory location is allocated for the object
-but it is not deleted explicitly
- once the pointer is returned from a method in class A, the memory is
- If I continue to call func() say, for 1000 times(or any number), is it possible that it results into a segmentation fault if we try to access the ptr variable which is no longer existing, as there cud be other processes running (creating objects and operating system can allocate those memory to other objects which was initially reserved)

I am not sure if was able to make my query clear and understandable.

please help.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Will this code generates a segmentation fault???

 
0
  #2
Jul 2nd, 2009
Assuming that return B; should be return ptr; , there shouldn't be a segmentation fault. The memory doesn't go away until your code deletes it. As long as you have a pointer to the memory, it's safe to access. But if you don't ever delete it, that could cause a memory leak.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: ermithun is an unknown quantity at this point 
Solved Threads: 0
ermithun ermithun is offline Offline
Light Poster

Re: Will this code generates a segmentation fault???

 
0
  #3
Jul 2nd, 2009
oh!! that wa a typo.. yeah u assumed it rite , that was 'ptr' instead of B in the return statement.

yeah, i m not deleting it. so, i m going against the OOPs concept here.. when i am creating an object using 'new' operator and it shud be deleted using 'delete' operator explicitly. so if i never eve delete it , will it result into memory issue , specifically a segmentation fault.

thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Will this code generates a segmentation fault???

 
0
  #4
Jul 2nd, 2009
so if i never eve delete it , will it result into memory issue , specifically a segmentation fault.
Never deleting memory causes a memory leak, not a segmentation fault. A segmentation fault is when you try to access memory outside of your address space, like through an uninitialized pointer. It usually manifests as a program crash right away. A memory leak is silent and just eats up memory in the process until the computer runs out of memory and slows down or halts.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: ermithun is an unknown quantity at this point 
Solved Threads: 0
ermithun ermithun is offline Offline
Light Poster

Re: Will this code generates a segmentation fault???

 
0
  #5
Jul 2nd, 2009
Many thanks Tom. That answers.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Will this code generates a segmentation fault???

 
0
  #6
Jul 2nd, 2009
Originally Posted by ermithun View Post
oh!! that wa a typo.. yeah u assumed it rite , that was 'ptr' instead of B in the return statement.

yeah, i m not deleting it. so, i m going against the OOPs concept here.. when i am creating an object using 'new' operator and it shud be deleted using 'delete' operator explicitly. so if i never eve delete it , will it result into memory issue , specifically a segmentation fault.

thanks in advance.
Ya it will result in a memory issue but thats called a Memory Leak not a segmentation fault.Segmentation fault occurs when you access illegal memory(not assigned to your code).For example :
  1. int array[10];
  2. array[10]=10;//Segmentation fault because your limit is upto array[9]
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 29
Reputation: ermithun is an unknown quantity at this point 
Solved Threads: 0
ermithun ermithun is offline Offline
Light Poster

Re: Will this code generates a segmentation fault???

 
0
  #7
Jul 3rd, 2009
Hello All
can anyone tell me,
i am using a list class and creating a list in my code..
if I am not explicitly defining list::erase () in my class then can my code internally invoke this method and if yes then how?
I mean to say, is there any other method in list class, calling of which calls the erase() method internally?

Thanks in advance.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 430 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC