Pointer variables can t be directly assigned address values, right?

Updated on technology 2024-03-01
9 answers
  1. Anonymous users2024-02-06

    You can assign a value, but this action is very obscene... Hehe...

    1.If p has been assigned address a, the original address a will become a dead address after the assignment, because p is assigned but p is not released, and it is not used, so that a will not be used when assigning addresses to other pointers, and the space pointed to by the a address becomes a leaked space and is suspended... If this program is executed in a loop, it will cause the memory to run out and the system will crash...

    2.If you newly assign the address b to p, if you operate on p in the future, you will operate on the address that b points to. If b is blank and unused, it's nothing...

    But in case B stores important data, and you do a write operation on the content pointed to by P, then the content in the B address will be modified, which will seriously cause the system to be paralyzed...

    So assigning a value to a pointer is a very dangerous action, be careful!

  2. Anonymous users2024-02-05

    You can assign a value directly, as long as you know exactly that his address is correct.

  3. Anonymous users2024-02-04

    char a[5] = ;

    char*p = a;

    Yes, what do you mean by direct address?

    Yes, but it's dangerous, you'll change the contents of other variables.

    Here's how, it runs correctly:

    #include

    using namespace std;

    int main()

    char a[5] = ;

    char*p;

    p = (char*)0x0012fecd;

    cout <

  4. Anonymous users2024-02-03

    You can assign a value directly, but the consequences can be severe.

    #include

    main()

    int *p;

    p=0x1245052;

    printf("%d",p);

    This is possible. But.

    printf("%d",p);

    Become. printf("%d",*p);

    Strange errors may occur.

  5. Anonymous users2024-02-02

    For secure access.

    When we define a variable and access it, such as int a; a=10;The compiler helps us allocate addresses and space (length) in memory, and when accessing (using) we only need to write its name in the source **, we don't need to care what the specific address is, and the compiler will automatically help us convert to a specific address in the target ** when compiling.

    For example, when the compiler determines that the specific address is 1000 after allocating memory for the variable (object), then all encounters a in the current namespace will be automatically converted to 1000, and all operations on a are replaced by operations on address 1000, and because the variable type has been determined at the time of definition, the compiler also knows how to access the address, such as length and data encoding (int is 4 bytes, positive numbers are original code, negative numbers are complement).

    Knowing the above details, it is more dangerous to manipulate data directly with addresses (pointers) than with variable names, because it requires the programmer to be very clear that everything they do when using pointers must be "exactly right", and when using pointer access, the compiler is leaving some of the error checking work to the programmer (which is the cost of pointer convenience and efficiency).

    Therefore, it is a simple and good security habit to develop an initialization pointer, and you must initialize null before the pointer does not obtain the correct address for the time being, and you must also develop the habit of judging before accessing before using the pointer, such as if (p), don't be afraid of troublesome if type. And if the pointer is not assigned to the correct address, the address in the pointer variable is random, perhaps a remnant of the previous temporary variable or program, and these values are indeterminate, when this value happens to be a sensitive address of the system, your program is likely to access the wrong address, and in most cases will cause the program to crash or even the system crash.

    When your program is large enough to reach a certain size, if there is an error in a pointer access somewhere, it is very difficult to find the error, so big that you will also crash.

  6. Anonymous users2024-02-01

    The addition of * is to clarify that this is a pointer variable, otherwise it will be confused.

    For example, defining an int variable is.

    int a;

    If you define a pointer to the int type, too.

    int p;

    So how can you tell if p is a pointer to an int and not an int? Add * to identify it (in fact, it can also be set to something else, such as int pointer p; Indicates that p is a pointer to the int type, but can five letters of point be more convenient than *? )

    int *p;Explain that p is a pointer to the int type.

    You may be confused as to why you should specify what type of pointer the definition is pointing to, and not all pointers should be defined the same way, for example.

    pointer p;

    This is because the pointer not only stores the address, but also the method of "how to read the data", otherwise reading the data will be incorrect. For example, if we have an address 28790 stored in the pointer, should we start reading one byte from 27890, or four bytes, or eight bytes? We only know that the "start" of the address is 27890, and if we don't know where the "end" of the address that this pointer points to, we can't read the data accurately.

    If we give the pointer "what type it points to", then we can read the data accurately, such as char *p; Suppose the address stored in p is 27890, then since p is of char *, we know that we have to read one byte from 27890, and if p is int *, then we will read four bytes.

    That's why the definition of a pointer requires an * symbol.

  7. Anonymous users2024-01-31

    Actually, you're asking a question about lvalues and rvalues.

    The variable to the left of the assignment number is called the lvalue variable, and the variable to the right of the assignment number is called the rvalue variable.

    The lvalue variable takes its address, and the rvalue variable takes its value (content).

    You can think of a variable as an object, and any variable has 2 properties, one is an address and one is a numeric value. The address corresponds to a piece of memory space in which numerical values (contents) are stored.

    Therefore, when C++ executes an assignment statement, it first takes the lvalue (the address of the variable on the left) and then the value of the rvalue (the value of the variable on the right), and then executes the assignment operation, that is, the rvalue is stored in the lvalue.

    The operation to set the value does not affect the original value. So answer 1 is the address of a, answer 2 is still 7

  8. Anonymous users2024-01-30

    Or the address of A.

    A has a value, which is 7

  9. Anonymous users2024-01-29

    Although B is the correct answer, the understanding of CD is incorrect.

    a, *p is a memory assignment with an address p, because p has not yet been assigned, this behavior is dangerous b, the first address of the array can be understood as a constant pointer (i.e., a needle that points to an address that cannot be changed), although the pointer cannot be modified, but the content of the address it points to can be changed.

    is a constant, and its address cannot be modified.

    d。The error lies in the initialization of reference a, which should use the same type (int &a = b).

    In fact, the complete pointer can be understood as an integer (for the common one is a 32-bit unsigned integer), but its value has a clear meaning (representing a memory space), so its value cannot be arbitrary (limited by the range of accessible addresses), from this point of view, it is easy to understand why the pointer addition is meaningless (will be out of the access range), and the difference is meaningful (how much memory space is separated between two pointers, but it makes sense to pay attention to the large address to reduce the address).

Related questions
11 answers2024-03-01

Hello landlord, what is the delivery method? Give it to someone else or to yourself? If it's for someone else, you just ask a question with a high reward score, and then the title is written to send points, when someone comes, then you are someone else, but such a question will definitely be reported by someone to brush points, not feasible, if it is given to yourself, then first ask a high score question yourself, and then use another one, and finally for the answer, then it's okay, but this is also cheating, not feasible.

5 answers2024-03-01

Relevance means that the accounting information provided by the enterprise should be relevant to the economic decision-making needs of the users of the financial statements, and help the users of the financial statements to evaluate the past, present or future situation of the enterprise. >>>More

12 answers2024-03-01

It's not a question of whether it can be broken or not.

Do you ask a question and can't see the answer, or someone else's question can't get the answer. >>>More

10 answers2024-03-01

Does it matter whether the domain name can be opened? It must have something to do with the browser. >>>More

13 answers2024-03-01

Although the sunlight is much weaker than usual during a solar eclipse, it can still damage the eyes and damage the cornea if you look at it directly.