Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

malloc() and free()

Name: Anonymous 2011-01-18 22:44

#include <stdlib.h>

int * b;

int main(void)
{
    int * a = NULL;
    a = (int *) malloc(sizeof(int));
    b = &a;
    free(b);
    return 0;
}


Would this result in a memory leak? If yes, why?

Name: Anonymous 2011-01-18 22:49

Why isn't someone who answers "no" asked "why?"

Name: Anonymous 2011-01-18 22:54

>>2
#include <stdbool.h>
#include <stdio.h>
int main(void) {
    bool yes = true, no = false;
    if (yes || no) printf("Why?");
    return 0;
}


Happy now?

Name: Anonymous 2011-01-18 23:36

No, it won't. Because it does what you expect.

Name: Anonymous 2011-01-18 23:50

>>4
Thanks.

Name: Anonymous 2011-01-19 1:02

b = &a;
Shouldn't this be: b = a;?

Name: >>4 2011-01-19 1:23

>>6
Aw fuck, you're right.

Name: Anonymous 2011-01-19 2:43

>>6
>>1

Shouldn't it also be: if(b == NULL) free(b);

That's what I'd do mai niggas. Keep it real yo.

Name: Anonymous 2011-01-19 2:44

>>8
haha I meant !=, I swear

Name: Anonymous 2011-01-19 3:19

>>9
No. Fuck you. free(NULL) is no-op on any decent compiler.

Name: Anonymous 2011-01-19 4:29

>>8,9,10
Please do not look at 4chan's /b/ as an example of what anonymous boards are like!

Name: Anonymous 2011-01-19 4:49

>>11
Your assumption tha I'm visiting /b/ is wrong.

Name: Anonymous 2011-01-19 5:22

>>11
You should optimise your reply by using a range.
>>8-10

Name: Anonymous 2011-01-19 5:48

>>13
-O3 -froll-quote

Name: Anonymous 2011-01-19 8:32

    a =(int *) malloc(sizeof(int));
IHBT

Name: Anonymous 2011-01-19 9:53

>>15
Why.

Name: Anonymous 2011-01-19 9:55

>>16
casting void* considered harmful.

Name: Anonymous 2011-01-19 10:06

(define-syntax >implying
  (syntax-rules (that is called with)
    ((>implying proc is called with vars ... and var)
     (curry proc vars ... var))))

((>implying + is called with 2 3 and 4) 5) ; 14

Name: Anonymous 2011-01-19 10:13

>>17
How are you supposed to allocate memory for an int then?

Name: Anonymous 2011-01-19 10:22

>>19
Terrible!

Name: Anonymous 2011-01-19 12:32

#include <stdlib.h>

int * b;

int main(void)
{
    int * a = NULL;
    a = (int *) malloc(sizeof(int));
    b = a;
    free(b);
    return 0;
}


I still don't trust this code. Can someone elaborate.

Name: Anonymous 2011-01-19 12:40

>>21
To be or not to be.

Name: Anonymous 2011-01-19 13:41

>>21
int * a = NULL
a = (int *) malloc(sizeof(int));

5/10

Name: Anonymous 2011-01-19 16:04

>>15,17
Javafag detected
This is the way things work in C, dickbag.

>>1
It will work depending on your compiler implementation.  Every C compiler that I know of manages the heap by address.  So free() doesn't care what variable name you pass to it, only what address it points to.

Name: Anonymous 2011-01-19 16:08

>>24
So is it b = a or b = &a?

Name: Anonymous 2011-01-19 16:09

>>24
Oops, I meant to refer to >>21, no >>1>>1 is broken because you are trying to free a pointer to a stack variable, which >>21 fixed.

Name: Anonymous 2011-01-19 16:23

>>24
Javafag detected
This is the way things work in C, dickbag.

What the hell is this bullshit in my /prog/.
In C you don't cast void* away, it's C++ that bitches about it.
Also,
(.+)fag detected
Go back to the imageboards.
IHSBT

Name: Fuck off, !Ep8pui8Vw2 2011-01-19 19:07

>>27
Fuck off you cock sucking faggot.

Name: Anonymous 2011-01-19 19:13

>>1
It will not result in a memory leak. Why?
Because the OS will free it right after the execution anyway.

Name: Anonymous 2011-01-19 19:44

>>29
I'm running Ubuntu 9.10, so I don't think so Tim.

Name: Anonymous 2011-01-19 20:24

>>30
Once a process has ended, the kernel frees all the memory it allocated for it and that includes heap memory. Now go learn about memory management before making random assertions about the operating system you've been using for two weeks.

Name: Anonymous 2011-01-19 20:28

>>31
I thought memory is only free'd after the system is restarted.

Name: Anonymous 2011-01-19 20:31

>>10

Has nothing to do with the compiler, ANSI requires that free(NULL) is a valid no-operation call. Any conforming libc must be written this way.

Name: Anonymous 2011-01-19 23:23

>>32
:D only on the Java VM.

Name: Anonymous 2011-01-20 11:32

>>32

Why would you think that, do you think the people developing Linux are retards?

Name: Anonymous 2011-01-20 21:34

>>32
3/10

Name: Anonymous 2011-01-20 21:44

>>35
He's on a Windows box, clearly.

Name: Anonymous 2011-01-21 5:47

>>37

He said he was using Ubuntu 9.10.

Name: Anonymous 2011-01-21 11:52

>>31
So does that mean there are no memory leaks on linux?

Name: Anonymous 2011-01-21 14:58

>>39
0/10

Now fuck off.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List