Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 23 Sep 2018, 11:49
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
dynamic memory allocation in C++
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
mahaju


Joined: 11 Oct 2010
Posts: 491
Location: between the keyboard and the chair

PostPosted: 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
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 778

PostPosted: 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:
Code:

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.
Back to top
View user's profile Send private message 
jamesjeffries2

Joined: 27 Apr 2008
Posts: 196

PostPosted: 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
View user's profile Send private message 
mahaju


Joined: 11 Oct 2010
Posts: 491
Location: between the keyboard and the chair

PostPosted: 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
View user's profile Send private message 
jamesjeffries2

Joined: 27 Apr 2008
Posts: 196

PostPosted: 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
View user's profile Send private message 
jamesjeffries2

Joined: 27 Apr 2008
Posts: 196

PostPosted: 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
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0677s ][ Queries: 11 (0.0060s) ][ GZIP on ]