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 8 1399
Michael wrote: 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?
Michael wrote: 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
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
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
Michael wrote: 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;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
Michael <mi****@gmx.net> wrote in message news:<c4**********@rzcomm2.rz.tu-bs.de>... 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.
Michael wrote: 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 da***********@yahoo.com wrote: BTW: If you have found the error then don't forget to post it in the news group.
+From: Michael <mi****@gmx.net>
Newsgroups: comp.lang.c
Subject: Please Close! -- I was stupid
Date: Thu, 01 Apr 2004 13:46:34 +0200
Message-ID: <c4**********@rzcomm2.rz.tu-bs.de>
References: <c4**********@rzcomm2.rz.tu-bs.de> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gary |
last post by:
I've always taught that arrays are not pointers. So how come I can do:
#include <iostream>
#include <cstdlib>
using namespace std;
int main( )
{
int myArray={1, 10};
cout <<...
|
by: Paweł |
last post by:
Hello!
I'm looking for efficient code or site where I can find code for finding one
string in another string. String which I search should have "wild"
characters like '?' for any one char and...
|
by: Michael |
last post by:
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...
|
by: David Scarlett |
last post by:
Is the following code safe?
SomeType *a, *b;
/* Some code.... */
free(a);
|
by: Felix Kater |
last post by:
Hi,
when accessing the variables in a struct: What's the reason why in C
you have -> and . instead of only . ? Are there cases in which the
compiler couldn't figure out what to do?
Felix
|
by: Yevgen Muntyan |
last post by:
6.2.4 of standard says:
"The value of a pointer becomes indeterminate when
the object it points to reaches the end of its lifetime."
Do I understand it right that value of pointer may
or may...
|
by: Scotty |
last post by:
I'm a C++ novice and need help figuring out some odd behavior I've
encountered.
Here's the situation: I create a class and have its constructor store a
random number in a private "number"...
|
by: smp |
last post by:
Does anyone know why making a vector of pointers is so much less efficient
than a vector of objects? For a simple example:
int num = 20;
vector<int*v_int_ptr;
v_int_ptr.reserve(num);
...
|
by: Horacius ReX |
last post by:
Hi,
I have the following program structure:
main
.....
int A=5;
int* ptr= A; (so at this point ptr stores address of A)
.....
.....
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |