new char

S

SJ

Hi:

I am reviewing some bad code. Help me on this! The following is a
modified version of the code.

whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?
This code sometimes crashes with a "memory could not be read" problem.

Can anyone explain me the internals of how memory is handled and what
causes the problem?

thanks
 
M

Mike Wahler

SJ said:
Hi:

I am reviewing some bad code. Help me on this! The following is a
modified version of the code.

whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?

Yes. But the above is just an elaborate way to do:

while(some_condition)
{
foo(' ');
}

No holes, no leaks.

This code sometimes crashes with a "memory could not be read" problem.

Probably not related to the above. But we can't know for
sure without seeing the rest of the code.
Can anyone explain me the internals of how memory is handled

The 'internals' are platform-specific.
and what
causes the problem?

Could be one or more of any number of things. If you can put
together a small compilable program which exhibits the problem
and post it here, we can probably find the error(s).

-Mike
 
J

JKop

SJ posted:
Hi:

I am reviewing some bad code. Help me on this! The following is a
modified version of the code.

whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?
This code sometimes crashes with a "memory could not be read" problem.

I suspect this is caused by either:

A) An invalid pointer, like the following:

int* p_k;

//p_k now contains gibberish

*p_k = 4;


B) Running out of memory

for(;;)
{
char* p_k = new char;
}

Can anyone explain me the internals of how memory is handled and what
causes the problem?


When you define a variable as follows:

int main()
{
int k = 4;
}


Then you don't have to worry about deallocating the memory, it gets done
automatically. "int k = 4;" is really an abbreviation of:

int auto k = 4;

("auto" as in it's taken care of automatically)

Moving on, there's a thing called "dynamic memory allocation". The thing
here is that you have complete control over when, where, how, who, what, and
why memory is allocated. So let's say I allocate some memory dynamically:

int main()
{
int* p_k = new int;
}


I dictate when and where it will be deallocated. In the above, the memory
for "p_k" is deallocated at the end of main, as it's an automatic variable,
ie.:


int* auto p_k = new int;


However, the memory allocated by the expression "new int" is never
deallocated. To deallocate it, I would do the following:

int main()
{
int* p_k = new int;

delete p_k;
}


That's simply how it works: With automatic variables, you just decide when
they're made and they get killed off automatically when you no longer need
them (ie. at the end of their scope). But with dynamic memory allocation,
you have exact duress over what's going on.


Which leads me onto the concept of "Garbage Collection". Garbage Collection
is something that's hyped up a lot by stupid people. For instance, have you
noticed how with every version of Windows, it gets more dumbed down and more
dumbed down. You open a menu in WinXP and it doesn't even show you the whole
contents of the menu, it just (very annoyingly) shows you the most used
elements. Anyway, the idea behind Garbage Collection is as follows:


int main()
{
int* p_k = new int;
}


Even though I haven't deallocated the dynamically allocated memory, it still
gets deallocated when the program ends by the "Garbage Collector".

Well, C++ isn't quite as dumbed down, but one can make a Garbage Collector
in C++ by overloading the operators "new" and "delete" with their own code
that (inefficiently) takes care of things.


Alternatively, if you want something very simple:

#include <memory> //not sure if that's the right header

int main()
{
std::auto_ptr<int> p_k = new int;
}


In the above we sort of have a Garbage Collector. The class "auto_ptr"
cleans things up for us when the automatic variable, ie.:

std::auto_ptr<int> auto p_k = new int;

goes out of scope.


-JKop
 
R

Rolf Magnus

SJ said:
Hi:

I am reviewing some bad code. Help me on this! The following is a
modified version of the code.

whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?

If it's not deleted, yes. But are you sure that foo() doesn't delete it?
This code sometimes crashes with a "memory could not be read" problem.

A memory leak means that you just waste memory by not freeing it after you
need it. But such a crash usually has another reason.
Can anyone explain me the internals of how memory is handled and what
causes the problem?

That's dependant on the platform and on the code. You might see such errors
e.g. when attempting to delete memory twice or when trying to access memory
that you didn't allocate before or that you already deallocated.
 
J

John Harrison

SJ said:
Hi:

I am reviewing some bad code. Help me on this! The following is a
modified version of the code.

whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?

Unless foo deletes the memory, yes there is.

Have you considered this code

while(some condition)
{
char x = ' ';
foo(&x);
}

Much better than your code UNLESS foo is expecting to delete the memory.
This code sometimes crashes with a "memory could not be read" problem.

Can anyone explain me the internals of how memory is handled and what
causes the problem?

Crashing is caused by a bug in your code, what that bug is is impossible to
say. Why not post all of the code?

I think you need to read a book. How memory is handled is a huge topic.

john
 
B

Bo Rydberg

SJ said:
whie(some condition)
{
char *x;
x = new char(' ');
foo(x);
}
The variable x is not deleted. So, there is a memory leak?
This code sometimes crashes with a "memory could not be read" problem.

Most likely the function foo takes a zero terminated c-style string. (void
foo(char* string)?)
This would mean that writing

foo("hello, world");

might work as intended. The "memory coud not be read" problem would then
be related to
passing an adress of memory and foo then keeps looking at the memory
following that adress
untill it hits a zero, \0, temination. If in your environment it got too far
it reaches some environmental
boundry causing the "memory could not be read" problem.

Regards,
Bo
 
P

Peter Koch Larsen

Mike Wahler said:
Yes. But the above is just an elaborate way to do:

while(some_condition)
{
foo(' ');
}

No holes, no leaks.



Probably not related to the above. But we can't know for
sure without seeing the rest of the code.


The 'internals' are platform-specific.


Could be one or more of any number of things. If you can put
together a small compilable program which exhibits the problem
and post it here, we can probably find the error(s).

-Mike
It's not because I'm chasing you, but here there's again a slight accuracy -
namely if the argument to foo is not declared const (and i doubt it is).
This snippet works if foo expects a char* (and not a char const*):

while(some_condition)
{
char c(' ');
foo(&c);
}

/Peter
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top