C Language: Why do dynamically allocated objects return a pointer while statically allocated objects give you a choice?

This is actually a much more concise, much more understandable question than the one I asked here earlier (for anyone who cares): C Language: Why does malloc () return a pointer, not a value? (Sorry for those who initially thought I was spam ... I hope this will not be construed as the same question, because I think that, as I put it, he made it unintentionally misleading)

-> Basically, I'm trying to ask: why does a C programmer need a pointer to a dynamically allocated variable / object? (no matter what the difference between the variable / object ...)

If a C programmer can only create "int x" or just "int * x" (both statically allocated), then why doesn't he also have the ability to simply initialize his dynamically allocated variable / object as a variable (and NOT returning the pointer via malloc () )?

* If there are some obscure ways to do what I explained above, then, well, why does malloc () seem like the way most textbooks deal with dynamic allocation?

+5
source share
7 answers

Note: below byterefers tosizeof(char)

, , malloc a void *. : C . C ; , , . void * , .

malloc : , . ++ operator new, , , ( - , , ).

, malloc , ( malloc: ). , , : . , , malloc(sizeof(int)), int, sizeof(int) . int, sizeof(int) char s.

(calloc, realloc) (calloc , , realloc , ).

+8

, . . .

, malloc, , . . .

+1

, , malloc(), , - .

int x;, x, . x 10, x = 10;.

-, int *p = &x;, x *p = 10;. , x, ( , ).

malloc(sizeof(int)), , . , . , , -, , : int *p = malloc(sizeof(int));, *p = 10;.

: ", malloc, ?" - - malloc(sizeof(int), "x"). :

  • -, C . ;
  • -, : , C , , : int x;.
+1

, "int x" "int * x". int; - - .

" " , ( , , , ), . , - :

int x = malloc(sizeof(int)); *x = 0;

:

int x = 0;
0

. , int x , malloc (sizeof (int)) . . , . , , . , .

, int x , malloc (sizeof (int)) . . . , , .

0
  • . "int x" - - -, - , , .

  • ... . , - . , . . , .

  • C , . . , , (, ); , . , ; , .

, , ++. "MyInt x = 42;" - .

0

, :

C int x int *x ( )

. .data .bss ( static , ).

- . *x=1, SIGSEGV, - . C :

$ cat stack.c
#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;
    int j;
    int k;
    int *l;
    int *m;
    int *n;
    printf("i: %d\n", i);
    printf("j: %d\n", j);
    printf("k: %d\n", k);
    printf("l: %p\n", l);
    printf("m: %p\n", m);
    printf("n: %p\n", n);
    return 0;
}
$ make stack
cc     stack.c   -o stack
$ ./stack
i: 0
j: 0
k: 32767
l: 0x400410
m: (nil)
n: 0x4005a0

l n - - , , . - , , , . , .

m , NULL - , , , .

. . .

L - , - , C: R- ( ) L- ( ) .

, :

int a;
a = 3;

3 , a.

:

int b;
b = a;

, , a, , b.

:

int *ap;
ap=malloc(sizeof int);
*ap=3;

ap= ap. ap . *ap= . ap ; , ap, .

When you later use the pointer, you can choose which of the two values ​​associated with the pointer used: either the actual contents of the pointer, or the value that the pointer points to:

int *bp;
bp = ap; /* bp points to the same memory cell as ap */

int *bp;
bp = malloc(sizeof int);
*bp = *ap; /* bp points to new memory and we copy
              the value pointed to by ap into the
              memory pointed to by bp */

I found the assembly much easier than C for many years because I found the difference between foo = malloc();and *foo = value;confusing. I hope I found what baffled you, and seriously hope that I will not make it worse.

0
source

All Articles