C ++ questions: pointers / memory addresses and subclasses

Why are we allowed to run this code:

int* FunctionB(int x) { int temp =30; //more code return &temp; } 

It seems to me that I am not returning what I said. Why can the memory address be returned if I declared the return type as a pointer. Is there something that points to a memory address, and not really a memory address?


 class Image : public BMP { public: void invertcolors(); void flipleft(); void adjustbrightness(int r,int g,int b); private: }; 

After compiling the previous code, I get this error:

  image.h: 3: error: expected class-name before '{' token 

but I thought I was using the correct syntax to declare a subclass. Is there something wrong with what I wrote?

+4
source share
4 answers

In the first case, the pointer is a memory address that points to some data, so your syntax is correct. In this case, however, you return the address of the local variable, so undefined is what it will refer to after the function call is completed.

In the second case, the compiler complains because it does not know what BMP . You may have forgotten the #include header file for the BMP class.

+4
source

Regarding your first question

Why are we allowed to run this code:

Nothing can stop you from returning the address of a local variable, but dangerous (mark my words). The address of the backward link or local variable reference causes Undefined Behavior (this means that anything can happen).

Also see this.

Why is it that the memory address can be returned if I declared the return type of the pointer?

This means that you need to learn the basics of pointers. In computer science, a pointer is a data type of a programming language whose value refers directly to (or "points to") another value stored elsewhere in the computer's memory using its address.

.. but I thought I was using the correct syntax to declare a subclass. Is there something wrong with what I wrote?

Have you identified a BMP or have you included a header containing its defnition?

+4
source

In C and C ++, the term address and term index are [almost] exact synonyms. In a practical, less formal language, the term address is most often used to denote the values โ€‹โ€‹of the rvalue pointer, while the term pointer can be seen for both rvalue and lvalue values. In the first example, you are returning an rvalue value, so the terms "address" and "pointer" can be used interchangeably. You are returning a pointer to an int object. You are returning the address of an int object. Both mean the same thing. Of course, returning the address of a local object makes no sense, except as a deliberate attempt to invoke undefined behavior.

Regarding the second question, the compiler just does not know what BMP . You have not stated this. In addition, the class must be defined before you can use it as a base in another class declaration.

+4
source

Think of it this way:

If a

 char myChar; 

declares a variable that stores a character,

 int* myPtr; 

declares a variable that stores the memory address.

Similarly, if

 char myCharFunction(); 

declares a function that returns a character,

 int* myPtrFunction(); 

declares a function that returns a memory address.

Therefore, since your function signature is int* FunctionB(int x) , it is correct when returning the memory address.

Important to understand here - int* means "memory address". And what we call a "pointer" is nothing more than a variable in which the memory address is stored. When someone says that the pointer "points to an address", they all mean that the pointer contains this address on its own (just as the char variable will contain a character).

0
source

Source: https://habr.com/ru/post/1299857/


All Articles