我想要一个简单的函数,它接收一个字符串并在一些解析后返回一个字符串数组.所以,这是我的功能签名:
int parse(const char *foo, char **sep_foo, int *sep_foo_qty) {
int i;
char *token;
...
strcpy(sep_foo[i], token); /* sf here */
...
}
Run Code Online (Sandbox Code Playgroud)
然后我称之为:
char sep_foo[MAX_QTY][MAX_STRING_LENGTH];
char foo[MAX_STRING_LENGTH];
int sep_foo_qty, error;
...
error = parse(foo, sep_foo, &sep_foo_qyt);
...
Run Code Online (Sandbox Code Playgroud)
这样我在编译期间会收到警告:
warning: passing argument 2 of 'parse' from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
然后在标记为/*sf的行中执行期间出现分段错误*/
我的C代码有什么问题?
提前致谢
我有一些关于C++的基本问题.考虑以下代码,我尝试返回一个字符串.
const std::string&
NumberHolder::getValueString() {
char valueCharArray[100];
sprintf_s(valueCharArray,"%f",_value);
std::string valueString(valueCharArray);
return valueString;
}
Run Code Online (Sandbox Code Playgroud)
我试图返回一个字符串,其中包含一个名为_value的类成员的值.但是我收到警告,我正在尝试传回指向局部变量的指针.这当然是件坏事.如果我在这一点上对C++有足够的了解,这意味着我传回的指针在有人试图使用它时就已经有了对它的调用.所以我修改:
const std::string&
NumberHolder::getValueString() {
char valueCharArray[100];
sprintf_s(valueCharArray,"%f",_value);
std::string valueString = new std::string(valueCharArray);
return (*valueString);
}
Run Code Online (Sandbox Code Playgroud)
这应该在堆栈上创建一个指针,该指针将在此函数之外生存.这里有两个问题:1)它无论如何都不编译,我不明白为什么(错误= 无法从'std :: string*'转换为'std :: basic_string <_Elem,_Traits,_Ax>')和2 )这似乎是一个潜在的内存泄漏,因为我依赖别人来调用这个人的删除.我应该在这里使用什么模式?
我现在用C++编程,我喜欢使用指针.但似乎其他较新的语言,如Java,C#和Python,不允许您显式声明指针.换句话说,你可以不写都int x和int * y,并具有x同时是一个值y是一个指针,在任何一种语言.这背后的原因是什么?
我听说越来越多,我应该使用智能指针而不是裸指针,尽管我已经实现了有效的内存泄漏系统.
请问使用智能指针的正确编程方法是什么?它们是否真的应用,即使我检查分配的内存块上的内存泄漏?还是由我决定吗?如果我不使用它们,这可以被视为编程弱点吗?
如果强烈建议使用智能指针(例如:std :: auto_ptr),我应该使用它们而不是每个裸指针吗?
在注册对象必须具有唯一名称的系统中,我想在名称中使用/包含对象的this指针.我想要最简单的方法来创建???:
std::string name = ???(this);
我一直听到这句话,但我无法真正找到const_cast是邪恶的原因.
在以下示例中:
template <typename T>
void OscillatorToFieldTransformer<T>::setOscillator(const SysOscillatorBase<T> &src)
{
oscillatorSrc = const_cast<SysOscillatorBase<T>*>(&src);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用引用,并使用const,我保护我的引用不被更改.另一方面,如果我不使用const_cast,代码将无法编译.为什么const_cast在这里不好?
这同样适用于以下示例:
template <typename T>
void SysSystemBase<T>::addOscillator(const SysOscillatorBase<T> &src)
{
bool alreadyThere = 0;
for(unsigned long i = 0; i < oscillators.size(); i++)
{
if(&src == oscillators[i])
{
alreadyThere = 1;
break;
}
}
if(!alreadyThere)
{
oscillators.push_back(const_cast<SysOscillatorBase<T>*>(&src));
}
}
Run Code Online (Sandbox Code Playgroud)
请给我一些例子,我可以看到使用const_cast是一个坏主意/不专业.
谢谢你的任何努力:)
假设我有两个代码示例用于创建10个元素的整数数组:
int *pi = (int*)0;
realloc(pi,10);
Run Code Online (Sandbox Code Playgroud)
另一个是正常写的,即:
int *pi;
pi= malloc(10*sizeof(int));
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是:第一种类型的转让是合法的,但没有使用.为什么,虽然我可能会得到我选择的起始位置?使用常量的指针的初始化是合法的但不使用.为什么?
我无法弄清楚这一点.也许是因为凌晨2点.无论如何,我在这里不知所措.
#include <stdio.h>
int main()
{
char array[] = "123456789";
char* ptr = array;
printf("%c\n", *(ptr++));
printf("%c\n", *ptr);
*ptr = array[3];
printf("%c\n", *(ptr++));
printf("%c\n\n", *ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
1
2
4
3
Run Code Online (Sandbox Code Playgroud)
我有一个指针,我指定array.
然后我打印,我认为将是第一个索引('2'),而是得到1.- 所以,我假设它*(ptr++)实际上是取消引用,然后才增加指针.
然后我重新分配ptr第四个索引('4')并重复步骤2.这样就可以正常工作,因为我看到C在解除引用之前不会先计算括号.
然后我打印新增加ptr的显示('5')...我得到了3?
那是怎么回事,第1步和第2步以及第3步和第4步是相同的,但是我得到了不同的结果?
我假设当我们写:(arr[i]相当于*(arr+i))时会发生内部强制转换.因为i例如可以是一个short,int或long或任何这些三个无符号的变体.
所以我的问题很简单:哪种类型应该i不会发生内部转换?这样代码可以最有效地运行?
粗猜:size_t?
我试图将成员函数指针传递给c风格的函数(因为它是C中的lib)
它想要的指针定义为:
void (*)(int, const char*)
Run Code Online (Sandbox Code Playgroud)
所以我试图传递的功能是:
void Application::onError(int error, const char *description)
Run Code Online (Sandbox Code Playgroud)
我试图通过这段代码传递:
setCallback(bind(&Game::onError, this, placeholders::_1, placeholders::_2));
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
cannot convert ‘std::_Bind_helper<false, void (Application::*)(Application*, int,
const char*), Application* const, const std::_Placeholder<1>&, const
std::_Placeholder<2>&>::type {aka std::_Bind<std::_Mem_fn<void (Application::*)
(Application*, int, const char*)>(Application*, std::_Placeholder<1>,
std::_Placeholder<2>)>}’ to ‘GLFWerrorfun {aka void (*)(int, const char*)}’ for
argument ‘1’ to ‘void (* glfwSetErrorCallback(GLFWerrorfun))(int, const char*)’
glfwSetErrorCallback(bind(&Application::onError, this, placeholders::_1, placeholders::_2));
Run Code Online (Sandbox Code Playgroud)
有没有办法成功将成员函数作为绑定函数传递给c风格的函数?
pointers ×10
c++ ×6
c ×5
arrays ×1
c++11 ×1
callback ×1
casting ×1
const ×1
const-cast ×1
parameters ×1
performance ×1
reference ×1
stl ×1
string ×1
visual-c++ ×1