By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

Deallocating memory for non members?

P: n/a
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 :)

Nov 5 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
AMT2K5 wrote:
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
Nov 5 '05 #2

P: n/a
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

Nov 5 '05 #3

P: n/a
"AMT2K5" <Aa*********@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com
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.)

--
John Carson

Nov 5 '05 #4

P: n/a
AMT2K5 wrote:
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.
Nov 5 '05 #5

P: n/a
AMT2K5 wrote:
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.

Nov 5 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.