So, I'm dynamically allocating a char array in a function, then returning that array to an un-allocated char array in main().
Code:

#include<iostream>

using namespace std;

char* cc(char*,char*);

int main(){

	char* strMain;
	char* str1="Well, hello there, ";
	char* str2="How are you?";
	
	strMain=cc(str1,str2);
	
	cout<< strMain;
	
}
	
char* cc(char* str1,char*str2){

	char* retVal;
	int i=0,len=0;
	
	while(str1[len]!='\0'){
		len++;
	}
	while(str2[i]!='\0'){
		len++;
		i++;
	}
	retVal=new char[len];
	i=0;
	while(str1[i]!='\0'){
		retVal[i]=str1[i];
		i++;
	}
	len=0;
	while(str2[len]!='\0'){
		retVal[i]=str2[len];
		len++;
		i++;
	}
	
	return retVal;
	
}

will strMain become the reference for the block of memory allocated in the cc function as retVal?
If I delete[] strMain, will that be the same as freeing the memory I allocated in cc()?


Oh, second question,
if I go

char* variable="hello world!";
char* variable2;
variable2=new char[13];
variable2="hello world!";

what's the difference between how variable and variable 2 are stored?
My understanding is, variable is stored on the stack and will be pop'd automatically when it goes out of scope, whereas variable2 is allocated in a block of memory reserved for my program ("heap") and will stay there even if I lose the reference to it. Is this correct? How is the "heap" created? Do I just get some memory from the system when I allocate memory? Or is there a limited block of memory already there, and that's all I'm allowed to use? If the latter, how does one control the size of the "heap?"

Recommended Answers

All 6 Replies

> will strMain become the reference for the block of memory allocated in the cc function as retVal?
> If I delete[] strMain, will that be the same as freeing the memory I allocated in cc()?
Yes and yes.
Though you need to add a \0 to make it a string inside cc().


> char* variable="hello world!";
variable is wherever you declare it (on the stack if this is inside a function). The string "hello world" is stored in the programs constant data. It always exists for the life of the program.

> char* variable2;
As above, but not initialised

> variable2=new char[13];
Now initialised, pointing to 13 chars on the heap.

> variable2="hello world!";
Bad news!
The heap memory you had has now leaked (you lost your last pointer to it).

"> variable2="hello world!"
Bad news!
The heap memory you had has now leaked (you lost your last pointer to it). "

I thought this would put "Hello world!" into the heap memory...
would I have to variable2[0]='h'; etc to send the characters to the heap memory?

"> char* variable="hello world!";
variable is wherever you declare it (on the stack if this is inside a function)."

what if it's in main()? Or declared outside of main(), as a global variable?

oh, and does the system just give my program some memory when I allocate it, or do I have a predefined limit to the memory I can allocate? Like, is there a block of memory already dedicated to my program when it runs, regardless of how much I use?

> would I have to variable2[0]='h'; etc to send the characters to the heap memory?
Yes, or use strcpy() to do the whole string for you.

> what if it's in main()? Or declared outside of main(), as a global variable?
Makes no difference.

but you said where the variable is stored depends on where it is declared/initialized

okay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.