dynamic data inside a loop

Posted on 2004-08-20
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
  • 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.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


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 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.


Author Comment

ID: 11854496
got it. thanks

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

840 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