Solved

dynamic data inside a loop

Posted on 2004-08-20
7
201 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

706 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

20 Experts available now in Live!

Get 1:1 Help Now