Deallocating memory for non members?

A

AMT2K5

If I have the class

class IOLabel : public IOField
{
private:
int len;
char format[11];

public:
IOLabel(int row, int col, int len):IOField(row, col)
{
this->len = len;
char *str = (char *)malloc(sizeof(len) + 1);
strcpy(str,'\0');
data = &str;
}
IOLabel(const char *str, int row, int col):IOField(row,col)
{
this->len = (int)(strlen(str) + 1);
char *temp = (char *)malloc(sizeof(len) + 1);
strcpy(temp,str);
data = &temp;
}

~IOLabel(){} //Deallocating non members?
};

How I de-allocate str and temp?

I love being a newbie :)
 
V

Victor Bazarov

AMT2K5 said:
If I have the class

class IOLabel : public IOField
{
private:
int len;
char format[11];

public:
IOLabel(int row, int col, int len):IOField(row, col)
{
this->len = len;
char *str = (char *)malloc(sizeof(len) + 1);
strcpy(str,'\0');
^^^^^^^^^^^^^^^^
This is not going to compile. Did you mean to use 'calloc' instead?
data = &str;

What's "data"?
}
IOLabel(const char *str, int row, int col):IOField(row,col)
{
this->len = (int)(strlen(str) + 1);
char *temp = (char *)malloc(sizeof(len) + 1);

What's "sizeof(len)"? Don't you mean 'len'?
strcpy(temp,str);
data = &temp;

Again, what's "data"?
}

~IOLabel(){} //Deallocating non members?
};

How I de-allocate str and temp?

You apparently don't. You essentially lose those. It's called "memory
leak". In order to deallocate them you need (a) decide when you don't
need those any more and (b) retain those pointers so you can pass them
to the 'free' function.
I love being a newbie :)

Then you should consider stopping asking all those questions :)

V
 
A

AMT2K5

I didnt paste the 5 other classes,

yes I didnt include free in the destructor

but I cant use ~IOLabel(){free(str);} because it is a non member
 
J

John Carson

AMT2K5 said:
I didnt paste the 5 other classes,

yes I didnt include free in the destructor

but I cant use ~IOLabel(){free(str);} because it is a non member

Exactly. So make it a member if you need the memory to persist beyond the
life of the constructor. If you don't need the memory beyond the life of the
constructor, then call free(str) within the constructor. (It is doubtful if
you should be using these C-style constructs at all, but that is another
question.)
 
R

red floyd

AMT2K5 said:
If I have the class

class IOLabel : public IOField
{
private:
int len;
char format[11];

public:
IOLabel(int row, int col, int len):IOField(row, col)
{
this->len = len;
char *str = (char *)malloc(sizeof(len) + 1);
Are you sure you want sizeof(len)? That's only the size of an int. If
you use malloc()-- which you shouldn't, you should use new -- it
doesn't need to be cast, malloc() returns void*, which converts to any
pointer.

char *str = new char[len + 1];
strcpy(str,'\0');
data = &str;
}
IOLabel(const char *str, int row, int col):IOField(row,col)
{
this->len = (int)(strlen(str) + 1);
char *temp = (char *)malloc(sizeof(len) + 1);
same as above.
strcpy(temp,str);
data = &temp;
}

~IOLabel(){} //Deallocating non members?
};

How I de-allocate str and temp?
If you use malloc(), you use free() in the destructor.
If you use new, you use delete[] in the destructor.
 
R

Rolf Magnus

AMT2K5 said:
If I have the class

class IOLabel : public IOField
{
private:
int len;
char format[11];

public:
IOLabel(int row, int col, int len):IOField(row, col)
{
this->len = len;
char *str = (char *)malloc(sizeof(len) + 1);
strcpy(str,'\0');
data = &str;
}
IOLabel(const char *str, int row, int col):IOField(row,col)
{
this->len = (int)(strlen(str) + 1);
char *temp = (char *)malloc(sizeof(len) + 1);
strcpy(temp,str);
data = &temp;
}

~IOLabel(){} //Deallocating non members?
};

How I de-allocate str and temp?

I love being a newbie :)

It's best to avoid fiddling around with raw memory yourself, especially when
you're a newbie. Use std::string instead of raw character arrays, and you
don't need to worry about the memory anymore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top