473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problem understanding returning a pointer and memory allocation

In the following code, I am trying to return a char[], a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at all is how variables are created in what type of memory and how they are deleted in the following three cases when calling and exiting test, test1 and test2 functions.

I also note that I see a different result while using char[] and char* which I thought were the same. I return char[] in test seems to be deleted once test is exited and char* in test1 seems to return a memory location and value which is same as that being accessed in main(). I always thought that the variables are deleted and including the pointers other than those created using new operator. Any help about basics of the memory allocation in these cases is appreciated.

#include<iostream>
using namespace std;

long int tmpAddStorage;

char* test2()
{
char* buf = new char[10];
buf = "abcdefgh";
cout << "In test1 using dyn mem allocation: (int *)buf" << (int *)buf << endl;
return buf;
}

char* test1()
{
char* buf = "abcdef"; // created in its temporary memory ??
cout << "In test1 using char* buf creation" << endl << " (int *)buf" << (int *)buf << endl;
//delete buf; // NO USE: delete must be followed by a poitner created using new
// and not pointer to a const, however, delete buf does not
// throw an error nor does it create any change
return buf; // same as return buf;
}

char* test()
{
char buf[] = "abcdef"; // created in its temporary memory
cout << "In test() using char buf[] creation" << "(int *)buf" << (int *)buf << endl;
return (char *)buf; // same as return buf;
// warning address of local variable buf is returned
}

int main() {
char* p ;

//***********************
p = test(); // I know that this call is useless. So I tried to modify the
// test() function to return the char * properly. I listed my
// solution down below. Please feel free to advise.
cout << "(int *)p " << (int *)p << endl;
// points to the same memory location but this time it is empty because
// test() has returned
cout << "p " << p << endl;
//***********************

//***********************
p = test1(); // returning a char* in test1()
cout << "(int *)p " << (int *)p << endl;
cout << "p " << p << endl;
//***********************

//***********************
p = test2(); // using dyanmic memory allocation in test1()
cout << "(int *)p " << (int *)p << endl;
cout << "p " << p << endl;
//***********************

//So can we modify something in a different function if we know its address?
// How can we store an address and modify the contents in its location?
// int a = 20;
// cout << &a << endl;
// tmpAddStorage = (int)&a;
// cout << hex << tmpAddStorage << endl;

delete p;
system("pause");
return 0;
}
Oct 4 '07 #1
1 1704
ilikepython
844 Expert 512MB
In the following code, I am trying to return a char[], a char* (a type of non-const without using new, what do we call this type of pointer?) and char* created using new operator. What I do not know at all is how variables are created in what type of memory and how they are deleted in the following three cases when calling and exiting test, test1 and test2 functions.

I also note that I see a different result while using char[] and char* which I thought were the same. I return char[] in test seems to be deleted once test is exited and char* in test1 seems to return a memory location and value which is same as that being accessed in main(). I always thought that the variables are deleted and including the pointers other than those created using new operator. Any help about basics of the memory allocation in these cases is appreciated.

#include<iostream>
using namespace std;

long int tmpAddStorage;

char* test2()
{
char* buf = new char[10];
buf = "abcdefgh";
cout << "In test1 using dyn mem allocation: (int *)buf" << (int *)buf << endl;
return buf;
}

char* test1()
{
char* buf = "abcdef"; // created in its temporary memory ??
cout << "In test1 using char* buf creation" << endl << " (int *)buf" << (int *)buf << endl;
//delete buf; // NO USE: delete must be followed by a poitner created using new
// and not pointer to a const, however, delete buf does not
// throw an error nor does it create any change
return buf; // same as return buf;
}

char* test()
{
char buf[] = "abcdef"; // created in its temporary memory
cout << "In test() using char buf[] creation" << "(int *)buf" << (int *)buf << endl;
return (char *)buf; // same as return buf;
// warning address of local variable buf is returned
}

int main() {
char* p ;

//***********************
p = test(); // I know that this call is useless. So I tried to modify the
// test() function to return the char * properly. I listed my
// solution down below. Please feel free to advise.
cout << "(int *)p " << (int *)p << endl;
// points to the same memory location but this time it is empty because
// test() has returned
cout << "p " << p << endl;
//***********************

//***********************
p = test1(); // returning a char* in test1()
cout << "(int *)p " << (int *)p << endl;
cout << "p " << p << endl;
//***********************

//***********************
p = test2(); // using dyanmic memory allocation in test1()
cout << "(int *)p " << (int *)p << endl;
cout << "p " << p << endl;
//***********************

//So can we modify something in a different function if we know its address?
// How can we store an address and modify the contents in its location?
// int a = 20;
// cout << &a << endl;
// tmpAddStorage = (int)&a;
// cout << hex << tmpAddStorage << endl;

delete p;
system("pause");
return 0;
}
In test2() you should use strcpy to assign:
Expand|Select|Wrap|Line Numbers
  1. strcpy(buf, "abcdefgh");
  2.  
In test1() I'm not sure, but I don't think you should do that.
In test() you are returning the address of a local variable so it will be destroyed when the function returns and the pointer will point to garbage. It is different from test2() because there you are allocating on the heap and it stays there until a delete call.

Maybe someone else can explain more.
Oct 4 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
6
by: Nick | last post by:
excuse me!! may i ask a simple problem here? if i dynamically allocate a memory(ex. new in C++ or malloc in C) in a sub-function and forget free the space end of the sub-function. does it...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
by: Brandon Langley | last post by:
I have this structure that I am using in conjunction with NetLocalGroupAddMembers: public struct LOCALGROUP_MEMBERS_INFO_3 { public string lgrmi3_domainandname; } I am having failures...
7
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.