免责声明:这是一个家庭作业,但问题不在于作业,只是一般语法怪异.
我试图在一个更大的程序的上下文中使用libpcap,但是当我尝试获取每个数据包的数据包头和数据时,gcc抱怨pcap_next_ex的第三个参数是一个不兼容的指针类型.这是一些示例代码,看看我在说什么:
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
int main()
{
pcap_t *pcap;
char pcapErr[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr *pktHeader;
u_char *pktData;
pcap = pcap_open_offline("somefile.pcap", pcapErr);
if (pcap == NULL)
{
fprintf(stderr, "pcap_open_offline failed: %s\n", pcapErr);
exit(EXIT_FAILURE);
}
while (pcap_next_ex(pcap, &pktHeader, &pktData) == 1)
{
// do things here
}
pcap_close(pcap);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
手册页指出pcap_next_ex()的原型是:
int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, const u_char **pkt_data)
Run Code Online (Sandbox Code Playgroud)
我传递的是一种不兼容的指针类型到底是怎么回事?谢谢.
我最近编写了一个程序来帮助我理解C++中内存指针的基础知识,我选择了一个简单的素数查找器.
我终于开始工作了.(对于调试!)
我让它跑去看它走了多远,它得到了#815389,我的详细告诉我是65076的素数,我得到了一个应用程序崩溃.我能想到的一件事是我的注意力溢出,所以我把它们变成了多头,它被困在同一个地方.
有人能够帮助解释造成这种限制的限制吗?
comp:WinVista 64位家庭高级版,6GB内存AMD 4800+ X2程序崩溃,内存使用量为4,664K
资源:
#include <cstdlib>
#include <iostream>
\\\\(Backslashes added for readability)
using namespace std;
long number;
long numnum;
class num;
class num {
public:
long i;
void check();
bool nxt;
num* nxtnum;
};
void num::check() {
if (number % i != 0) {
if (nxt == true) {
(*nxtnum).check();
} else {
nxtnum = new num();
(*nxtnum).i = number;
numnum++;
cout << numnum << ":" << number << ", ";
nxt = true;
}; …Run Code Online (Sandbox Code Playgroud) 我有一个函数,它接受一个对象的引用:
void move(Ball& ball);
Run Code Online (Sandbox Code Playgroud)
我有另一个函数调用'move()',它有一个指向ball的指针:
void foo(Ball* ball){
//call move()
}
Run Code Online (Sandbox Code Playgroud)
foo()应该如何将球传给move()?
应该是这样的:
move(*ball);
Run Code Online (Sandbox Code Playgroud)
要么:
move(ball);
Run Code Online (Sandbox Code Playgroud)
要么:
move(&ball);
Run Code Online (Sandbox Code Playgroud) 好的,所以我有两个类,在代码中按顺序称它们为A和B. B类将A类实例化为数组,B类也有一个错误消息char*变量,A类必须在发生错误时设置.我创建了一个带有纯虚函数的第三个类,用于在B中设置errorMessage变量,然后使B成为该第三个类的子元素.A类创建一个指向第三个类的指针,称之为C - 当B初始化A对象的数组时,它循环遍历它们并调用A中的函数将A的指针设置为C--它将"this"传递给该函数,然后A将指向C的指针设置为"this",并且由于C是B的父级,A可以设置C-> errorMessage(我必须这样做,因为A和B在编译时不能同时意识到对方时间).
但是,无论如何它工作正常,当我将命令行参数传递给main(int,char**)时,除非我将七个,八个或十二个以上的参数传递给它,否则它会起作用...我将其缩小(通过注释)出来的行代码,在A中,它将指针指向C,设置为由B传递给它的值.这对我没有意义......我怀疑是内存泄漏或其他什么,但它似乎错了,我不知道如何修复它...另外我不明白为什么特别是七,八,十二个以上的参数不起作用,1-6和9-12工作正常.
这是我的代码(剥离) -
//class C
class errorContainer{
public:
virtual ~errorContainer(){ }
virtual void reportError(int,char*)=0;
};
//Class A
class switchObject{
void reportError(int,char*);
errorContainer* errorReference;
public:
void bindErrorContainer(errorContainer*);
};
//Class A member function definitions
void switchObject::reportError(int errorCode,char* errorMessage){
errorReference->reportError(errorCode,errorMessage);
}
void switchObject::bindErrorContainer(errorContainer* newReference){
errorReference=newReference; //commenting out this line fixes the problem
}
//Class B
class switchSystem: public errorContainer{
int errorCode;
char* errorMessage;
public:
switchSystem(int); //MUST specify number of switches in this system.
void reportError(int,char*);
int errCode();
char* …Run Code Online (Sandbox Code Playgroud) 如果我有:
function init(t,y,u)
{
alert(t + " " + y + " " + u);
}
// String.prototype.add = init(5, 6, 7); // 1)
// window.onload = init(5,6,7); // 2)
Run Code Online (Sandbox Code Playgroud)
在1)init中将执行然后它指针被指定String.prototype.add
但在2)该函数只执行一次...但为什么不是两次也onload引发事件?
谢谢
指针为重载分辨率提出了一些特殊问题.
比如说,
void f(int* x) { ... }
void f(char* x) { ...}
int main()
{
f(0);
}
Run Code Online (Sandbox Code Playgroud)
调用f(0)有什么问题?如何修复f(0)的函数调用?
当使用sprintf将指针复制到字符串时,我有一些代码堆栈转储.我试图将动物的内容复制到一个名为output的新指针数组中.但是,我得到一个堆栈转储.
输出应该是以下内容:新动物兔新动物马新动物驴
我正确地走这条路吗?
非常感谢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void p_init(const char **animals, char **output);
int main(int argc, char **argv)
{
char *animals[] = {"rabbit", "horse", "donkey", '\0'};
char **prt_animals = animals;
char *output[sizeof(*animals)];
/* print the contents here */
while(*prt_animals)
{
printf("Animal: %s\n", *prt_animals++);
}
/* copy and update in the output buffer */
p_init(*&animals, *&output);
getchar();
return 0;
void p_init(const char **animals, char **output)
{
while(*animals)
{
sprintf(*output, "new animal %s", *animals);
*output++;
}
}
Run Code Online (Sandbox Code Playgroud) 我可以期望用户空间程序中的任何"数据"指针与地址0和0xffffffff ...保持安全距离,这样我可以安全地向指针添加一个小偏移而不检查溢出吗?当p是指向常量字符缓冲区或动态分配字符串的char指针(在现代> = 32位操作系统上)时,我可以安全地假设p + n不会溢出的最大正n是多少?
为了避免混淆:我在谈论溢出检查,而不是边界检查.例如:如果你有一个带有m个字符的字符串开头的指针p,并且你想要访问正偏移量i的字符,那么你需要检查i <m或者你可以间接检查p + i < p + m.但是,在后一种情况下,您还必须确保p + i不会溢出,即您必须确保p + i> = p.
更新:好的,如果i> m,p + i无效标准C,无论p + i是否实际被解除引用或是否溢出.然而,我真正感兴趣的问题是,是否存在一个小的n,p + n 在实践中不会溢出.回答这个问题显然需要一些关于现代操作系统如何组织地址空间的知识.
Update2:听说任何一个特定的平台已经非常有趣了,即使它不具有推广性.优选地,不是一些模糊的嵌入式嵌入式.x86或基于Power的32位Win,Linux和Mac将是最有趣的.
由于我对将指针类型内存分配给指针的过分理解,以下原因导致对barrier_create的调用出现总线错误("hi"从不打印).
typedef struct barrier barrier_t;
typedef struct barrier *barrier_p;
barrier_p test_barrier_p;
int main(int argc, char *argv[]) {
barrier_create(*test_barrier_p);
}
int barrier_create(barrier_p *barrier_pointer) {
printf("hi\n");
barrier_p old_barrier, new_barrier;
int count;
old_barrier = (barrier_p) *barrier_pointer;
new_barrier = (barrier_p) malloc(sizeof(*new_barrier));
count = pthread_mutex_init(&new_barrier->lock, NULL);
new_barrier->is_valid = VALID_BARRIER;
new_barrier->counter = 0;
new_barrier->release_flag = 0;
*barrier_pointer = new_barrier;
return HAPPY_HAPPY_JOY_JOY;
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么或输入错误?
这个版本正在运行.我在//整个代码中都发表了评论,以便更好地说明我遇到的问题.该程序依赖于读取文本文件.包含标点符号的段落格式.
可以将此和上述内容复制到文本文件中并运行该程序.
// Word.cpp
#define _CRT_SECURE_NO_WARNINGS // disable warnings for strcpy
#define ARRY_SZ 100
#include <iostream>
#include <fstream>
#include "Word.h"
using namespace std;
Word::Word( const char* word )
{
ptr_ = new char[ strlen( word ) + 1 ];
strcpy( ptr_, word );
len_ = strlen( ptr_ );
}
Word::Word( const Word* theObject )
{
ptr_ = theObject->ptr_;
len_ = theObject->len_;
}
Word::~Word()
{
delete [] ptr_;
ptr_ = NULL;
}
char Word::GetFirstLetterLower()
{
// I want to …Run Code Online (Sandbox Code Playgroud) pointers ×10
c++ ×6
c ×4
arrays ×1
bus-error ×1
class ×1
copy ×1
crash ×1
function ×1
inheritance ×1
javascript ×1
malloc ×1
memory-leaks ×1
overflow ×1
overloading ×1
pcap ×1
reference ×1
struct ×1