dynamic data inside a loop

Posted on 2004-08-20
Medium Priority
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?

Question by:ramnars
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 11

Expert Comment

ID: 11853710
Hi ramnars,

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


Author Comment

ID: 11853877
Actuallly I want a container like thing. just like c++ vector.

Expert Comment

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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

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 ?

Expert Comment

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.

Accepted Solution

ankuratvb earned 100 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.


Author Comment

ID: 11854496
got it. thanks

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

650 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