Memory allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafi Kfir

    #1

    Memory allocation

    HI,

    I would like to ask if the follwoing example is correct:

    char *Title=NULL;

    Title = "This is message 1";
    ----
    ----
    ---
    Title = This is another message";

    It works fine for me but I was advised to use memory allocation like
    malloc. Is it necessary? I don't seem to have problem with it...

    Thank you
  • Joona I Palaste

    #2
    Re: Memory allocation

    Rafi Kfir <rafi.kfir@telr ad.co.il> scribbled the following:[color=blue]
    > HI,
    >
    > I would like to ask if the follwoing example is correct:[/color]
    [color=blue]
    > char *Title=NULL;[/color]
    [color=blue]
    > Title = "This is message 1";
    > ----
    > ----
    > ---
    > Title = This is another message";[/color]
    [color=blue]
    > It works fine for me but I was advised to use memory allocation like
    > malloc. Is it necessary? I don't seem to have problem with it...[/color]

    If you only ever intend to read the contents of Title, this way works
    fine. But if you want to modify the string itself, instead of moving
    Title to point at another string, you need memory allocation like
    malloc.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "No, Maggie, not Aztec, Olmec! Ol-mec!"
    - Lisa Simpson

    Comment

    • Mike Wahler

      #3
      Re: Memory allocation


      "Rafi Kfir" <rafi.kfir@telr ad.co.il> wrote in message
      news:f74d6851.0 411040907.48dbc ebd@posting.goo gle.com...[color=blue]
      > HI,
      >
      > I would like to ask if the follwoing example is correct:
      >
      > char *Title=NULL;
      >
      > Title = "This is message 1";
      > ----
      > ----
      > ---
      > Title = This is another message";[/color]

      Once you fix the syntax error:

      Title = "This is another message";

      It's fine.

      [color=blue]
      >
      > It works fine for me but I was advised to use memory allocation like
      > malloc. Is it necessary?[/color]

      Not in this case. All you're doing is creating a pointer, then
      assigning it the addresses of literal strings. Just don't try
      to modify them (i.e. by writing via a dereference of the pointer
      'Title'). You can help protect against this if you define your
      pointer thus:

      char const *Title = NULL;

      Then if you try to write through this pointer, you should
      get a compiler error or warning.

      If you do need to modify what 'Title' points to, then change
      its definition to an array, e.g.:

      char Title[100];

      or allocate memory and assign
      its address to it.

      char *Title = malloc(100);

      If you allocate memory, don't forget to 'free()' it when
      you're done with it.

      -Mike


      Comment

      • Rafi Kfir

        #4
        Re: Memory allocation

        ">[color=blue]
        > or allocate memory and assign
        > its address to it.
        >
        > char *Title = malloc(100);
        >
        > If you allocate memory, don't forget to 'free()' it when
        > you're done with it.
        >
        > -Mike[/color]

        Thanks Mike,

        Do you still have to 'free()' it if it is inside a local procedure.
        Doesn't it get freed automatically where the procedure is returned?

        Thanks
        Rafi

        Comment

        • Joona I Palaste

          #5
          Re: Memory allocation

          Rafi Kfir <rafi.kfir@telr ad.co.il> scribbled the following:[color=blue][color=green]
          >> or allocate memory and assign
          >> its address to it.
          >>
          >> char *Title = malloc(100);
          >>
          >> If you allocate memory, don't forget to 'free()' it when
          >> you're done with it.[/color][/color]
          [color=blue]
          > Thanks Mike,[/color]
          [color=blue]
          > Do you still have to 'free()' it if it is inside a local procedure.[/color]

          Yes.
          [color=blue]
          > Doesn't it get freed automatically where the procedure is returned?[/color]

          No. The memory is still there, even if local pointer variables pointing
          to it have gone out of scope.
          It's the same as having this code:

          int getnumber() {
          int a = 1;
          return a;
          }

          and asking whether the return value can be used, because the number 1
          was freed when the function returned.

          --
          /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
          \-------------------------------------------------------- rules! --------/
          "And according to Occam's Toothbrush, we only need to optimise the most frequent
          instructions."
          - Teemu Kerola

          Comment

          • Mike Wahler

            #6
            Re: Memory allocation


            "Rafi Kfir" <rafi.kfir@telr ad.co.il> wrote in message
            news:f74d6851.0 411070056.68633 121@posting.goo gle.com...[color=blue]
            > ">[color=green]
            > > or allocate memory and assign
            > > its address to it.
            > >
            > > char *Title = malloc(100);
            > >
            > > If you allocate memory, don't forget to 'free()' it when
            > > you're done with it.
            > >
            > > -Mike[/color]
            >
            > Thanks Mike,
            >
            > Do you still have to 'free()' it if it is inside a local procedure.[/color]

            Yes.
            [color=blue]
            > Doesn't it get freed automatically where the procedure is returned?[/color]

            No. Memory allocated with 'malloc()', 'calloc()', or 'realloc()'
            remains allocated for the duration of the program's execution.
            Most operating systems will reclaim the memory upon termination
            of the program, but it's never a good idea to depend upon that.
            If you allocate it, you should free it.

            -Mike


            Comment

            Working...