pointers

hi all
I dont know why the third line in output is nothing.
allocate memory in func and assign it to main_ptr but when return from
func what happen to that memory?
i know we can not use temp variable outside func but what happened to memory allocation using new in func?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;
void func(char *);
int main()
{
    char * main_ptr;
    func(main_ptr);
    cout<<"main_ptr is: "<<main_ptr<<endl;
    return 0;
}
void func(char * main_ptr)
{

     char *temp=new char [100];
    temp[0]='a';temp[1]='b';temp[2]=0;
    main_ptr=temp;
    cout<<"temp is : "<<temp<<endl;
    cout<<"main_ptr in func is : "<<main_ptr<<endl;
}



temp is : ab
main_ptr in func is : ab
main_ptr is :
Your function leaks.
You are modifying a copy of the pointer.
Topic archived. No new replies allowed.