不同的变量使用相同的内存 [英] Different Variables Use Same Memory

查看:107
本文介绍了不同的变量使用相同的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Redhat版本的Linux和GNU C ++。


我不清楚这是Linux问题还是C ++问题。我没有这个问题在Windows上运行相同的程序没有这个问题但

调整C ++代码似乎解决了Linux上的问题。


我有一个在一个类中私有声明的静态数组和一个在另一个类中公开声明的

结构。该程序使用

两个类。我正在获取核心转储并将问题追溯到

这个数组与内存共享相同内存的事实因此,当写入数组元素时,结构被覆盖了
。当我将静态数组的声明更改为public时,我发现问题似乎消失了。但是,我不确定

我理解所涉及的问题。事实上,我并不是很清楚

将变量声明为私有而不是公开的优点,除了不想让某人错误地修改变量之外,还有b $ b。 br />
然而,似乎将它声明为私有使得程序前进并且无论如何都会错误地改变它。


我也应该指出静态数组是私有声明的类,具有publicaly声明结构的类

作为其类对象之一。我不知道为什么编译后的代码

会让他们使用相同的内存。


任何对此/这些问题的澄清将不胜感激。


谢谢,

彼得。

I am using the Redhat version of Linux and GNU C++.

It is not clear to me whether this is a Linux issue or a C++ issue. I
do not have this problem running the same program on Windows but
tweaking the C++ code appears to fix the problem on Linux.

I have a static array that is declared privately in one class and a
structure that is declared publicly in another class. The program uses
both classes. I was getting core dumps and traced the problem down to
the fact that the array shares the same memory as the structure so
that, when an array element is written to, the structure is
overwritten. I found that the problem appears to go away when I change
thet declaration of the static array to public. However, I am not sure
I understand the issues involved. In fact, I am not really clear of
the advantages of declaring a variable as private as opposed to public,
apart from not wanting someone to modify the variable erroneously.
However, it appears that declaring it as private makes the program go
ahead and erroneously change it anyway.

I should also point out that the class where the static array is
declared privately, has the class with the publicaly declared structure
as one of its class objects. I have no idea why the compiled code
would make them use the same memory.

Any clarification of this/these issues would be greatly appreciated.

Thanks,
Peter.

推荐答案

Ma ********** @ excite.com 写道:
Ma**********@excite.com wrote:

我使用的是Redhat版本的Linux和GNU C ++。


我不清楚这是Linux问题还是C ++问题。我没有这个问题在Windows上运行相同的程序没有这个问题但

调整C ++代码似乎解决了Linux上的问题。


我有一个在一个类中私有声明的静态数组和一个在另一个类中公开声明的

结构。该程序使用

两个类。我正在获取核心转储并将问题追溯到

这个数组与内存共享相同内存的事实因此,当写入数组元素时,结构被覆盖了
。当我将静态数组的声明更改为public时,我发现问题似乎消失了。但是,我不确定

我理解所涉及的问题。事实上,我并不是很清楚

将变量声明为私有而不是公开的优点,除了不想让某人错误地修改变量之外,还有b $ b。 br />
然而,似乎将它声明为私有使得程序前进了
并且无论如何都错误地改变了它。
I am using the Redhat version of Linux and GNU C++.

It is not clear to me whether this is a Linux issue or a C++ issue. I
do not have this problem running the same program on Windows but
tweaking the C++ code appears to fix the problem on Linux.

I have a static array that is declared privately in one class and a
structure that is declared publicly in another class. The program uses
both classes. I was getting core dumps and traced the problem down to
the fact that the array shares the same memory as the structure so
that, when an array element is written to, the structure is
overwritten. I found that the problem appears to go away when I change
thet declaration of the static array to public. However, I am not sure
I understand the issues involved. In fact, I am not really clear of
the advantages of declaring a variable as private as opposed to public,
apart from not wanting someone to modify the variable erroneously.
However, it appears that declaring it as private makes the program go
ahead and erroneously change it anyway.



变量是私有的这一事实限制了你必须看到的地方

,当你试图弄清楚你做错了什么导致数组写

来踩踏结构。这在我的书中是一个巨大的好处。

The fact that the variable is private limits the places you have to look
when trying to figure out what you did wrong to cause the array writes
to stomp on the structure. That''s a huge benefit in my book.


我还应该指出静态数组的类是私有声明的b $ b,具有publicaly声明结构的类

作为其类对象之一。我不知道为什么编译后的代码

会让他们使用相同的内存。


任何对此/这些问题的澄清将不胜感激。
I should also point out that the class where the static array is
declared privately, has the class with the publicaly declared structure
as one of its class objects. I have no idea why the compiled code
would make them use the same memory.

Any clarification of this/these issues would be greatly appreciated.



他们使用相同的内存因为您正在访问其中一个或两个

