以下代码片段在执行strncpy函数时以异常结束:
#define MAX_FILENAME_LEN 127
typedef struct {
unsigned long nameLength;
char name[MAX_FILENAME_LEN + 1];
} filestructure;
char *fileName;
strncpy( fileName, filestructure->name, MAX_FILENAME_LEN );
*( fileName + MAX_FILENAME_LEN+1 ) = 0;
Run Code Online (Sandbox Code Playgroud)
Ayone知道什么可能出错?在文件结构中,我有一个50个字符长的文件名,所以它在界限内...我真的有点迷失了可能导致这个简单代码的问题...
我有一个函数来计算几个不同的值:
int ASPfile::get_dimensions(int* Lat, int* Lon,
vector<double>* Latgrid, vector<double>* Longrid) {
_latsize = get_latsize();
_lonsize = get_lonsize();
Lat = &_latsize;
Lon = &_lonsize;
latgrid = read_latgrid();
longrid = read_longrid();
*Latgrid = latgrid;
*Longrid = longrid;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该函数调用如下:
int* Latsize = NULL;
int* Lonsize = NULL;
vector<double>* Latgrid = NULL;
vector<double>* Longrid = NULL;
int res = asp->get_dimensions(Latsize,Lonsize,Latgrid,Longrid);
Run Code Online (Sandbox Code Playgroud)
然后,我尝试访问如下值:
cout << (*Szagrid)[4];
Run Code Online (Sandbox Code Playgroud)
或者像这样
cout << Szagrid->at(4);
Run Code Online (Sandbox Code Playgroud)
该程序编译时没有任何警告.但是,当我尝试访问get_dimensions()应该"填充"的指针时,valgrind会向我显示以下内容:
==10531== Invalid read of size 8
==10531== at 0x633D5DA: std::vector<double, std::allocator<double> >::operator=(std::vector<double, …Run Code Online (Sandbox Code Playgroud) 我正在尝试对代码进行一些重构,并遇到了问题.该程序有一个数据管理器,它返回指向结构数组的指针作为void*.其中一种新类型的数据,而不是指向结构数组的单个指针,有两个指向数组的指针.问题是所有处理代码都是通过访问所有记录类型共有的array [index] .qwTimestamp和array [index] .snSample来完成的.
我认为像下面这样覆盖数组访问运算符([])可能会解决问题:
class ADRec {
public:
ADRec(unsigned __int64* ts, __int32* data, unsigned index = 0): mTimestamps(ts), mDataPoints(data), mIndex(index) {
qwTimeStamp = mTimestamps[mIndex];
snSample = mDataPoints[mIndex];
}
ADRec operator[](unsigned i) {
return ADRec(mTimestamps, mDataPoints, i);
}
unsigned __int64 qwTimeStamp;
__int32 snSample;
private:
unsigned __int64* mTimestamps;
__int32* mDataPoints;
unsigned mIndex;
};
Run Code Online (Sandbox Code Playgroud)
如果您使用对象,此方法可以正常工作:
unsigned __int64 ts[] = { 2, 3, 4, 5};
__int32 data[] = {4, 6, 8, 10};
ADRec tmp = ADRec(ts, data, 0);
ASSERT(tmp[0].qwTimeStamp == 2);
ASSERT(tmp[0].snSample …Run Code Online (Sandbox Code Playgroud) 如果构造函数Door看起来像这样:
Door::Door(Doorknob doorknob) : m_doorknob(doorknob) { }
Run Code Online (Sandbox Code Playgroud)
然后你会实例化Door这样的:
Doorknob doorknob;
Door door(doorknob); // Does an object copy of doorknob occur here?
Run Code Online (Sandbox Code Playgroud)
看起来如果你存储Doorknob为指针,你可以明确地避免副本:
Door::Door(Doorknob * doorknob_ptr) : m_doorknob_ptr(doorknob_ptr) { }
Run Code Online (Sandbox Code Playgroud)
实例化Door如下:
Door door(new Doorknob);
Run Code Online (Sandbox Code Playgroud)
但是现在你必须确保delete doorknob内部Door的析构函数,这似乎是丑陋的.
什么是首选方法?
有人可以为我解释以下代码段吗?
// Bind base object so we can compute offsets
// currently only implemented for indexes.
template<class DataObj> void BindAsBase(DataObj &rowbuf)
{
// Attempting to assign working_type first guarantees exception safety.
working_type = DTL_TYPEID_NAME (rowbuf);
working_addr = reinterpret_cast<BYTE*>(&rowbuf);
working_size = sizeof(rowbuf);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是sizeof(rowbuf)的结果是什么?它是DataObj的长度还是Byte*的长度?为什么?
另一个问题:为什么需要计算指针的偏移量?它的常用用途是什么?
sizeof(working_addr)等于什么?
我刚刚开始练习c ++而且我一度陷入困境.我有一个Node类,类有一个像这样的构造函数:
class Node
{
public:
Node(std::string,Node *,int,int,int,int);
private:
std::string state;
Node* parent_node;
int total_cost;
int path_cost;
int heuristic_cost;
int depth;
}
Node::Node(std::string state,Node *parent_node,int path_cost,int heuristic_cost,int total_cost,int depth)
{
this->state=state;
this->parent_node=parent_node;
this->path_cost=path_cost;
this->heuristic_cost=heuristic_cost;
this->total_cost=total_cost;
this->depth=depth;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常,但我无法使用NULL parent_node创建Node对象.我试过这个:
Node *n = new Node("state name",NULL,0,15,20,1);
Run Code Online (Sandbox Code Playgroud)
我也尝试创建一个新对象并将其指定为parent_node,但也没有成功.
Node *temp = new Node();
Node *n = new Node("state name",temp,0,15,20,1);
Run Code Online (Sandbox Code Playgroud)
我做错了指针,但我不知道我错过了什么.我收到一个编译错误,说没有匹配的函数调用.
提前致谢
在C中,我们可以在变量前放置"&"来计算出该变量的地址.我有一台32位机器.每当我在控制台中打印地址时,控制台都会显示一个7位数的基数10.我只是想知道这个数字(10 ^ 7)与32位机器有什么关系.(2 ^ 32)谢谢
我正在学习C++并编写二叉搜索树.以下是我为insert方法编写的代码.
BSTNode * BST::Insert(const std::string & v) {
BSTNode *n = !root ? root = new BSTNode(v) : Insert_Helper(v,root);
if(n) size++;
return n;
}
BSTNode * BST::Insert_Helper(const std::string & v, BSTNode *n) {
if(!n->value.compare(v))
return NULL; // already have v
else if(n->value.compare(v) > 0) // v goes to the left
if(n->left) return Insert_Helper(v,n->left);
else return n->left = new BSTNode(v);
else // v goes to the right
if(n->right) Insert_Helper(v,n->right);
else return n->right = new BSTNode(v);
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是这样的:它一切正常和花花公子,直到我尝试插入一个重复的节点.它不会添加新节点,但会增加计数.
通过在GDB中观察,我发现当我尝试添加我已经拥有的字符串时,Insert_Helper正常工作并返回NULL.然而,这个值(在我的机器上)就像0x6,当然超出界限,但不像我想的那样是0x0.我认为这会导致我有if(n)语句的问题.在这种情况下,n的计算结果为true,因此增加的大小比它应该增加一个.
此外,在我的程序中的这一点,节点继续正确添加,但我的插入函数继续返回0x6作为地址,即使它们确实在我可以访问的内存中的有效位置. …
#include<stdio.h>
#include "amicablenumber.h"
int i,j;
struct amicable
{
int **amicablePair;
int size;
};
main()
{
int startnum = 250;
int endnum = 1000;
struct amicable* ami;
ami = getAmicablePairs(startnum, endnum);
printf("{");
for(int i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
}
printf("}");
}
amicable *getAmicablePairs(int startnum,int endnum)
{
int size=0;
int sumfactors(int);
amicable record;
for(i=startnum;i<=endnum;i++)
{
for(j=endnum;j>=startnum;j--)
{
if((sumfactors(i)==j)&&(sumfactors(j)==i) && (i!=j))
{
record.amicablePair[size][0]=i;
record.amicablePair[size][1]=j;
size++;
}}}
record.size=size;
return record;
}
int sumfactors(int number)
{
int sum=0;
for(i=1;i<number;i++)
{
if(number%i==0) …Run Code Online (Sandbox Code Playgroud) 这有我想要的功能(它的工作原理)
#include <stdio.h>
//includes other libraries needed
int foo();
int main()
{
while(true)
{
while(foo()==1)
{
//do something
}
//does other unrelated things
}
}
int foo()
{
// Returns if a switch is on or off when the function was called
// on return 1;
// off return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,我希望这种情况发生:
#include <stdio.h>
//includes other libraries needed
int foo();
int main()
{
while(true)
{
//THIS IS THE PROBLEM
int something = foo();
while(something==1)
{
//do something
}
//does other …Run Code Online (Sandbox Code Playgroud)