hey guys, I am a beginner and am having trouble loading data from an external data file and then loading it to the screen. It is supposed to be the temperatures from everyday for a year for 2 years. 1 column is 1930 the other 2000

This is my script, and I am getting a stack overflow error:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAX_ELEM 365

void getdata(int d[], double A[][MAX_ELEM]);
void displaydata(int d[],double A[][MAX_ELEM]);
void main(){
	
	
	int d[2];
	double A[MAX_ELEM][MAX_ELEM];
	
	getdata(d,A);

	displaydata(d,A);

	system("pause");
}


void getdata(int d[],double A[][MAX_ELEM]){
	FILE *fp;
	
    int i, j;

	
	
	fp = fopen("data.txt", "r");


    fscanf(fp, " %d %d", &d[0], &d[1]);
	for(i = 0; i < d[0]; i=i+1)
	{
		for(j = 0; j < d[1]; j=j+1)
		{
			fscanf(fp,"%lf", &A[i][j]);
		}
	}
	fclose(fp);
}



void displaydata(int d[],double A[][MAX_ELEM]){

	int i,j;

	for(i = 0; i < d[0]; i++){
		for(j = 0; j < d[1]; j++){
			printf("\t%lf", A[i][j]);
		}

}
}

If anyone could answer why this is happening I would greatly appreciate it.

Thanks

there seems to be one major problem with your code....


*** IT'S IN C ***

(try posting your problem on the C forum, not c++)

Programming is Programming and some say "C++ is better C", so posting this in C is not a big issue.

I think the problem is related to size of arrays. Is it possible if you can give the text file containing the data?

By the way 2000 was a leap year so

#define MAX 365 // will not work for 2000

Secondly...

void getdata(int d[], double A[][MAX_ELEM]);
void displaydata(int d[],double A[][MAX_ELEM]);

You are treating A array as Global variable, but it is not.
You need to define it globally then you can use it in different methods.

Hello,

This code is indeed in C, and there are some things that you're trying to do that would be easier in C++. However, I think your problem here is that you have #define MAX_ELEM 365 and then you declare double A[MAX_ELEM][MAX_ELEM] , which is going to try and reserve 365x365 = 133225 doubles on the stack. A double is probably 4 (or 8) bytes on your computer so this is going to require 532900 bytes. There's not enough room on the stack for this!

Basically, you're going to have to use dynamic memory allocation for your array A[][]. That is, something like:

double **A = malloc(MAX_ELEM, sizeof(double));
for(int i = 0; i < MAX_ELEM; i++)
   A[i] = malloc(MAX_ELEM,sizeof(double));

in C. However, in C++ you can simply use a std::vector :

std::vector< std::vector< double > > A(MAX_ELEM, std::vector< double >(MAX_ELEM));

Hope that makes sense.

Sorry, I should have added that the std::vector is not the direct equivalent of using C-style arrays and malloc() . The equivalent of the malloc() code above, but in C++ would be:

double **A = new double *[MAX_ELEM];
for(int i = 0; i < MAX_ELEM; i++)
   A[i] = new double[MAX_ELEM];

std::vector is a lot more flexible than a C-style array though, so I would use them instead (if you're using C++, that is)

*** glibc detected *** double free or corruption (out): 0x09d17378 ***
Aborted

I come to this problem when I run a program,even if I pass the edit

I cannot find out how to solve this problem,please help me,many thanks!

I cannot find out how to solve this problem,please help me,many thanks!

Er, you seem to be trying to hijack this thread. If you're not posting a solution or something extra about exactly the same problem, you should really start a new thread. Additionally, you should provide some more details of what your problem is. For example, post the part of the code that you think is causing this problem.

I think I know what is going wrong, but I'm going to wait for you to start a new thread before answering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.