| | |
Will this code generates a segmentation fault???
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
I have written a sample test code as,
and
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
C++ Syntax (Toggle Plain Text)
Class A { main () { A *a; B *ptr; B* A::func() const { [COLOR="Green"] B* ptr = new B; return B; [/COLOR] } }
C++ Syntax (Toggle Plain Text)
Class B { [COLOR="Green"] // some code accessing the func() in class A [/COLOR][/COLOR] }
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
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
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
so if i never eve delete it , will it result into memory issue , specifically a segmentation fault.
-Tommy (For Great Justice!) Gunn
•
•
•
•
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.
c++ Syntax (Toggle Plain Text)
int array[10]; array[10]=10;//Segmentation fault because your limit is upto array[9]
I Surf in "C"....
•
•
Join Date: Jun 2009
Posts: 29
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- Passing linked list to function, segmentation fault (C)
- segmentation fault (C++)
- Segmentation Fault (C++)
- Segmentation fault during execution of code. (C++)
- segmentation fault (C)
- Access Violation (Segmentation Fault) + atol (C++)
- unix/C++ segmentation fault (C++)
- what is the best way to track segmentation fault errors (C++)
Other Threads in the C++ Forum
- Previous Thread: Need some help understanding MFC
- Next Thread: where do i start?
Views: 430 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





