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-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.

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