我有一个家庭作业,要求我使用缓冲区溢出调用函数而不显式调用它.代码基本上是这样的:
#include <stdio.h>
#include <stdlib.h>
void g()
{
printf("now inside g()!\n");
}
void f()
{
printf("now inside f()!\n");
// can only modify this section
// cant call g(), maybe use g (pointer to function)
}
int main (int argc, char *argv[])
{
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然我不知道该怎么办.我想改变程序计数器的返回地址,以便它直接进入g()的地址,但我不知道如何访问它.无论如何,提示将是伟大的.
我更喜欢在任何地方使用引用,但是当你使用STL容器时,你必须使用指针,除非你真的想按值传递复杂的类型.我觉得很难转换回参考,这似乎是错误的.
是吗?
澄清...
MyType *pObj = ...
MyType &obj = *pObj;
Run Code Online (Sandbox Code Playgroud)
这不是'脏',因为你可以(即使只是在理论上,因为你先检查它)取消引用一个NULL指针?
编辑:哦,你不知道对象是否是动态创建的.
const void *和之间有什么区别void *?在什么情况下可以将void指针强制转换为const void指针?
我正在学习golang,当我阅读描述Structures的章节时,我遇到了不同的方法来初始化结构.
p1 := passport{}
var p2 passport
p3 := passport{
Photo: make([]byte, 0, 0),
Name: "Scott",
Surname: "Adam",
DateOfBirth: "Some time",
}
fmt.Printf("%s\n%s\n%s\n", p1, p2, p3)
Run Code Online (Sandbox Code Playgroud)
虽然这些打印结构的值为
{ }
{ }
{ Scott Adam Some time}
,下面的代码用&符号打印,因为它是一个引用.
pointerp1 := &p3
fmt.Printf("%s", pointerp1)
pointerp2 := new(passport)
pointerp2.Name = "Anotherscott"
fmt.Printf("%s", pointerp2)
Run Code Online (Sandbox Code Playgroud)
&{ Scott Adam Some time}&{ Anotherscott }
请帮助我解决疑惑.
在用法中pointerp1 := &p3,pointerp1是引用变量to p3,它保存实际数据.同样,保存数据的实际变量是pointerp2什么?
使用这些不同类型的初始化的最佳方案是什么?
有谁知道为什么Stroustrup的风格是指针的放置如下?具体来说,Stroustrup 就此事提供了哪些指导?
int* p;
Run Code Online (Sandbox Code Playgroud)
对比
int *p;
Run Code Online (Sandbox Code Playgroud)
因为声明多个变量需要在每个变量名称旁边加上星号。这将导致:
int* p, *x;
Run Code Online (Sandbox Code Playgroud)
对比
int *p, *x;
Run Code Online (Sandbox Code Playgroud)
在 K&R C 书中,他们解释说星号/指针用作助记符以帮助理解。我觉得奇怪的是,指针/星号与类型相关联,而不是每个示例的第二个显示的变量。有兴趣了解为什么选择第一种样式的背景。
希望能从 Stroustrup 那里得到一些引述来解释这一点。
我正在添加 K&R C 第二版语法第 235 页,其中星号(指针)与声明符相关联,声明符是一个标识符。
答案 在Stroustrup 的这篇关于编码风格的文章中。他解释说两者都是有效的,这取决于程序员的偏好。
我不同意这是一个基于意见的问题。Stroustrup 的文章在没有意见的情况下清楚地回答了这个问题。
昨天,有人给我看了这个代码:
#include <stdio.h>
int main(void)
{
unsigned long foo = 506097522914230528;
for (int i = 0; i < sizeof(unsigned long); ++i)
printf("%u ", *(((unsigned char *) &foo) + i));
putchar('\n');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致:
0 1 2 3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
我很困惑,主要是for循环中的行。据我所知,似乎&foo是被强制转换为 anunsigned char *然后被i. 我觉得*(((unsigned char *) &foo) + i)是一个更详细的书写方式((unsigned char *) &foo)[i],但是这使得它看起来像foo,一个unsigned long被索引。如果是这样,为什么?循环的其余部分似乎是典型的打印数组的所有元素,所以一切似乎都表明这是真的。演员unsigned char *阵容让我更加困惑。我试图寻找有关转换的整数类型,以char *对谷歌而言,但我的研究得到了一些后无用的搜索结果停留约铸造int …
c pointers casting char-pointer implementation-defined-behavior
我写了一个简单的,工作的俄罗斯方块游戏,每个块作为类单块的一个实例.
class SingleBlock
{
public:
SingleBlock(int, int);
~SingleBlock();
int x;
int y;
SingleBlock *next;
};
class MultiBlock
{
public:
MultiBlock(int, int);
SingleBlock *c, *d, *e, *f;
};
SingleBlock::SingleBlock(int a, int b)
{
x = a;
y = b;
}
SingleBlock::~SingleBlock()
{
x = 222;
}
MultiBlock::MultiBlock(int a, int b)
{
c = new SingleBlock (a,b);
d = c->next = new SingleBlock (a+10,b);
e = d->next = new SingleBlock (a+20,b);
f = e->next = new SingleBlock (a+30,b);
}
Run Code Online (Sandbox Code Playgroud)
我有一个扫描完整行的函数,并运行删除相关的块的链接列表并重新分配 - >下一个指针. …
以下程序编译:
template <const int * P>
class Test{};
extern const int var = 42; //extern needed to force external linkage
int main()
{
Test<&var> test;
}
Run Code Online (Sandbox Code Playgroud)
然而,这个没有,这对我来说是一个惊喜:
template <const int * P>
class Test{};
extern const int var = 42; //extern needed to force external linkage
extern const int * const ptr = &var; //extern needed to force external linkage
int main()
{
Test<ptr> test; //FAIL! Expected constant expression.
}
Run Code Online (Sandbox Code Playgroud)
替代示例:
int main()
{
const int size = 42;
int …Run Code Online (Sandbox Code Playgroud) 我有以下文件结构:
车型/ db.go
type DB struct {
*sql.DB
}
var db *DB
func init() {
dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
DB_USER, DB_PASSWORD, DB_NAME)
db, err := NewDB(dbinfo)
checkErr(err)
rows, err := db.Query("SELECT * FROM profile")
checkErr(err)
fmt.Println(rows)
}
func NewDB(dataSourceName string) (*DB, error) {
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return &DB{db}, nil
}
Run Code Online (Sandbox Code Playgroud)
车型/ db_util.go
func (p *Profile) …Run Code Online (Sandbox Code Playgroud) pointers ×10
c++ ×5
c ×4
go ×2
c++-faq ×1
c++11 ×1
casting ×1
char-pointer ×1
coding-style ×1
const ×1
conventions ×1
implementation-defined-behavior ×1
operators ×1
reference ×1
stack-trace ×1
stl ×1
struct ×1
syntax ×1