错误。它似乎消失了,因为当你把一个移到

public时你正在更改它们的顺序的类的部分

在内存中布局,因此你覆盖了内存的其他部分

而不是你注意到的特定结构你需要做什么才能找到你跳到阵列边界的地方

并修复那段代码。


一种解决方案是用向量替换数组并使用

at成员函数或创建某种已检查的数组类,

在你写的时候提醒你。


-

要给我发电子邮件,请输入sheltie;在主题中。

They "use the same memory" because you are accessing one or both of them
incorrectly. It seems to go away because when you move one to the
"public" section of the class you are changing the order that they are
laid out in memory, thus you are overwriting some other part of memory
instead of the particular structure you noticed the problem in. What you
need to do is find out where you are jumping the bounds of your array
and fix that code.

One solution would be to replace the array with a vector and use the
"at" member-function or create some sort of checked array class that
alerts you when you write outside of it.

--
To send me email, put "sheltie" in the subject.


这几乎必须是编译器或环境问题

而不是语言或编程问题(除非

有些东西你没告诉我们。)


即便如此,尝试创建最小的

程序可能会有用展示问题并进行分析。


Steve
This almost has to be a compiler or environment problem
rather than a language or programming problem (unless
there is something you''re not telling us).

Even so it might be useful to try to create the minimal
program that exhibits the problem and analyze that.

Steve




MajorSetb ... @ excite。 com写道:

MajorSetb...@excite.com wrote:

我使用的是Redhat版本的Linux和GNU C ++。


我不清楚是否这是Linux问题或C ++问题。我没有这个问题在Windows上运行相同的程序没有这个问题但

调整C ++代码似乎解决了Linux上的问题。


我有一个在一个类中私有声明的静态数组和一个在另一个类中公开声明的

结构。该程序使用

两个类。我正在获取核心转储并将问题追溯到

这个数组与内存共享相同内存的事实因此,当写入数组元素时,结构被覆盖了
。当我将静态数组的声明更改为public时,我发现问题似乎消失了。但是,我不确定

我理解所涉及的问题。事实上,我并不是很清楚

将变量声明为私有而不是公开的优点,除了不想让某人错误地修改变量之外,还有b $ b。 br />
然而,似乎将它声明为私有使得程序前进并且无论如何都会错误地改变它。


我也应该指出静态数组是私有声明的类,具有publicaly声明结构的类

作为其类对象之一。我不知道为什么编译后的代码

会让他们使用相同的内存。


任何对此/这些问题的澄清将不胜感激。


谢谢,

彼得。
I am using the Redhat version of Linux and GNU C++.

It is not clear to me whether this is a Linux issue or a C++ issue. I
do not have this problem running the same program on Windows but
tweaking the C++ code appears to fix the problem on Linux.

I have a static array that is declared privately in one class and a
structure that is declared publicly in another class. The program uses
both classes. I was getting core dumps and traced the problem down to
the fact that the array shares the same memory as the structure so
that, when an array element is written to, the structure is
overwritten. I found that the problem appears to go away when I change
thet declaration of the static array to public. However, I am not sure
I understand the issues involved. In fact, I am not really clear of
the advantages of declaring a variable as private as opposed to public,
apart from not wanting someone to modify the variable erroneously.
However, it appears that declaring it as private makes the program go
ahead and erroneously change it anyway.

I should also point out that the class where the static array is
declared privately, has the class with the publicaly declared structure
as one of its class objects. I have no idea why the compiled code
would make them use the same memory.

Any clarification of this/these issues would be greatly appreciated.

Thanks,
Peter.



你是如何初始化静态数组的?

为什么数组是静态的?

为什么可以''你提供最少的代码吗?


#include< iostream>


A级

{

static int narray [10];

public:

static int get(int index){return A :: narray [index]; }

static void set(int index,int val){narray [index] = val; }

};


int A :: narray [] = {0,1,2,3,4,5,6,7,8, 9}; //初始化数组


int main()

{

A a;

a。 set(5,50);

for(size_t index = 0; index< 10; ++ index)

{

std: :cout<< a.get(index)<< std :: endl;

}

返回0;

}


/ *

0

1

2

3

4

50

6

7

8

9

* /

How did you initialize the static array?
Why is the array static?
Why can''t you provide minimal code?

#include <iostream>

class A
{
static int narray[10];
public:
static int get(int index) { return A::narray[index]; }
static void set(int index, int val) { narray[index] = val; }
};

int A::narray[] = { 0,1,2,3,4,5,6,7,8,9 }; // initialized array

int main()
{
A a;
a.set(5, 50);
for( size_t index = 0; index < 10; ++index)
{
std::cout << a.get(index) << std::endl;
}
return 0;
}

/*
0
1
2
3
4
50
6
7
8
9
*/


这篇关于不同的变量使用相同的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