Difference in behavior when returning a local link or pointer

#include <iostream.h> using namespace std; class A { public: virtual char* func()=0; }; class B :public A { public: void show() { char * a; a = func(); cout << "The First Character of string is " << *a; } char * func(); }; char* B::func() { cout << "In B" << endl; char x[] = "String"; return x; } int main() { B b; b.show(); } 

The problem is that I keep the local varibale pointer / link. This is currently char x[]="String" , but when I use the pointer char *x="String" , the result is "S" , but when the Array reference to the output comes as (i)

+2
source share
4 answers

When you do something like:

 char *f(){ return "static string"; } 

You are returning the address of a string literal, but this string literal is not local to the function. Rather, it is statically allocated, so returning it gives clearly defined results (i.e., the String continues to exist after the function exits, so it works).

When you (try) to return the address of a char array as follows:

 char *f() { char x[] = "automatically allocated space"; return x; } 

The compiler allocates space for x on the stack, and then initializes it from a string literal that you do not have direct access to. What you are returning is a memory address on the stack, not a string literal, therefore, as soon as the function completes, this array ceases to exist, and you have no idea what else can be placed at that address. Attempting to use this address causes undefined behavior, which means that anything can happen.

+6
source

This is because when B::func() is allocated memory allocated for x[] , so if you try to access this memory location, you will get garbage values. But when you do char *x="String" , the memory for the string "String" is most likely allocated only once from the read-only section in your process memory. This address will remain valid until the execution of your program. In this case, if you try to access the pointer variable, it will work correctly. By the way, as a side note, you need to declare a virtual base class destructor.

+3
source

First of all, figure out how to send blocks of code. It’s not difficult, just set them aside for four spaces.

Secondly, you should never return a pointer to a local variable function. Since they are allocated on the stack, and all bets are disabled in relation to whether they are after the completion of the function.

EDIT: This was written when the code provided was blocked and incomplete. My point of view does not apply to this particular case, but it is still important to know.

0
source

To be more precise: B :: func () returns a pointer to the heap of memory that it used for the temporary array. There is absolutely no guarantee what is in this memory after the function returns. Either (1, the simplest good practice), the calling function can allocate memory and pass in the string buffer for the called function, or (2) the called function needs to be allocated a buffer, and the calling function needs to be freed later (but this requires a very disciplined approach by the developer of the calling function, and I would never want to rely on this) or (3, probably really best practice), the calling function can pass some kind of smart pointer that will go out of memory when it goes out of scope, and calling May function uses the smart pointer to point to the allocated memory.

0
source

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


All Articles