problem in multiplying arrays


 
Thread Tools Search this Thread
Top Forums Programming problem in multiplying arrays
# 8  
Old 12-05-2011
Quote:
Originally Posted by giampoul
If you have the time can you explain me a little these lines:
Code:
B = A + OFFSET(3, 2, 0);
C = B + OFFSET(3, 4, 0)

It's the same as B = A + (3*2). Just reusing my code.
Quote:
Also why its wrong with the 3 shmat() functions?
shmat() always gives you the same memory for the same ID -- that's what lets you share it between processes, you get the same memory.

Code:
// process 1

int *mem=shmat(...);

mem[0]=0xdeadbeef;

// process 2

int *mem=shmat(...);
printf("%d\n", mem[0]); // should print deadbeef if process 2 runs after 1

Modify it in one process and it gets modified in the other.

The idea's the same here, except you were using the same segment in the same process three times. It's not like malloc(), where you get new memory each time. Modify A and you modify B and C.

If you want three different segments, you need 3 different IDs. Or you can just use different memory in the same segment, like I did.

Code:
 A[0]=0xdeadbeef;
        printf("%x %x %x\n", A[0], B[0], C[0]);

To avoid that, I use different areas of the same segment for different matrices. It's sort of like this:

Code:
int bigarray[]={1, 2, 3, 4, 5, 6, // 6 elements
        12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, // 12 elements
        0, 0, 0, 0, 0, 0, 0, 0 // 8 elements
        };

int *A=bigarray; // Starts at element 0
int *B=A+(2*3); // Starts at element 6
int *C=B+(3*4); // Starts at element 18



Quote:
In another program i made where A and B were read by a .txt file the arrays are correct.
I couldn't tell you what your other program was doing. I can't see your computer from here.

Last edited by Corona688; 12-05-2011 at 05:57 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 12-05-2011
I understand.

But can't i just use the easy way with arrays, like that:
Code:
...
int A[2][3];
A=(int *)shmat(id,NULL,0);
...

I googled before i start the project and i found that arrays can be saved only with this way in shared memory.
If i could save them in the easy way that would be very helpful for my project.
# 10  
Old 12-05-2011
Well, it depends... Are these arrays going to change in size, at all, ever? Or are they going to be a fixed size, forever?

If they're going to be the same size forever, you can do this:
Code:
struct shared {
        int A[2][3];
        int B[4][3];
        int C[4][2];
};

struct shared *s=(struct shared *)shmat(id,NULL, 0);

x=1;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
        s->A[i][j]=x++;

x=12;
for(i=0; i<4; i++)
for(j=0; j<3; j++)
        s->B[i][j]=x--;

---------- Post updated at 04:27 PM ---------- Previous update was at 04:17 PM ----------

In fact you can put any kind of data structure you want in shared memory, as long as it doesn't contain pointers.
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 12-05-2011
They are going to be in fixed sized during the program...
In fact i read their size from the .txt and also their contents from i, so they are going to be like that:
Code:
struct shared {
        int A[l][m];
        int B[m][n];
        int C[l][n];
};

So maybe with the struct i'll do the job.
I'll try it thanks!
# 12  
Old 12-05-2011
Quote:
Originally Posted by giampoul
They are going to be in fixed sized during the program...
In fact i read their size from the .txt and also their contents from i
That's mutually exclusive.

If they're fixed in size, they don't change and therefore can't be set at runtime.

Quote:
so they are going to be like that:
Code:
struct shared {
        int A[l][m];
        int B[m][n];
        int C[l][n];
};

That won't work. l, m, n have to be constant numbers. That's why I asked whether they ever change.

---------- Post updated at 05:00 PM ---------- Previous update was at 04:51 PM ----------

You can do something like this:

Code:
#define A(R,C)        a[(a_columns*R)+C]
#define B(R,C)        b[(b_columns*R)+C]
#define C(R,C)        c[(c_columns*R)+C]

int main(void)
{
        int a_columns=3, a_rows=2;
        int b_columns=4, b_rows=3;
        int c_columns=4, c_rows=2;

        int *a=shmat(...);
        int *b=a + (a_rows * a_columns);
        int *c=b + (b_rows * b_columns);

        for(i=0; i<2; i++)
        for(j=0; j<3; j++)
                A(i, j)=x++;
}

Or this:

Code:
// Cheap substitution trick.
// ARR(zzz, R, C) becomes zzz[(zzz_columns * R) + C]
// So this won't work unless a zzz_columns variable actually exists.
#define ARR(NAME,R,C)        NAME[(NAME ## _columns*R)+C]

// ARR_SIZE(yyy) becomes (yyy_columms * yyy_rows)
// so yyy_columns and yyy_rows need to exist somewhere if you do that!
#define ARR_SIZE(NAME)  (NAME ## _columns * NAME ## _rows)

int main(void)
{
        int A_columns=3, A_rows=2;
        int B_columns=4, B_rows=3;
        int C_columns=4, C_rows=2;
        
        int *A=shmat(...);
        int *B=A+ARR_SIZE(A);
        int *C=B+ARR_SIZE(B);

        for(i=0; i<A_rows; i++)
        for(j=0; j<A_columns; j++)
                ARR(A, i, j)=x++;
}

This User Gave Thanks to Corona688 For This Post:
# 13  
Old 12-05-2011
Trying it ....
# 14  
Old 12-05-2011
these are my programs but i get this error at both:
Code:
main.c: In function ‘main':
main.c:33:22: error: called object ‘a' is not a function
main.c:43:22: error: called object ‘b' is not a function
main.c:115:34: error: called object ‘c' is not a function


Last edited by giampoul; 12-05-2011 at 08:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Multiplying 2D arrays using fork()

HI, i am trying to multiply 2 2D arrays (a,b) using fork. The answer will be at c. Each child have to calculate 1 row of c. The code is right, as i think of it, with no errors but i dont get the correct c array... I think there is maybe a mistake in i dimension ... Anyway, here is the code: ... (16 Replies)
Discussion started by: giampoul
16 Replies

2. Shell Programming and Scripting

Multiplying array element

I am trying to take all the elements of an array and multiply them by 2, and then copy them to a new array. Here is what I have i=0 for true in DMGLIST do let DMGSIZES2="${DMGSIZES}"*2 let i++ done unset i echo ${DMGSIZES2} It does the calculation correctly for the first element,... (7 Replies)
Discussion started by: nextyoyoma
7 Replies

3. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

4. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

5. Shell Programming and Scripting

Problem with arrays

Hi I have two arrays: arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) I want to check that the values the "arr2" are present in "arr1" Example arr1 = (demo demo2 demo3 demo4 demo5) arr2 = (demo2 test demo) Output: Error arr1 = (demo demo2 demo3 demo4 demo5)... (3 Replies)
Discussion started by: blito_loco
3 Replies

6. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

7. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

8. Shell Programming and Scripting

Problem with arrays in awk

Hello! I'm trying to make a script that will make a list of the files in a source tree and sort them by size. Problem is I've run into a weird problem. print array will give me numbers like 160, 220, 444 that i don't even know where they come from, and print array will give me the correct numbers... (5 Replies)
Discussion started by: Glauco
5 Replies

9. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies

10. Shell Programming and Scripting

Error only when multiplying two numbers

Hi $ a=10 ; b=2 $ expr $a + $b 12 $ expr $a - $b 8 $ expr $a / $b 5 $ expr $a * $b expr: syntax error Any idean why I am getting this error only when multiplying two numbers. Whats the exact syntax? Thanks a lot to all in advance CSaha (5 Replies)
Discussion started by: csaha
5 Replies
Login or Register to Ask a Question