| Author |
Message |
mahaju

Joined: 11 Oct 2010 Posts: 455 Location: between the keyboard and the chair
|
Posted: Fri 14 Oct 2011, 09:39 Post_subject:
dynamic memory allocation in C++ |
|
Hi everyone
Suppose I do something like this
| Code: | #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
|
|
Back to top
|
|
 |
Moose On The Loose

Joined: 24 Feb 2011 Posts: 283
|
Posted: Fri 14 Oct 2011, 10:45 Post_subject:
Re: dynamic memory allocation in C++ |
|
| mahaju wrote: | Hi everyone
Suppose I do something like this
| Code: | #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: |
i=0;
while (i<10) i++
|
The compiler makes:
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.
|
|
Back to top
|
|
 |
jamesjeffries2
Joined: 27 Apr 2008 Posts: 196
|
Posted: Fri 11 Nov 2011, 14:16 Post_subject:
|
|
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!
|
|
Back to top
|
|
 |
mahaju

Joined: 11 Oct 2010 Posts: 455 Location: between the keyboard and the chair
|
Posted: Sat 12 Nov 2011, 06:29 Post_subject:
|
|
Hi
Thanks for your replys
What if I do this?
| Code: | #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?
|
|
Back to top
|
|
 |
jamesjeffries2
Joined: 27 Apr 2008 Posts: 196
|
Posted: Sat 12 Nov 2011, 09:52 Post_subject:
|
|
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: | #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
|
|
Back to top
|
|
 |
jamesjeffries2
Joined: 27 Apr 2008 Posts: 196
|
Posted: Sat 12 Nov 2011, 09:55 Post_subject:
|
|
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
|
|
Back to top
|
|
 |
|