473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ Dynamic allocation within a loop

I have a program in which I am trying to create a chain of (unspecified number) nodes. I want to be able to use a loop to create a new node and then link it in. I've tried it (and I think it works) to put the new within the loop and just have the compiler allocate a new version of the variable using the same name.

E.g.,
Expand|Select|Wrap|Line Numbers
  1.  for (      )
  2.                      Node * anode = new Node();    
This doesn't seem to be the best way to do things.... I also tried creating an array of Node pointers beforehand and then doing the linking in a subsequent loop. Is there a more effective approach to this type of situation.

Thanks for any help. Sorry for not offering a more concrete example in the code.
Dec 2 '08 #1
9 4398
newb16
687 512MB
If you need ability to delete these nodes one-by-one there will always be N calls to memory allocation routine and N call to Node() constructor, I don't see how it can be faster if you put it in separate loop.
Dec 2 '08 #2
I meant that the array method would give each instance of the variable a unique identifier within the program whereas if I run through the for loop 5 times I have 5 objects I can't access by name. I understand your point about speed.
Dec 2 '08 #3
gpraghuram
1,275 Expert 1GB
I cant clearly get ur requirement.
You want to store all the elements in a arry or want to store it as a linked list.?

Raghu
Dec 2 '08 #4
Savage
1,764 Expert 1GB
@JonathanS
Depends on what do you want.Are those nodes going to be searched frequently?(Array is faster)
Will you have a need to add a node to the top or the bottom?(List can be made faster)
Do you want it to dynamically resize easily?(List is faster)

Answer those questions and you should know whether do you need to use lists or array of pointers to Node.
Dec 2 '08 #5
Banfa
9,065 Expert Mod 8TB
You should not be using an array at all. You should be using an STL <vector> by default. If you have a reason like needing to be able to search or needing to be able to insert/delete from anywhere other than the end then consider one of the other STL containers.
Dec 2 '08 #6
Thanks for all of the advice on the matter. The vector container seems like a better bet. I'm still troubled by whether or not (outside of this particular case) creating instantiations of an object with new within a loop structure is good practice.

To gpraghuram: I wanted to ultimately use a linked list, but didn't know how to get a set of pointers in a loop due to the above issue
Dec 3 '08 #7
Banfa
9,065 Expert Mod 8TB
If you know the number of iterations of the loop before you start it then you could have a vector of objects (rather than pointers) and just set its size before the loop starts. That would allocate your data in 1 large block.
Dec 3 '08 #8
jtgd
2
@JonathanS
In any case you must keep track of the memory that you new so you can delete it later, whether it is in an array of pointers or a linked list.
Dec 5 '08 #9
Banfa
9,065 Expert Mod 8TB
@JonathanS
It isn't bad practice in general if that what the program logic calls for.

What you have to watch out for is your over-all memory allocation scheme, for instance a logic that requires that you are constantly allocating and deallocating small amounts of memory but that not every piece allocated is necessarily deallocated immediately is likely to fragment your heap (particularly on more simplistic heaps). This is undesirable as it makes the heap inefficient and if it becomes sufficiently fragmented then you can find yourself in the situation of being unable to allocate a larger block on memory because although there is more and enough heap space there is not a single block big enough.
Dec 5 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
18
by: hyperboogie | last post by:
Hello all I'm pretty new to C, so please accept my apologies in advance :-) I'm trying to allocate space for an array of pointers to strings (which are accepted as ellipses) inside a while...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.