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

Pages: 1-4041-8081-

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.

Name: Anonymous 2011-01-22 6:29

>>17
Says who?

Name: Anonymous 2011-01-22 6:38

>>41
In C, it's redundant, because void* can be implicitly casted to any type. It's like an Any, an Object, an object. It's sepples that sucks so much that even the ``Any'' type must be casted.

Name: Anonymous 2011-01-22 6:52

>>42
You do realize the difference between upcasting and downcasting and its implications?

Name: Anonymous 2011-01-22 7:00

>>43
In C, the only thing you need to know is:
You need to cast void* only when you have to dereference it.
You don't dereference void*.
When you need to dereference void*, you assign the pointer to another pointer with the correct type instead of working on the casted void*.

Name: Anonymous 2011-01-22 7:39

>>44
That's a special case in C, instated due to the frequent need to pass around untyped memory addresses.

That is not the case in high-level languages, and there is no direct parallel. In particular, the object base class is normally not implicitly convertible to any class.

C++ tossed it due to delusions of high-levelness, to discourage C-style code and interfaces.

Name: Anonymous 2011-01-22 8:29

>>45
C++ tossed it due to delusions of high-levelness
I chortled.

Name: Anonymous 2011-01-22 8:48

>>45
SAY THAT TO MY DUCK!

Name: Anonymous 2011-01-22 9:55


#include <stdlib.h>

int* b;

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


or


#include <stdlib.h>

int** b;

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


will work.

also depending on compiler, will require casting a void*

Name: Anonymous 2011-01-22 9:56

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

Name: Anonymous 2011-01-22 9:58

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

Name: Anonymous 2011-01-22 10:11

>>50
int*b;main(){free(b=malloc(sizeof(*b));return 0;}

Name: Anonymous 2011-01-22 10:19

>>51
main(){int b;return 0;}

Name: Anonymous 2011-01-22 10:19

>>52
main(){return 0;}

Name: Anonymous 2011-01-22 10:21

>>51

_start: xor eax, eax
        ret

Name: Anonymous 2011-01-22 10:23

>>54
_start:xor eax,eax
ret

Name: Anonymous 2011-01-22 10:24

>>55
_start:mov eax,0
ret

Name: Anonymous 2011-01-22 10:35

there is no memory leak OP. a and b points to the same memory. once you free one of them that memory location is unallocated and both pointers are invalid.

Name: Anonymous 2011-01-22 10:37

>>57
DQN QUALITY

Name: Anonymous 2011-01-22 10:53

>>55
xor eax,eax
>>56
mov eax,0

OMG UNOPTIMISED

Name: Anonymous 2011-01-22 11:43

>>59
It takes less time for the compiler to parse it.

Name: Anonymous 2011-01-22 11:46

>>60
xor is faster.

Name: Anonymous 2011-01-22 12:05

>>61
benchmarks or gtfo

Name: Anonymous 2011-01-22 12:28

Should use C99-restrict keyword to avoid situations like that.

Name: Anonymous 2011-01-22 12:50

>>62
The ``xorreg,reg'' instruction compiles to fewer bytes than ``movreg,const''.

Name: Anonymous 2011-01-22 13:14

>>64
CPUs nowadays have a large instruction cache anyway. What's yo'ure point?

Name: Anonymous 2011-01-22 14:11

>>65
xor reg,reg executes faster than mov reg,const

Name: Anonymous 2011-01-22 14:13

>>65
Oh, this bullshit again. Let me fix that for you:
CPUs nowadays are largely instruction-fetch bound

Name: Anonymous 2011-01-22 14:36

>>67
Well, memory bound, at least.
A Core 2 Duo has 32KiB icache and 32KiB L1 data cache.

Name: Anonymous 2011-01-22 15:36

>>66
No it do'esnt, you fucking cretin. Post some benchmarks or shut the fuck up.

Name: Anonymous 2011-01-22 15:55

>>69
TOO BAD mov reg, const IS SLOW AS FUCK

Name: Anonymous 2011-01-22 17:35

Sure, why not.
[m]$ time test.xor

real    0m2.745s
user    0m2.744s
sys    0m0.001s


$ time test.mov

real    0m10.893s
user    0m10.886s
sys    0m0.000s[/m]
Q.E.D., bitches.

Name: Anonymous 2011-01-22 17:37

>>72
And where's the source code to test.xor and test.mov ?

Name: Anonymous 2011-01-22 18:10

>>73
On my hard drive, duh.

Name: Anonymous 2011-01-22 19:54

>>74
EPIC WIN OP

Name: Anonymous 2011-01-22 20:12

I'm just being cute and tsundere. Here you are. Now please remember the frightening power of cache, and try to stay on its good side.
http://pastebin.com/3cVBAqhK
http://pastebin.com/AJSYYC65

Name: Anonymous 2011-01-22 20:14

>>75
lulz

Name: Anonymous 2011-01-22 20:19

>>76
Oh god, I lol'd so much. Thank you.

Name: Anonymous 2011-01-22 21:56

>>76
Oh god, so nice. God bless.

Name: Anonymous 2011-01-22 22:14

>>78-79
Not this shit again.

Name: Anonymous 2011-01-22 22:39

>>33
When coding C++ or C on Windowz in Visual Studio if you free a NULL pointer it seg faults (at least in my experience). Windows <3

Name: Anonymous 2011-01-23 8:18

>>81
No. Nothing happens if you do.

Name: Anonymous 2011-01-23 11:37

>>81
Are seg faults good?

Name: Anonymous 2011-01-23 13:05

Protip: What do you think free looks like?
It already takes a copy of the pointer in the argument passed it, so of course you can assign pointers back and forth between how many variables you want.

Name: Anonymous 2011-01-23 13:08

>>84
In a dumb memory allocator, it just sets the "free" bit of the header of the pointer to true.

Name: Anonymous 2011-01-23 13:56

>>85
nobody does that, come on.

Name: Anonymous 2011-01-23 21:53

sizeof(char) == '1'
0

Name: Anonymous 2011-01-23 21:59

>>87 see >>86
now go away

Name: Anonymous 2011-01-23 21:59

>>87
'1'
IHBT

Name: Anonymous 2011-01-23 22:05

>>87

(imply (>implying (compiler-sizeof 'char) is #\1) being true and do (>implying displayln gets applied to "EPIC WIN OP!" and (current-output-port)) otherwise (>implying displayln gets applied to "Fuck off, >>87"))


`>imply COMBINATOR

Name: Anonymous 2011-01-23 22:06

>>81
I'm sorry Windows isn't standards-conforming.
Look at the bright side, at least you aren't using GNU. :)

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