Frage zu Zeiger

Gast #5037526
Lesenswert?

Autor: A. K. (prx) schrieb:
Ausnahme: Gerät oder Mikrocontroller deren I/O an
ebensolcher Adresse sitzt.


Aaah! Jetzt kommen wir der Sache näher. In dem Original-Beispiel des 
Autors wird nämlich für ein int nur eine Größe von 2 Bytes angegeben. 
Dann passt das zusammen...
Dann ist das hier wohl ein Sonderfall...
Gast #5037664
Lesenswert?

So the point is that, when you say some value 1000, it is treated as an 
integral constant in C language standards.

When you say int *ptr;

ptr is fully qullified to hold some memory location XXXX such that 
consecutive 4 bytes(as a typical gcc compiler supports) from XXXX 
represents a reserved memory location for an integral variable.

Now when you say (int *)XXXX you want to typecast the value XXXX which 
is currently of integer type to be treated as of address type, and so 
when you say

int *ptr  = (int *) 1000; you are declaring a pointer type variable 
which is allowed to hold a memory location and the correct way to get 
memory location is to have some symbol which is representing a memory 
location and then get the location that symbol is representing by using 
& operator with that, i.e. int *ptr = &a;

The code you have wrote tries to treat 1000 as a memory location value 
for an integral variable and puts it into pointer variable ptr. Although 
for security reasons it must not be allowed, but still, these are some 
security threats which hackers exploit m in C language constructs.
Gast #5038501
Lesenswert?

timertick_t schrieb:
> Für mich liest sich dieses Beispiel als ob man einem Zeiger eine Adresse
> zuweist. Ist das nicht böse?
> int *ptr=(int *)1000;

Für mich liest sich das wie eine der vielen C-Schreiber-Schludrigkeiten. 
Na klar, man kann sowas schreiben, der Compiler hat gewöhnlich nix 
dagegen. Aber es liest sich schlecht. Also trenne einfach die 
Typdefinition von der Variablendeklaration und diese von der 
Wertzuweisung. Dann wird es verständlicher und lesbarer.

Ach ja.. überlege dir mal als kleine Denksportaufgabe, wo ptr und wo 
deine 1000 vom Compiler hingepackt werden. (Denk-Alternative:  const int 
Tausend = ...)

W.S.
Gast #5039479
Lesenswert?

Eric B. schrieb:
> Aha, also ein Pointer ist auf dieser Architektur 4 Bytes gross.
Nein!
Das der neue Wert 1004 ist liegt daran, weil der Datentyp auf den der 
Zeiger zeigt (auf dieser Architektur) 4 Byte gross ist, nicht weil 
der Zeiger 4 Byte gross ist.

Das ist ein fundamentaler unterschied.
1
#include <stdio.h>
2
#include <stdint.h>
3

4
int main()
5
{
6
    char        *p_char  = (char *) 1000;
7
    int         *p_int   = (int *) 1000;
8
    uint64_t    *p_ui64  = (uint64_t *) 1000;
9
    __uint128_t *p_ui128 = (__uint128_t *) 1000;;
10

11
    printf("Init Value of p_char : %u\n", p_char);
12
    printf("Init Value of p_int : %u\n", p_int);
13
    printf("Init Value of p_ui64 : %u\n", p_ui64);
14
    printf("Init Value of p_ui128 : %u\n\n", p_ui128);
15

16
    p_char++;
17
    p_int++;
18
    p_ui64++;
19
    p_ui128++;
20

21
    printf("New Value of p_char : %u\n", p_char);
22
    printf("Size of datatype char : %u\n", sizeof(char));
23
    printf("Size of p_char : %u\n\n", sizeof(p_char));
24

25
    printf("New Value of p_int : %u\n", p_int);
26
    printf("Size of datatype int : %u\n", sizeof(int));
27
    printf("Size of p_int : %u\n\n", sizeof(p_int));
28

29
    printf("New Value of p_ui64 : %u\n", p_ui64);
30
    printf("Size of datatype uint64_t : %u\n", sizeof(uint64_t));
31
    printf("Size of p_ui64 : %u\n\n", sizeof(p_ui64));
32

33
    printf("New Value of p_ui128 : %u\n", p_ui128);
34
    printf("Size of datatype __uint128_t : %u\n", sizeof(__uint128_t));
35
    printf("Size of p_ui128 : %u\n\n", sizeof(p_ui128));
36

37

38
    return 0;
39
}
1
Init Value of p_char : 1000
2
Init Value of p_int : 1000
3
Init Value of p_ui64 : 1000
4
Init Value of p_ui128 : 1000
5

6
New Value of p_char : 1001
7
Size of datatype char : 1
8
Size of p_char : 8
9

10
New Value of p_int : 1004
11
Size of datatype int : 4
12
Size of p_int : 8
13

14
New Value of p_ui64 : 1008
15
Size of datatype uint64_t : 8
16
Size of p_ui64 : 8
17

18
New Value of p_ui128 : 1016
19
Size of datatype __uint128_t : 16
20
Size of p_ui128 : 8

Wie du siehst: Die groesse des Zeigers hat nichts mit dem neuen Wert 
von ptr zu tun. Dafuer ist ausschliesslich die Groesse des Datentyps, 
auf den gezeigt wird, verantwortlich.
#5039514
Lesenswert?

Kaj schrieb:
> Eric B. schrieb:
>> Aha, also ein Pointer ist auf dieser Architektur 4 Bytes gross.
> Nein!
> Das der neue Wert 1004 ist liegt daran, weil der Datentyp auf den der
> Zeiger zeigt (auf dieser Architektur) 4 Byte gross ist, nicht weil
> der Zeiger 4 Byte gross ist.

Ah stimmt, haste Recht. Hab da wohl zu schnell geschossen, zu wenig 
Kaffee getrunken :-)
Gast #5039540
Lesenswert?

A. K. schrieb:
> Komisch schrieb:
>> Öhmmmm, wofür braucht man einen Pointer, wenn man ihm keine Adresse
>> zuweist?
>
> Hier geht es im eine Adresse in Form einer gecasteten Zahl.
> Üblicherweise wird die Adresse eines Objektes zugewiesen.

Und wenn das Objekt ein Register ist? Was meinst, was wohl im Header 
file steht, ein #define mit einer Zahl vielleicht?

Du bist mir ein Schlaumeier. ?

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren