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

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

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