pointers going wild?

M

Michael

Hello everyone here,

I've got a question according to a C program,
but I'm not sure if this one is "off-topic".

So, its a program using two structures each containing
an array (a.array1[][] b.array2[][])
Both arrays are of type double and of size 2x11.
I access the structs via pointers (p_a, p_b).
If I use
p_a->array1[0][0] = 3.0
in
p_b->array2[0][2]
the value also changes to 3.0. The first attemp I made
was invoking gdb and look for the addresses of both
arrays:
its 0x401020 (gdb> p &(p_a->array1[0][0]) )
and 0x400020 (gdb> p &(p_b->array2[0][2]) ).

But somehow they are linked to each other. Even if I
change the value of the variable within the debugger
I effect both arrays.

As I said it may be not the right newsgroup, but the program
is written in C and maybe you've got some hints to solve
or tackle this problem.

Thanks,
Michael
 
G

Grumble

Michael said:
So, its a program using two structures each containing
an array (a.array1[][] b.array2[][])
Both arrays are of type double and of size 2x11.

What is the complete definition of the structure?

How are a and b defined?

How are p_a and p_b defined?

Can you provide the shortest possible source code for
a program exhibiting the bug you see?
 
M

Martin Ambuhl

Michael said:
Hello everyone here,

I've got a question according to a C program,
but I'm not sure if this one is "off-topic".

So, its a program using two structures each containing
an array (a.array1[][] b.array2[][])
Both arrays are of type double and of size 2x11.
I access the structs via pointers (p_a, p_b).
If I use
p_a->array1[0][0] = 3.0
in
p_b->array2[0][2]
the value also changes to 3.0. The first attemp I made
was invoking gdb and look for the addresses of both
arrays:
its 0x401020 (gdb> p &(p_a->array1[0][0]) )
and 0x400020 (gdb> p &(p_b->array2[0][2]) ).

But somehow they are linked to each other. Even if I
change the value of the variable within the debugger
I effect both arrays.

As I said it may be not the right newsgroup, but the program
is written in C and maybe you've got some hints to solve
or tackle this problem.

Frankly, I don't believe you. Below is, as best as I can decode your
description, code that you claim would do this. It doesn't. Please
post a minimal program that demonstrates your problem instead of trying
to translate your code into narrative prose.

#include <stdio.h>

struct foo1
{
double array1[2][11];
};
struct foo2
{
double array2[2][11];
};

int main(void)
{
struct foo1 a = { {{0}} }, *p_a = &a;
struct foo2 b = { {{0}} }, *p_b = &b;

printf("before assignment,\n"
"p_a->array1[0][0] = %g\n"
"p_b->array2[0][2] = %g\n\n",
p_a->array1[0][0], p_b->array2[0][2]);
p_a->array1[0][0] = 3.0;
printf("after assignment,\n"
"p_a->array1[0][0] = %g\n"
"p_b->array2[0][2] = %g\n\n",
p_a->array1[0][0], p_b->array2[0][2]);
return 0;
}



before assignment,
p_a->array1[0][0] = 0
p_b->array2[0][2] = 0

after assignment,
p_a->array1[0][0] = 3
p_b->array2[0][2] = 0
 
M

Michael

Ok, you are right

so lets go on with more code.

typedef struct sStruct1
{
// [some others, no pointers]
double d[2][11];
} tStruct1;


typedef struct sStruct2
{
// [some others, no pointers]
double e[2][11];
} tStruct2;

[...]
tStruct1 *p_a;
tStruct2 *p_b;

p_a = malloc( sizeof (tStruct1) );
p_b = malloc( sizeof (tStruct2) );
// !:see remark below

[...]
p_a->d[0][0] = 100;
// smash! also effect the other array at
// p_b->e[0][2]

Remark:
Well the maloc isn't done exactly like that, but I cannot
deliver the code. Its not because it has to be secret
its just because I get a lib, which I have to use -->
off topic i know.
But even if I cannot deliver the code, the different addresses
printed by the debugger confuse me. Am I take it wrong? is
there one more (or less) redirection I miss? 'Cause I thought
different addresses don't effect each other.

Thanks,
Michael
 
M

Michael

Ok,

sorry for the time you spend on my problem,
it was a mistake in the memory allocation done
in a external lib. Whew, not my mistake.

The memory problem is indeed system dependend
the pocesses do not see the correct address its
a fake and remapped by the OS.

Thanks and soory again to anybody thinking about
this problem.

After it is solved I'm in good mood again,

Michael
 
A

Al Bowers

Michael said:
Ok, you are right

so lets go on with more code.

typedef struct sStruct1
{
// [some others, no pointers]
double d[2][11];
} tStruct1;


typedef struct sStruct2
{
// [some others, no pointers]
double e[2][11];
} tStruct2;

[...]
tStruct1 *p_a;
tStruct2 *p_b;

p_a = malloc( sizeof (tStruct1) );
p_b = malloc( sizeof (tStruct2) );
// !:see remark below

[...]
p_a->d[0][0] = 100;
// smash! also effect the other array at
// p_b->e[0][2]

Remark:
Well the maloc isn't done exactly like that, but I cannot
deliver the code. Its not because it has to be secret
its just because I get a lib, which I have to use -->
off topic i know.
But even if I cannot deliver the code, the different addresses
printed by the debugger confuse me. Am I take it wrong? is
there one more (or less) redirection I miss? 'Cause I thought
different addresses don't effect each other.

