how to alloc this
May 14, 2011 at 2:01pm UTC
Hi all,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void myfunc(type **t)
{
// i want to alloc it
}
int main()
{
type *a = NULL;
myfunc(&a);
if (!a)
//error...
a->....
...
return 1;
}
i tried
t = new type*;
(*t) = new type();
but it crash...
May 14, 2011 at 2:13pm UTC
What exactly are you trying to allocate? What is type
? Do you want to allocate a single element, or an array. If it is the second one, how are you going to tell myfunc
this?
May 14, 2011 at 2:16pm UTC
type is a structure like this
struct type
{
type();
~type();
void func1();
void func2();
};
May 14, 2011 at 2:47pm UTC
This one should work, as far as I can see. What is in the
type
constructor?
[edit]And, of course, Remember to free allocated memory before you quit :)
Last edited on May 14, 2011 at 3:10pm UTC
May 14, 2011 at 4:10pm UTC
struct type
{
type();
~type();
void *mem;
long len;
}
type::type() : mem(0), len(0)
{
}
May 14, 2011 at 4:19pm UTC
Run your debugger and you should be able to discover exactly where the error is.
As a guess, do try to free mem
in your destructor without checking if it's zero?
Topic archived. No new replies allowed.