Page 1 of 1

dynamic memory allocation in C++

Posted: Fri 14 Oct 2011, 13:39
by mahaju
Hi everyone
Suppose I do something like this

Code: Select all

#include<iostream.h>
#define NUM 15
#define DEN 6

int main(){
	int e;
	e=NUM/DEN;
	char array[e];
	.
	.
	.
	.
	return 0;
}
Does the array declaration now need dynamic memory allocation? I know that the compilers like gcc will compile it, even if the value of e was taken as an input from the user, but that is another question as well. Since value of e is not hardcoded into the program, why does declaring an array with maximum size e not create an error?Is it because of some features built into the compiler so that it always works? I will eventually need to use numbers that are thousands of bits long, so I will probably be trying to implement my own data types, and at that moment I don't want to be bothered by some obscured compiler feature that I don't understand

Please help me with this
Thank you very much

Re: dynamic memory allocation in C++

Posted: Fri 14 Oct 2011, 14:45
by Moose On The Loose
mahaju wrote:Hi everyone
Suppose I do something like this

Code: Select all

#include<iostream.h>
#define NUM 15
#define DEN 6

int main(){
	int e;
	e=NUM/DEN;
	char array[e];
	.
	.
	.
	.
	return 0;
}
Please help me with this
Thank you very much
gcc is clever. It sees that NUM and DEN are both constants and computes the value of "e" at compile time. when it gets to the line where you declare the array, the value of "e" is known and fixed. This means it is as though you had typed in a constant and thus there is no issue with how much space to provide.

Some compilers will even unroll simple loops and work out the value at compile time.

Feed in this:

Code: Select all

i=0;
while (i<10) i++
The compiler makes:

Code: Select all

i=10
In some cases, it will crush out nearly your whole program. If you use both the "inline" and "combine" features, calls to functions that pass constants get reduced to simple assignments. I use this when writing code. You turn off all the optimizes when first debugging so you can see the values being worked out and then turn them on to reduce the program overhead to nill.

Posted: Fri 11 Nov 2011, 18:16
by jamesjeffries2
Hello

Hope this explanation isn't too technical...

The reason your array doesn't need dynamic memory allocation is that you have allocated it on the stack. This means it will be allocated in a bit of memory especially for you function to use and it will be automagically freed (or deleted in c++) when your function finishes.

If you were to allocate it on the heap (using malloc or new) then it would be accessible after the function has finished, if you made sure you still had a pointer to it. If you don't and you haven't deleted it then you will get a memory leak, so watch out!

Hope this is useful and not too confusing!

Posted: Sat 12 Nov 2011, 10:29
by mahaju
Hi
Thanks for your replys
What if I do this?

Code: Select all

#include<iostream.h>
#define NUM 15
#define DEN 6

int main(){
   int e;
   cout<<"Enter e>> ";
   cin>>e;
   char array[e];
   return 0;
}
Why doesn't gcc produce error in this case while my old turbo C++ does? Value of e definitely cannot be known at compile time. How does this compile then?

Posted: Sat 12 Nov 2011, 13:52
by jamesjeffries2
Sorry I think I misunderstood what you meant by dynamic allocation. This is still dynamic allocated in terms of it's size, however you don't need to worry about tidying up after your self (freeing memory after you have used it). The compiler will just know that at that point it needs to allocate some memory with the size specified in your variable 'e'.

If you are planning to be dealing with strings a lot then I would recommend using the STL string container. This is mutable and will handle different sizes a lot more easily than if you are using a char array.

For example

Code: Select all

#include <string>
#include<iostream> 

using namespace std;

int main()
{
	string s1;
	string s2;
	cout << "Enter a word:";
	cin >> s2;
	s1 += s2;
	cout << "\nEnter another word:";
	cin >> s2;
	s1 += s2;
	
	cout << s1 << endl;
	
	return s1.size();
}
also in c++ you do not need to put the ".h" bit for precompiled header files such as those in the STL like iostream and string.

Hope this helps

Posted: Sat 12 Nov 2011, 13:55
by jamesjeffries2
In particular I would recommend this version of the string class. It is derived from std::string, but has lots of extra features, such as string formatting.
http://www.codeproject.com/KB/string/stdstring.aspx