By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,529 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,529 IT Pros & Developers. It's quick & easy.

Is this OK?

P: 42
I have a segmentation fault, and I am looking for it!

I am wondering if it is OK to do that:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class A{
  3.            public:
  4.                   B* myfunc(...);
  5. }
  6.  
  7.  
  8. void func(A * arg)
  9. B* test=new B();
  10.  
  11. test= arg->myfunc(...);
  12.  
  13. ....
  14.  
  15. }
  16.  
  17.  
may I do that or not?
I usually use memcpy if I know the size of B... but here... No idea!

thanks,

Amir.
Jun 15 '07 #1
Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This code:
class A{
public:
B* myfunc(...);
}


void func(A * arg)
B* test=new B();

test= arg->myfunc(...);

....

}
shows test allocated as a B*. Fine. But the next line of code overstores the address in test with the return from arg->myfunc().

At the least you leak the original allocation.

However, I have no idea what arg->myfunc() has put in test.

From here on you have indeterminate processing.
Jun 15 '07 #2

P: 42
This code:


shows test allocated as a B*. Fine. But the next line of code overstores the address in test with the return from arg->myfunc().

At the least you leak the original allocation.

However, I have no idea what arg->myfunc() has put in test.

From here on you have indeterminate processing.

so what shall I do?? have any idea?
Jun 15 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Let's start by asking what it is you are trying to do?
Jun 15 '07 #4

P: 42
Let's start by asking what it is you are trying to
do?
I just want to do

test= arg->myfunc(...);

safely with no memory leakage!
Jun 16 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
I just want to do

test= arg->myfunc(...);

safely with no memory leakage!
Then you need to use handles rather than pointers. I have an article in the C/C++ Articles section that does contain a template for such a handle.

Read the article, copy the template to your code and chnage your code you use handle objects. Your leaks will go away.
Jun 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.