1

I attach the following block of code

int main()
{
    char b;
    char *a = (char*)malloc(10);
    a="MeUni";
    b=*a;
    cout << b << endl;
    return 0;
}

Should I use a=NULL; after free(a);

free(a) deallocates the memory block, but does not delete the value that has been stored to this block.

Am I right?

KostasA
  • 5,204
  • 6
  • 23
  • 29
  • 1
    Rule of thumb: `free` what you obtained via `malloc` and friends, and `delete` what you have obtained via `new`. – Jabberwocky May 18 '17 at 13:23
  • 4
    Actually, using `free` in this case will lead to *undefined behavior* since `a` no longer points to memory returned by `malloc`. – Some programmer dude May 18 '17 at 13:23
  • Should I use `a=NULL; ` after `free(a);`. It's not necessary, but it can help. – Jabberwocky May 18 '17 at 13:23
  • 3
    `a="MeUni";` replaces the pointer that `malloc(10)` returned – Kevin May 18 '17 at 13:24
  • 5
    Simple answer: Don't use `malloc`/`free` or `new`/`delete`. Use `std::sting` for strings and `std::vector` for all other dynamic arrays. That puts memory management on the implementors instead of you. – NathanOliver May 18 '17 at 13:27
  • malloc() allocates the memory means that no other variable can use this block of code. Using free() means that I deallocate this block of code, but the value in that block remains! So another allocation of memory can use the precious block of memory and lead to an unexpected behavior. That why I think that I have to use a=NULL. But I don't understand why this is not necessary? – KostasA May 18 '17 at 13:27
  • If you use `free` after `a="MeUni"` you will get a dump. If you assign `NULL`, I don't know where you want to do this since I don't know where you want to free, then you will be safe since if you accidentally try to `free` again nothing will happen. – jiveturkey May 18 '17 at 13:30
  • 6
    This looks like another case of trial-and-error C++. That does not work, learn the language from a good book instead. – Baum mit Augen May 18 '17 at 13:30
  • 1
    @KostasA I don't know what you mean by allocating/deallocating blocks of code (I think you may mean blocks of memory?), but as long as you don't try to write to something after you `free()` it there's no need to explicitly set it to `NULL`. But fix the other issues I and others have pointed out first. – Kevin May 18 '17 at 13:31

2 Answers2

0

It would be a mistake to call free(a) after your program executes a="MeUni"; because at that point, a no longer points to the block of memory that the malloc(10) call allocated.

Also, a=NULL; will not "delete the value" of the block of memory that a points to.

I suspect that there are quite a few things wrong with your program, but it's hard to tell what they are, because it's hard to tell what you were hoping the program would do.

You seem to have a very weak idea of what pointers are and how to use them. If you want to learn C and/or C++, then you are going to need to work through some good tutorials, and some good books, and maybe take a class.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

Ohters have already commented on using malloc/free, not using std::string and this obviously being a toy program etc. and they're right; but to answer your question briefly:

  • If this would be a serious program, you'd have to think about where you would not need the allocated memory anymore, and indeed use free to release it. Certainly before return 0, possibly earlier. Not releasing it leaks memory.

  • Under the same assumption, it is not required to clear the pointer after free, but it's certainly a good idea, as it prevents accidental usage the memory location that's no longer yours to access.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
ecotax
  • 1,933
  • 17
  • 22