new char

Discussion in 'C++' started by SJ, Oct 19, 2004.

  1. SJ

    SJ Guest

    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
     
    SJ, Oct 19, 2004
    #1
    1. Advertising

  2. SJ

    Mike Wahler Guest

    "SJ" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 19, 2004
    #2
    1. Advertising

  3. SJ

    JKop Guest

    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
     
    JKop, Oct 19, 2004
    #3
  4. SJ

    Rolf Magnus Guest

    SJ wrote:

    > 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.
     
    Rolf Magnus, Oct 19, 2004
    #4
  5. "SJ" <> wrote in message
    news:...
    > 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
     
    John Harrison, Oct 19, 2004
    #5
  6. SJ

    Bo Rydberg Guest

    "SJ" <> wrote in message
    news:...
    >
    > 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
     
    Bo Rydberg, Oct 19, 2004
    #6
  7. "Mike Wahler" <> skrev i en meddelelse
    news:HAbdd.1062$...
    >
    > "SJ" <> wrote in message
    > news:...
    >> 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
    >
    >

    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
     
    Peter Koch Larsen, Oct 20, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    594
  2. wwj
    Replies:
    24
    Views:
    2,569
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    504
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    624
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
Loading...

Share This Page