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

Pages: 1-

Memory allocation in C

Name: Anonymous 2011-09-06 9:12

I'm confused about how memory allocation works in C.
Consider the following code.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int a[5];
    int *b;
    b = malloc(sizeof(char) * 5);
    printf("a = %lu b = %lu", sizeof(a), sizeof(b));
    free (b);
    return 0;
}


It returns sizeof(a) to be the actual size of the array a that I allocated. I thought C didn't keep any record of the size of arrays which was the reason why strings have to be null terminated so what's up with that?
On the other hand sizeof(b) returns just the size of the pointer not the size of the array. When I do free(b) how does it know how much memory to deallocate?

Name: Anonymous 2011-09-06 9:14

*meant to allocate sizeof(int) but the question is still the same.

Name: Anonymous 2011-09-06 9:20

malloc stores the size of the block you allocated, obviously.

Name: Anonymous 2011-09-06 9:41

Read K&R2. It contains example implementations of malloc.

Name: Anonymous 2011-09-06 10:27

Malloc is systems way of stealing your memory and offering it in bits. Define a huge static array and write your own routine which will allow full control over it.

Name: Anonymous 2011-09-06 10:38

>>5
Hello, data-oriented-design faggot. I'd like to thank you a lot for helping me prematurely optimize my Danmakufu clone.

Name: Anonymous 2011-09-06 11:19

>>1
sizeof(a) gets the size of the whole array because the compiler initialized the array with 5 integers - it "knows" how big 'a' is. sizeof(b) gets the size of the pointer because the compiler doesn't know that 'b' is an array. The compiler doesn't know anything about the malloc() function, that's all down to the C library implementation you use.

malloc(), free() and realloc() know how big b is either because the C library implementation stores it somewhere or because the operating system keeps track of a process' allocated memory. I don't know which but I think it's the latter.

Name: Anonymous 2011-09-06 12:05

>>7
sizeof(a) gets the size of the whole array because the compiler initialized the array with 5 integers

Incorrect. You create an array of objects.

sizeof(b) gets the size of the pointer because the compiler doesn't know that 'b' is an array.

Again, that is incorrect you fucking little nigger. And I cite C8999

Under section 6.3.2.1 Lvalues, arrays, and function designators

"an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue."

Now you annoying little stupid fuck, where do you say anything about "an array of integers" in the standard? Where do you see anything that says or implies that sizeof() must get the size of the whole array?

I don't see anything that implies this. Now go clean another toilet you fucking dumb jew.

Name: Anonymous 2011-09-06 12:09

>>7
Actually, here is the entire passage from the C standard you fucking dumbass jew...

"Except when it is the operand of the sizeof operator or the unary & operator, or is the string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue."

Name: Anonymous 2011-09-06 12:10

>>8
Ok, ok, I'm sorry, just don't hurt me!

Name: Anonymous 2011-09-06 12:33

>>6
well that's just the thing about data-oriented design optimizations: they MUST be premature.

Name: Anonymous 2011-09-06 12:35

>>8
gcc and microsoft compilers sizeof() operators both return the size of the whole array.

standards mean nothing if nobody follows them, I guess.

Name: Anonymous 2011-09-06 13:41

>>5
Malloc is systems way of stealing your memory and offering it in bits.
Malloc is institutionalized theft! Malloc is unconstitutional! Ron Paul 2012!

>>12
>>8,9 is high or drunk or an idiot. I'm not even sure what he is trying to say.

Don't worry, sizeof returns the size of an array when passed an array and the size of a pointer when passed a pointer, the standard says it clearly. The two somewhat confusing parts about this might be that, first, you should understand clearly that value of sizeof operator must be determined at compile time so it operates on the type of the expression, not on the type of its runtime value, second and somewhat related, you should understand clearly that it's not a function but a special operator, and as such it can distinguish between arrays and pointers (which ordinary functions can't).

Name: Anonymous 2011-09-06 13:44

>>8-9
fuck you nazi faggot

Name: Anonymous 2011-09-06 14:36

>>8
dumb jew
Jews are on average more intelligent than whites.
Is a dumb jew of average intelligence for whites?

Name: Anonymous 2011-09-06 14:54

>>15
Fuck off you autistic nigger

Name: Anonymous 2011-09-06 15:33

>>16
what do you have against autistic niggers?

Name: Anonymous 2011-09-06 16:47

>>17
this!

*grabs dick*

Name: Anonymous 2011-09-06 17:15

sizeof(a)

Name: Anonymous 2011-09-06 17:21

>>15
This might surprise you, but I invented the ``hating on Jews'' meme.

I feel kind of bad about it :(

Name: Anonymous 2011-09-06 17:36

>>20
Are you jewish? Are you suave? Are you a suave jew?

Name: Anonymous 2011-09-06 19:12

sizeof(MUHDICK);

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