I have written a sample test code as,

Class A
{
main ()
{
A *a;
B *ptr;

B* A::func() const {

    
B* ptr = new B;
return B;

}
}

and

Class B
{

// some code accessing the func() in class A

}

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

Recommended Answers

All 6 Replies

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.

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.

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.

Many thanks Tom. That answers.

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 :

int array[10];
array[10]=10;//Segmentation fault because your limit is upto array[9]

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.