Solved

dynamic data inside a loop

Posted on 2004-08-20
7
203 Views
Last Modified: 2010-04-15
I have a loop inside which I will be getting external data using some function call. That function call returns an integer value which is the data.
Inside that loop, I want to keep accumulating all these data and once the loop is over, i'd like to process that data seuqntially.

how do i implement this. I do not know how many results i will be getting back . so every thing has to be dynamic.
pointer to pointer? array of pointers? what do i use?

Thanks
R
0
Comment
Question by:ramnars
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11853710
Hi ramnars,

Either
you create a buffer big enough to handle the maximum amount of data
or
you allocate memory ( malloc() ) for the new data, process it and then free it at the end.

======
Werner
0
 

Author Comment

by:ramnars
ID: 11853877
Actuallly I want a container like thing. just like c++ vector.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11853886
It depends on the size and the probability of the length of the data.

If the size can vary in very large amounts,malloc() a memory of a certain size and realloc another batch, when the current one gets full,using realloc().

If the size doesnt vary largely,malloc a large length or define an array of  a large enough size.

For the first case,
int *arr;
arr=(int*)malloc(100*sizeof(int));//say you allocate space for 100 integers initially

Inside the loop,if you need to realloc.

arr=(int *)realloc(arr,200);//now arr has space for 200 integers.the second argument specifies the new size.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:ramnars
ID: 11854007
ok. I want to process all the data only after the loop is over. So in ur example, I need to store arr into some other variabale and process that list later. Do I need to use a linked list? or is there any other option ?
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11854259
No.You can use arr as the array to store the data.You just dynamically keep setting its size to accomodate the new elements.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 25 total points
ID: 11854298
Consider this scenario.

You allocate space for 100 integers initially,as in my example.

Then,when you realize that your 100 integers space has been used up,realloc() another 100,and so on.

At the end of the loop,you'd have stored all your elements in the arr variable,and you can access them,just as you do in an array.


0
 

Author Comment

by:ramnars
ID: 11854496
got it. thanks
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference between mmap and malloc/valloc + mmap fixed 18 253
Pointer in one class to member in another 6 116
Inorder binary search tree 5 158
Acrinis True image 2 57
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now