Re: I'm a newbie. Is this code ugly?
In message <4b594ee2$0$1115$4fafbaef@reader4.news.tin.it>, io_x
<a@b.c.invalid> writes
"Richard Herring" <junk@[127.0.0.1]> ha scritto nel messaggio
news:FLTpfxBUtFWLFwWP@baesystems.com...
In message <4b57ff58$0$1135$4fafbaef@reader1.news.tin.it>, io_x
<a@b.c.invalid> writes
char* faiMemCopia(char* v)
Why are you (badly) reinventing strcpy() ?
{int i, n;
char *p;
if(v==0) R 0;
n=strlen(v);
if(n<=0) R 0;
Why? There's nothing intrinsically wrong with a zero-length string.
yes here i would say "if(n<0) R 0;"
And under what circumstances would strlen() ever return a negative value?
it seems strlen() here returned one unsigned type: size_t
here size_t is "unsigned int" of 32 bit but this means
strlen can return 0xF0000000 that is a negative number
seen it like int;
Only because you are perversely choosing to cast it to a signed type
when it is not.
and for me that negative number is not ok
So you should be declaring the variable n as size_t, not int.
[...]
what i can do is
p=(Y*) malloc(sizeof(Y));
p->inizialize();
and
for deallocate it
p->deinizialize();
free(p);
The C++ way to do that is:
p = new Y;
/* ... */
delete p;
and the constructor and destructor of Y will be called without any work on
your part.
thank you, i did not think about that, thanks
why the default operator delete
has one argument of type size_t?
It doesn't. A class-specific operator delete may have a second argument
of type size_t.
void operator delete(void*, size_t);
there is something hidden in the call delete?
Don't confuse the delete expression "delete p;" with the deallocation
function named "operator delete()". They do different things.
for example here
T a(1,1,1);
T *p=new T(a)
Why the unnecessary copy? Just write:
T * p = new T(1,1,1);
...
delete p;
[snip horrid C-style code]
--
Richard Herring