It's unfortunate that you cannot deliber the code. The
information you have provided may not provide an adequate clue.
Checking the code you need to make sure the allocations are checked for
success. And when you say, "smash!": what do you mean?
This is a concern because you do not show the arrays being
initialized or assigned any values after allocation.

Run this code on your implementation. The output should be:

Before Changing:
p_a->d[0][0] = 0.0
p_b->e[0][2] = 0.0

After Changing:
p_a->d[0][0] = 100.0
p_b->e[0][2] = 0.0

The code:

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

typedef struct sStruct1
{
/* [some others, no pointers] */
double d[2][11];
} tStruct1;


typedef struct sStruct2
{
/* [some others, no pointers] */
double e[2][11];
} tStruct2;

tStruct1 init1;
tStruct2 init2;

int main(void)
{
tStruct1 *p_a;
tStruct2 *p_b;

p_a = malloc( sizeof (tStruct1) );
p_b = malloc( sizeof (tStruct2) );

if(p_a && p_b)
{
*p_a = init1; /* put initial values in the array */
*p_b = init2;
printf("Before Changing:\n"
"\tp_a->d[0][0] = %.1f\n"
"\tp_b->e[0][2] = %.1f\n",
p_a->d[0][0],p_b->e[0][2]);

p_a->d[0][0] = 100.0 ;
/* DOES IT smash! also effect the other array at
p_b->e[0][2] ? */

printf("\nAfter Changing:\n"
"\tp_a->d[0][0] = %.1f\n"
"\tp_b->e[0][2] = %.1f\n",
p_a->d[0][0],p_b->e[0][2]);
}
else puts("Memory allocation error");
free(p_a);
free(p_b);
return 0;
}
 
D

dam_fool_2003

Michael said:
Ok, you are right

so lets go on with more code.

typedef struct sStruct1
{
// [some others, no pointers]
double d[2][11];
} tStruct1;


typedef struct sStruct2
{
// [some others, no pointers]
double e[2][11];
} tStruct2;

[...]
tStruct1 *p_a;
tStruct2 *p_b;

p_a = malloc( sizeof (tStruct1) );
p_b = malloc( sizeof (tStruct2) );
// !:see remark below

[...]
p_a->d[0][0] = 100;
// smash! also effect the other array at
// p_b->e[0][2]

Remark:
Well the maloc isn't done exactly like that, but I cannot
deliver the code. Its not because it has to be secret
its just because I get a lib, which I have to use -->
off topic i know.
But even if I cannot deliver the code, the different addresses
printed by the debugger confuse me. Am I take it wrong? is
there one more (or less) redirection I miss? 'Cause I thought
different addresses don't effect each other.

Thanks,
Michael

There must be an error in your code ,a typo. May be you have written
the same variables name two times. If not then I could be a
pointer-sharing problem.

BTW: If you have found the error then don't forget to post it in the
news group.
 
M

Martin Ambuhl

Michael said:
Ok, you are right

so lets go on with more code.

typedef struct sStruct1
{
// [some others, no pointers]
double d[2][11];
} tStruct1;


typedef struct sStruct2
{
// [some others, no pointers]
double e[2][11];
} tStruct2;

[...]
tStruct1 *p_a;
tStruct2 *p_b;

p_a = malloc( sizeof (tStruct1) );
p_b = malloc( sizeof (tStruct2) );
// !:see remark below

[...]
p_a->d[0][0] = 100;
// smash! also effect the other array at
// p_b->e[0][2]

Remark:
Well the maloc isn't done exactly like that, but I cannot
deliver the code. Its not because it has to be secret
its just because I get a lib, which I have to use -->
off topic i know.
But even if I cannot deliver the code, the different addresses
printed by the debugger confuse me. Am I take it wrong? is
there one more (or less) redirection I miss? 'Cause I thought
different addresses don't effect each other.

I _still_ don't believe you:

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

typedef struct sStruct1
{
double d[2][11];
} tStruct1;

typedef struct sStruct2
{
double e[2][11];
} tStruct2;

int main(void)
{
tStruct1 *p_a;
tStruct2 *p_b;

if (!(p_a = malloc(sizeof *p_a))) {
fprintf(stderr, "p_a not allocated, aborting.\n");
exit(EXIT_FAILURE);
}
if (!(p_b = malloc(sizeof *p_b))) {
fprintf(stderr, "p_b not allocated, aborting.\n");
free(p_a);
exit(EXIT_FAILURE);
}

*p_a = (tStruct1) { { { 0} } };
*p_b = (tStruct2) { { { 0} } };

printf("before assignment,\n"
"p_a->d[0][0] = %g\n"
"p_b->e[0][2] = %g\n\n", p_a->d[0][0], p_b->e[0][2]);
p_a->d[0][0] = 100;
printf("after assignment,\n"
"p_a->d[0][0] = %g\n"
"p_b->e[0][2] = %g\n\n", p_a->d[0][0], p_b->e[0][2]);
free(p_a);
free(p_b);
return 0;
}



before assignment,
p_a->d[0][0] = 0
p_b->e[0][2] = 0

after assignment,
p_a->d[0][0] = 100
p_b->e[0][2] = 0
 

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,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top