Solved

dynamic data inside a loop

Posted on 2004-08-20
7
207 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
[X]
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
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
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!

 

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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

691 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