473,409 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Allocating memory for arrays

Hi everyone! I'm new to this forum so any advise will be very much appreciated.

I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum values of an image(satellite image) and do some computation on it so I can classify them into dry grass, road, trees, water, building,etc.

The text files contains 4 values for each pixel representing 4 bands(band 1,band 2 band 3, band 4) for each pixel so if i have a 4x5 image i would have a total of 16 rows and 5 columns. the first 4 rows would be for band 1 representation of the image,the next 4 rows would be for band 2 representation of the image...

Im using 2D arrays in storing these pixel values from the file,it works for that 4x5 image but when i use a file representing a 400x400 image it crashes. So i think it has a problem in allocating memory for my arrays.

here's a copy of the code i created.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void stripNewline(char *string,int size);
  6.  
  7. int main()
  8. {
  9.     char fileName[30];
  10.     int rows, cols, bands;
  11.  
  12.     //user input
  13.     printf("================ TEST IMAGE ===============\n");
  14.     printf("Enter the SBQ text file: ");
  15.     fgets(fileName,30,stdin);
  16.     stripNewline(fileName,30);
  17.     printf("Enter the number of rows in the test image: ");
  18.     scanf("%d",&rows);
  19.     printf("Enter the number of columns in the test image: ");
  20.     scanf("%d",&cols);
  21.     printf("Enter the number of bands used in the test image: ");
  22.     scanf("%d",&bands);
  23.  
  24.     printf("\n================ REFERENCE SPECTRA ===============\n");
  25.     //prepare matrix for reference
  26.     int referenceArray[bands],b,i;
  27.     i = 1;
  28.     for(b=0;b<bands;b++){
  29.         printf("Spectrum value for Band %d: ",i);
  30.         scanf("%d",&referenceArray[b]);
  31.         i++;
  32.     }
  33.  
  34.     //get data from file
  35.     FILE *file;
  36.     static int imageArray[20][4];
  37.     int p;
  38.  
  39.     printf("\nOPENING FILE...\n");
  40.     file = fopen(fileName,"r");
  41.  
  42.     if(file == 0){
  43.         printf("%s could not be opened\n",fileName); //file not found or does not exist
  44.     }else{
  45.         printf("%s OPENED SUCCESSFULLY\n",fileName); //opened file
  46.  
  47.         printf("\nPREPARING NEEDED MATRICES FOR CLASSIFICATION...\n");
  48.  
  49.         //prepare matrices of each test pixel in image
  50.         for(b=(bands-1);b>=0;b--){
  51.             for(p=0;p<(rows*cols);p++){
  52.                 fscanf(file,"%d",&imageArray[p][b]);
  53.             }
  54.         }
  55.  
  56.         printf("\nCLASSIFYING USING SAM...\n");
  57.  
  58.         //COMPUTE FOR COSINE OF ANGLE
  59.         //compute for numerator
  60.         int summationOfTR[(rows*cols)];
  61.         for(p=0;p<(rows*cols);p++){
  62.             summationOfTR[p] = '\0';
  63.             for(b=0;b<bands;b++){
  64.                 summationOfTR[p] = summationOfTR[p] + (imageArray[p][b] * referenceArray[b]);
  65.             }
  66.             //printf("%d\n", summationOfTR[p]);
  67.         }
  68.         //compute for denominator
  69.         int summationOfT2[(rows*cols)];
  70.         double sqrtSumOfT2[(rows*cols)];
  71.         int summationOfR2 = 0;
  72.         double sqrtSumOfR2 = 0;
  73.         double denominator[(rows*cols)];
  74.         //summationOfT2
  75.         for(p=0;p<(rows*cols);p++){
  76.             summationOfT2[p] = '\0';
  77.             for(b=0;b<bands;b++){
  78.                 summationOfT2[p] = summationOfT2[p] + pow(imageArray[p][b],2);
  79.             }
  80.             //printf("%d\n", summationOfT2[p]);
  81.         }
  82.         //sqrtSumOft2
  83.         for(p=0;p<(rows*cols);p++){
  84.             sqrtSumOfT2[p] = '\0';
  85.             sqrtSumOfT2[p] = sqrt(summationOfT2[p]);
  86.         }
  87.  
  88.         //summationofR2
  89.         for(b=0;b<bands;b++){
  90.             summationOfR2 = summationOfR2 + pow(referenceArray[b],2);
  91.         }
  92.         //sqrtSumOfR2
  93.         sqrtSumOfR2 = sqrt(summationOfR2);
  94.         //denominator
  95.         for(p=0;p<(rows*cols);p++){
  96.             denominator[p] = '\0';
  97.             denominator[p] = sqrtSumOfT2[p] * sqrtSumOfR2;
  98.         }
  99.  
  100.         //cosine of angle
  101.         double cosineOfAngles[(rows*cols)];
  102.         for(p=0;p<(rows*cols);p++){
  103.             cosineOfAngles[p] = '\0';
  104.             cosineOfAngles[p] = summationOfTR[p] / denominator[p];
  105.  
  106.             if((p%(cols)) == 0){
  107.                 printf("\n");
  108.             }
  109.             printf("%.5lf ",cosineOfAngles[p]);
  110.         }
  111.  
  112.  
  113.         fclose(file);
  114.     }
  115.  
  116.     getchar();
  117.     return 0;
  118. }
  119.  
  120.  
  121. //strip newline character '\n' after getting string input from user
  122. void stripNewline(char *string,int size){
  123.     int i;
  124.     for(i=0;i<size;i++){
  125.         if(string[i] == '\n'){
  126.             string[i] = '\0';
  127.             break;
  128.         }
  129.     }
  130. }
Thanks in advance!
Oct 12 '08 #1
1 1516
Banfa
9,065 Expert Mod 8TB
Line 36, you allocate a fixed amount of memory instead of the amount the user requested.
Oct 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
6
by: Amit_Basnak | last post by:
Dear Friends I have two structures as below typedef struct { long_int length; char data; } CI_STRUCT_DATA; typedef CI_STRUCT_DATA *ptr_CiStructData;
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
13
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.