dynamic memory allocation in C++

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

dynamic memory allocation in C++

#1 Post 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

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

Re: dynamic memory allocation in C++

#2 Post 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.

jamesjeffries2
Posts: 196
Joined: Mon 28 Apr 2008, 00:50

#3 Post 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!

User avatar
mahaju
Posts: 487
Joined: Mon 11 Oct 2010, 07:11
Location: between the keyboard and the chair

#4 Post 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?

jamesjeffries2
Posts: 196
Joined: Mon 28 Apr 2008, 00:50

#5 Post 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

jamesjeffries2
Posts: 196
Joined: Mon 28 Apr 2008, 00:50

#6 Post 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

Post Reply