Click here to Skip to main content
14,638,959 members
Rate this:
Please Sign up or sign in to vote.
See more:
for(j=0;j<e1;j++)
{
index1=PZLoctn1[j];
index2=NZLoctn1[j];
printf("\n%d\t%d\n",index1,index2);
	
	for(i=index1;i<index2;i++)
		{
		   pz2nz[0][i]=kld_smooth_norm[i];
		  // printf("pz2nz:%f\n",pz2nz[0][i]);	
		}
		  
}

here kld_smooth_norm[80,000] and the index1 index2 will be changing for each loop
first loop index1=12612 index2=14521
2nd loop index1=16029 index2 =23753
3rd loop index1=31387 index2= 55794
4th loop index1=62933 index2=68856
and in pz2nz i want to store all the element for each loop from index1 t oindex2
can someone tell me how to declare the size of pz2nz for storing the elements
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't use an array, use one of the Collection classes[^].

[edit]
Apologies I misread C for C#. You can make an array dynamic in C by allocating it at run time via malloc[^].

[/edit]
   
v3
Comments
nv3 17-Jan-15 5:55am
   
Op is talking about C, not C#.
Richard MacCutchan 17-Jan-15 6:05am
   
Oops!
Rate this:
Please Sign up or sign in to vote.

Solution 2

There is no way to declare an array in C, the size of which is unknown at compile-time. Instead you can use malloc / free to allocate storage dynamically and assign its address to a pointer.

Allocating storage takes some time and it is advisable to avoid allocating / freeing storage in a high frequency, for example in a loop. In your case that seems to be possible easily: Determine in a first loop the maximum length needed and then allocate that storage once.

Another observation here, is that you seem to use only small index intervals in each loop, for example 62933 ... 68856. In such a case it may pay off to just allocate an array for that index interval, 5924 cells in that case, and recalculate the index with which you access that array.

In C++ the job would be somewhat easier as you could make use of classes like vector, which does much the storage management for you.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100