how to alloc this

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...
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?
type is a structure like this

struct type
{
type();
~type();

void func1();
void func2();

};

i tried
 
(*t) = new type();

but it crash...


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
struct type
{
type();
~type();

void *mem;
long len;
}

type::type() : mem(0), len(0)
{
}
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.