newbie q

C

Christopher Smith

All -

I'm a bonafied newbie to programming and am having trouble using
Arrays. Also, I'm not quite sure how to ask the question intelligently,
but here it goes...

I've got his array that I populate with data. I've got another array
that I want to populate with different data. (The reason I'm doing this
is I have time-series data that I want to analyze and so I need to
generate a series of statistics off the underlying data and then compare
them.)

The problem is that after I successfully populate the first array I then
move on to populating the second array, which seems to -- magically as
far as I've been able to figure out -- reset the ArrayA[0][0] value to
last value in ArrayA. So, if I 'ArrayA' looked like this at the end of
step one:

1 2 3
4 5 6
7 8 9
3 4 5

In step two, when the system had col = 2 and row = 3 and should be
filling ArrayB at location ArrayB[3][2] with some data -- call it X--,
what happens is ArrayA[0][0] actually gets overwritten with the value 5
at ArrayA[3][2]. I have absolutely no idea why this happening...using
GNU debugger, I've been able trace the overwrite down this last step...

I'm assuming that is some sort of memory addressing error or something,
and I have read everything I could find out there on the web about using
arrays but w/ no luck.

------------------------------------------------------------------------
/* step one */
for (col=0; col < num_columns; col++) {
for (row=0; row < num_rows; row++) {
ArrayA[row][col]=various_data;
}
}

/* step two */
for (col=0; col < num_columns; col++) {
for (row=0; row < num_rows; row++) {
ArrayB[row][col]=other_various_data;
}
}
 
K

Keith Thompson

Christopher Smith said:
In step two, when the system had col = 2 and row = 3 and should be
filling ArrayB at location ArrayB[3][2] with some data -- call it X--,
what happens is ArrayA[0][0] actually gets overwritten with the value
5 at ArrayA[3][2]. I have absolutely no idea why this
happening...using GNU debugger, I've been able trace the overwrite
down this last step...

I'm assuming that is some sort of memory addressing error or
something, and I have read everything I could find out there on the
web about using arrays but w/ no luck.

------------------------------------------------------------------------
/* step one */
for (col=0; col < num_columns; col++) {
for (row=0; row < num_rows; row++) {
ArrayA[row][col]=various_data;
}
}

/* step two */
for (col=0; col < num_columns; col++) {
for (row=0; row < num_rows; row++) {
ArrayB[row][col]=other_various_data;
}
}

It's impossible to tell from what you've posted. We don't know how
ArrayA and ArrayB are declared ,or how num_columns and num_rows are
initialized.

My best guess is that ArrayA and ArrayB happen to be adjacent in
memory, and that you're writing past the bounds of ArrayB and thereby
clobbering part of ArrayA. Check that your indices (row and col) are
within the declared bounds of the arrays. Don't forget that C arrays
are zero-based; for example, given:

int arr[10][20];

the first index runs from 0 to 9, and the second runs from 0 to 19.
(It looks like you've got this right, but check it anyway.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top