May 15, 2011 at 12:56pm UTC
i have this
type **a = NULL; // i need use as this a[i]->foo();
void alloc_it(type ***b, int c)
{
for(int i=0; i<c; i++)
*mem[i]=new type();
}
but it crash :S
May 15, 2011 at 1:56pm UTC
"mem" is undeclared in this scope, unless it's a global variable, which you shouldn't do with dynamicly allocated memory without a bunch of bounds checks.
May 15, 2011 at 2:36pm UTC
sorry i wrote it from scratch... it's *b[i]=new type();
May 15, 2011 at 2:43pm UTC
Ok... "type()" does not name a data type. I'm not even compiling this yet, I can tell just by looking at it you're pretty far off base.
Are you sure a class with a constructor wouldn't be a better move for you?
May 15, 2011 at 2:57pm UTC
type is a structure
struct type
{
type() : p(0) {}
~type() delete p; {}
void foo();
void *p;
};
now i tryied:
void _alloc(type **b)
{
(*b)=new type();
}
void alloc_it(type ***b, int c)
{
b=new type*[c];
for(int i=0; i<c; i++)
_alloc(&(*b)[i]);
}
but now is crashing only in release mode...
May 15, 2011 at 3:13pm UTC
Sorry if this is off topic but I need to ask everytime I see a void pointer like that. Are you trying to write a class that is able to deal with multiple data types? Do you know how to write a class template?
May 15, 2011 at 5:44pm UTC
I know but let me tell you what i'm trying to do.
In fact i'm writing a class that parse in memory a file.
my code is like that
MemoryFile **files;
int WriteAllFilesInMemory(MemoryFile ***files)
{
// here i need alloc the pointer. I know how many files there are and the size of a single file
}
Last edited on May 15, 2011 at 5:44pm UTC