You are not logged in.

#1 2012-08-11 21:19:44

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,408

[Solved] Variables overwritten when initializing others

I have a float []={.......}
followed by another float []={......}
followed by a matrix that can hold both above floats in a float[][]
When I go to initialize float[][]=0.00 it overwrites values in one of the above float[]={......}
This is verified when printf (float []) to see if the action by float[][]=0.00 was in fact overwriting and it is.
I'm wondering if this is bug of some kind or I need additional switches when I compile to g++?

----------
Apparently it doesn't like 2d arrays?
When I went to 1d array it didn't overwrite, but I should be able to use 2d, shouldn't I?
It compiles ok, just overwrites the variables it shouldn't be overwriting.
I'll just have to make a calculation so that I can access the proper number in the array.

Last edited by nomorewindows (2012-08-12 13:44:35)


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#2 2012-08-12 06:58:04

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,334

Re: [Solved] Variables overwritten when initializing others

nomorewindows wrote:

Apparently it doesn't like 2d arrays?

Nice anthropomorphism.

It is possible you are getting confused over arrays of pointers vs arrays of floats.  Can you post the actual code?
Have you looked at the standard template library ?

Can you attest this is not homework?


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Online

#3 2012-08-12 12:28:39

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,408

Re: [Solved] Variables overwritten when initializing others

float proc(.......)
{
.....
float a[]={........};
float b[]={........};
float c[2][5];
for (int d=0;d<3;d++)
for (int e=0;e<6;e++)
c[d][e]=0.00; //initialization
printf ("%f %f %f",b[0],b[1],b[2]); //assertion that values changed as a result of initialization
......
return(....);
}

It worked when I did:
{
.....
int c[17];
for (int d=0;d<17;d++)
c[d]=0;
.....
for (int f=0;f<3;f++)
for (int g=0;g<6;g++)
.....
c[(f*6)+g]++;
.....
}

I've found my equation that I'm using needs some slight integration across all of the matrix.
In order to pass on to another function, I only need parts of the matrix that have values that I'm looking for.
Even though I freshened up on standardized C/C++ with STL, I still do the old way with 2d arrays.
I guess the STL array keeps track of how many elements better than just the normal array does, but since I have such a small array, I didn't figure I need it.
I'm just trying to perfect something I use all the time.


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#4 2012-08-12 12:40:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [Solved] Variables overwritten when initializing others

Code tags would help.  Also redacted code is hard to troubleshoot.  You are assuming the problem is not in those parts you chose to leave out - but if you knew where the problem was, you wouldn't be here.

You are setting data outside your array with those for loops.  Why 3 and 6?  You should use 2 and 5.  I'm surprised this didn't give an error, but in the absence of the error this could be why the previous array is overwritten: your for loops are zeroing out memory space (8 floats worth to be exact) that does not belong to the two dimensional array.

Perhaps there is no error as arrays are really just pointers, and as they are pointing to your programs own allocated memory there is no seg fault.

Also, you may want to check out memset().

Last edited by Trilby (2012-08-12 12:42:15)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#5 2012-08-12 13:01:36

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,408

Re: [Solved] Variables overwritten when initializing others

I was thinking it started at 0, 1, 2 which would coincide with 1,2,3 in our world.
Arrays don't have terminators like strings do as far as I remember.
The for loop (a<3 and b<6) would be less than 3 and less than 6, which would stop at 2, and 5.  I could just change that to a<=2 and b<=5, but essentially as far as I remember it comes out the same.


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#6 2012-08-12 13:16:12

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: [Solved] Variables overwritten when initializing others

Yes, that would come out the same.  But they are both larger than your array.

You declare your array as [2][5].  This means it has 2 rows and 5 columns.

if you loop from 0 to 5 for columns that is 6 columns.  If you loop from zero to 2 for rows (0,1,2) that is three rows.

You array declarations (the [2][5]) do not hold the number of the highest index, they hold the number of rows and columns: the highest index is one less than this.

In other words, you declare a 2x5 array (10 elements).  But you loop through rows 0,1,2 and columns 0,1,2,3,4,5 (18 elements).

Last edited by Trilby (2012-08-12 13:18:11)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2012-08-12 13:25:08

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,408

Re: [Solved] Variables overwritten when initializing others

You're telling me for sanity's sake I need to query the sizeof(a[]/sizeof(float)) and sizeof(b[]/sizeof(float)) to see if it comes out the same as my c[2][5]?
Which I'm only needing 3 rows and 6 colums.
My integration works with the a[] and b[] using a<3 and b<6 but you're telling me I'm not matching up to what I think I'm matching up with c[2][5] and it needs to be c[3][6] otherwise it will spill over?
I only use C/C++ once in awhile when I'm doing something like this, so I forget this stuff.
-----------------
Made the change to the array for c[3][6] and now it looks like that was the problem.  May go back and try a<=2 and b<=5 in my "spare" time.
Looks like I'm not allocating enough space for brain[][][][][] (I'm not sure how many dimensions there are, but I'm not allocating enough space in it to remember anything).

Last edited by nomorewindows (2012-08-12 13:43:50)


I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.

Offline

#8 2012-08-13 00:40:18

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [Solved] Variables overwritten when initializing others

Let's use a 1-dimensional array for starters. The definition

int a[10];

reserves space for 10 contiguous ints. The first one is a[0], the second one is a[1], the third one is a[2], and the tenth one is a[9]. You might traverse this array using a for loop as follows:

for (size_t i = 0; i < 10; i++) {
    /* do stuff with a[i] */
}

This works because it iterates 10 times. The first time i == 0, the second time i == 1, and so forth, but when i == 10 the test `i < 10` is not true anymore, so the loop body is not executed. Right?

So let's expand this to 2D arrays. The definition

int a[10][5];

makes a an array[10] of array[5] of int, or 10 "rows" of 5 "columns" each ("rows" and "columns" are more or less arbitrary designations in this context; they could as easily go the other way around). So that's 50 integers, and if you use sizeof on it you're likely (but not certain) to get 50 * sizeof(int).

So what is a? First off, it's an array of length 10. So you handle it the same way as in the last example.

for (size_t i = 0; i < 10; i++) {
    /* do stuff with a[i] */
}

But each element of a is itself an array (a "row") of length 5. So you can handle that one similarly, within the above for loop.

for (size_t i = 0; i < 10; i++) {
    for (size_t j = 0; j < 5; j++) {
        /* do stuff with a[i][j] */
    }
}

There are three very important C principles in action here:

Arrays are zero-indexed. An array of length N has elements identified by indexes 0 through (N - 1). The nth element of the array `list` is accessed by `list[n - 1]`.

The array subscripting operator [] associates left-to-right. `array[x][y]` is equivalent to ( array[x] )[y], not array ( [x][y] ) (which isn't even syntactically legal, if it helps you to notice.)

Declaration mimics use. A declaration like "int a[N][M]" means that a[x][y] is an int, while a[x] is an array [M] of int. C FAQ question 1.21 explains it better than I.

I'm sure you probably already knew most of this, but hopefully I'll help you not to make the same mistake again. The rules really do make sense, and are quite sensible and orthogonal when you know why they're there. "Declaration mimics use" is one that I often miss in other languages (`int[] thing` always makes me flinch).

nomorewindows wrote:

Looks like I'm not allocating enough space for brain[][][][][]

LOL big_smile

Offline

Board footer

Powered by FluxBB