在函数内 malloc 全局变量是错误的吗?
我有两个 C++ 的“简单”问题。
1-我想创建一个由类中的某些方法共享的变量,所以我所做的就是在头文件中声明该变量并使其成为该类的全局变量。因此,所有方法都可以访问它。这是正确的做法吗?
2-按照我的第一点,如果我在标头中将变量 N
声明为 double *N
。然后在我的一个方法中,我知道
N = (double*) malloc (sizeof(double)*50);
N
将在函数内部进行内存分配。我的问题是:这个内存是只在函数内部保留还是一直保留在全局 N
中?
如果不为 N
保留内存,那么此方法就不是一个好方法,因为系统可能会在稍后的程序过程中覆盖 N
的值。
请提出建议,谢谢
编辑:感谢大家的意见和编辑我的问题。显然我错误地将我的变量称为全局变量,而它实际上是一个类成员。我认为这个声明被称为全局的,因为该变量对于类内的方法是全局的。希望下次我能问一个“更聪明”的问题:)
I have two 'simple' questions in C++.
1- I want to make a variable shared by some methods in a class, so what i am doing is declaring the variable in the header file and make it global to the class. Hence, all methods will have access to it. Is this the right way of doing it ?
2- Following my first point, if i declare variable N
as double *N
in the header. Then inside one of my methods, i do
N = (double*) malloc (sizeof(double)*50);
I know that N
will have a memory allocation inside the function. My question is: is this memory reserved only inside the function or it stays for the global N
all the time ?
If the memory won't be reserved for N
, then this method wouldn't be a good method, as the system might overwrite the values of N
later during the program.
Please kindly advice, Thank you
EDIT: Thank you all for the input and for editing my question. Apparently i mistakenly called my variable as global while it is actually a class member. I thought this declaration is called global since the variable is global for the methods inside the class. Hope i ask a 'smarter' question next time :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,避免全局变量。为您的变量找到正确的范围和封装。把它放在那里。
如果您将某些内容声明为
double
并尝试为其分配指针,则不会发生任何事情。你的代码根本无法编译。我假设您正在谈论全局double*
。您需要为该指针malloc
内存,并且与通常的malloc
一样,它不会在作用域末尾free
释放。您需要自己执行此操作,但这对于全局来说可能不太重要。我就停在这里。有这么多错误。您不应该在 C++ 中使用
malloc
。您不应该在 C++ 中使用普通数组。并且您应该尽可能避免使用全局变量。你应该用代码来说明你想要做什么。No, avoid globals. Find the right scope and encapsulation for your variable. Put it there.
If you declare something as
double
and try to assign a pointer to it, nothing is going to happen. Your code is simply not going to compile. I assume you are talking about a globaldouble*
. You'd need tomalloc
memory for this pointer and it is, as usual withmalloc
notfree
d at the end of scope. You need to do it yourself but this is probably less important for a global.I'll just stop here. There is so much wrong. You shouldn't use
malloc
in C++. You shouldn't use plain arrays in C++. And you should avoid globals as much as possible have globals. You should rather state what you are trying to do, with code.malloc
的内存将保持有效,直到您释放
它。然而,这是一个极其糟糕的设计。从技术上讲,这是合法且正确的。然而,这绝对不是正确的做法。首先,全局变量是不好的。如果您有一个设计并且其中包含全局变量,请再考虑一下,直到它不包含为止。其次,您应该使用new
、delete
和智能指针,而不是malloc
和free
。第三,就让它成为类的成员变量吧?malloc
's memory will remain valid until youfree
it. However, this is a hideously bad design. Technically, this is legal and correct. It is, however, definitely not the right way to do it. First, global variables are bad. If you have a design and it includes a global variable, think again until it doesn't. Second, you should usenew
,delete
and a smart pointer, notmalloc
andfree
. Thirdly, just make it a member variable of the class?作为一般规则,您应该避免全局变量,除非绝对没有其他解决方案可用(恕我直言,如果软件设计良好,则不应该出现这种情况)。
如果全球你指的是会员,就像这样
A级
{
私人的:
双N;
};
那么是的。
如果 global 你的意思是这样的(据我所知,这是 global 变量的正确概念)
,那么你应该避免这种情况并使用前面的(使用变量作为类 >成员)。
As a general rule, you should avoid global variables unless absolutely no other solution is available (which IMHO shouldn't be the case ever if a software is well designed).
If by global you mean as member, like this
class A
{
private:
double N;
};
then yes.
If by global you mean something like this (which AFAIK is correct concept for global variable)
then you should avoid this and use the previous (use the variable as a class member).
让我们把事情弄清楚一点。假设这是在头文件
members.h
中:请注意,像
a
或c
这样的变量/数据成员不需要显式分配。另一方面,d
确实如此,因为它是一个指针。另请注意,在 C++ 中,您使用new()
而不是malloc()
。这是源文件
members.cpp
:我希望我已经说得足够清楚并消除了您的疑虑。
Let's clear things up a bit. Assuming this is in header
members.h
:Note that variables/data members like
a
orc
do not require explicit allocation. On the other handd
does, because it is a pointer. Note also that in C++ you usenew()
rather thanmalloc()
.This is source file
members.cpp
:I hope I've been clear enough and covered your doubts.
根据我作为程序员的经验,我从未在头文件中实例化变量。
相反,我可以在 C 文件中实例化该变量,但在 EXTERN(在头文件中)以便其他类可以使用它。但如果你想声明的话,应该没问题。
当你 malloc 时,你说的是 N 有这个大小。
但是,如果您没有 malloc 它并且尝试在不同的方法中使用它,那么您将遇到分段错误,因为没有为它分配空间(这仅适用于指针和列表,数组和等等,您不需要对单一数据类型执行此操作。)
N 可以在任何地方实例化,但必须在使用之前实例化。
from my experience as a programmer, I never instantiate a variable in a header file.
Rather, I can instantiate the variable in a C file, but EXTERN (in the header file) so that other classes can use it. But if you want to declare it, it should be fine.
And when you malloc you are saying N has this size.
But if you haven't malloc it and you are trying to use it within a different method, then you will have a segmentation fault due to the fact that there is no allocated space for it (this is only for pointers and lists,arrays and etc. you dont need to do it for single data types.)
N can be instantiated anywhere, but it has to be instantiated before used.
这不是在 C++ 中构造和管理类的数据成员的惯用方法!通常,您可以按如下方式声明一个对象:
现在,
foo
中的所有方法都可以读取/修改(取决于const
ness)成员N
。您无需手动管理N
的存储,它作为foo
实例的一部分进行管理。您不需要单独的“全局”变量,除非您希望foo
的所有实例及其成员使用相同的N
。即使如此,它是动态的也是没有意义的。This is not the idiomatic way to construct and manage data members of classes in C++! Typically you would declare an object as follows:
Now all methods in
foo
can read/modify (depending onconst
ness) the memberN
. You don't need to manually manage the storage forN
, it is managed as part of the instance offoo
. You don't need a separate "global" variable unless you want all instances offoo
and it's members to use the sameN
. Even then, it doesn't make sense for it to be dynamic.