在c中,假设你有以下结构和一个实例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
typedef struct _a {
int *a1;
float *a2;
char *a3;
}a;
a b;
b.a1 = (int *) malloc(sizeof(int) * 10);
b.a2 = (float *) malloc(sizeof(float) * 10);
b.a3 = (char *) malloc(sizeof(char) * 10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,如何找到分配/与变量b关联的总内存?sizeof(b)将返回结构中指针的组合大小,但不会计算使用malloc为不同类型/大小分配的内存总和.如何在内存中找到此结构的总大小(包括填充,如果适用)?
为什么以下代码编译:
int main()
{
int j = 1;
int *jp = &j;
cout << "j is " << j << endl;
cout << "jp is " << jp << endl;
cout << "*jp is " << *jp << endl;
cout << "&j is " << &j << endl;
cout << "&jp is " << &jp << endl;
}
Run Code Online (Sandbox Code Playgroud)
但不是吗?
#include <iostream>
using namespace std;
int main()
{
int j = 1;
int *jp;
*jp =& j; // This is the …Run Code Online (Sandbox Code Playgroud) 我有这个类ChessBoard,这是它的标题:
class ChessBoard
{
Field** board;
Color currentColor;
public:
ChessBoard();
ChessBoard(const ChessBoard&);
Field* findField(std::string);
ChessBoard& operator = (const ChessBoard&);
bool checkIfFieldHasFigure(std::string);
void writeOneState(SahApi*);
void playAllMoves(std::istream*, SahApi*);
void playMove(std::string);
~ChessBoard();
};
Run Code Online (Sandbox Code Playgroud)
我有它的构造函数,它创建了棋盘的开始阶段:(非常可怕,我知道:))
ChessBoard::ChessBoard()
{
int i, j;
Error err;
board = new Field*[8];
if(!board)
{
err.writeToOutput(1);
exit(1);
}
for(i = 0; i < 8; i++)
{
board[i] = new Field[8];
if(!board[i])
{
err.writeToOutput(1);
exit(1);
}
}
currentColor = WHITE;
char c;
std::string s;
Figure f;
for(i = 0; i < 8; …Run Code Online (Sandbox Code Playgroud) 一个简短的问题主要是在这个例子中理解指针如何与数组一起工作:
char *lineptr[MAXLENGTH]
Run Code Online (Sandbox Code Playgroud)
现在我明白这与char **lineptr数组本身就是一个指针是一样的.
我的问题是它如何以不同的形式/去引用状态工作,例如:
lineptr
*lineptr
**lineptr
*lineptr[]
Run Code Online (Sandbox Code Playgroud)
在每个状态中,发生了什么,每个州在代码中做什么/工作?
任何帮助深表感谢!
以下两个代码片段编译时没有任何错误/警告,但在运行时崩溃.请赐教.
计划1
int main( )
{
char *p= "Hello" ;
*p = 'B' ;
printf("\n%s",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计划2
int main( )
{
char *p= "Hello" ;
Char *q="mug"
*q = *p ;
printf("\n%s",q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于程序2,我预计输出为'Hug'.
看下面的程序
int main()
{
char p[3]="bug";
puts(p);
char *a=p;
puts(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于第一次投注,我得到正确的输出(即)"bug"
但是对于第二次投注,我输出为"bug↓"
我正在为我正在研究的linux程序编写一些代码,需要libssh.我正在查看他们的教程页面,我看到它通过ssh_options_set()引用传递所有参数.这是为什么?
#include <libssh/libssh.h>
#include <stdlib.h>
int main()
{
ssh_session my_ssh_session;
int verbosity = SSH_LOG_PROTOCOL;
int port = 22;
my_ssh_session = ssh_new();
if (my_ssh_session == NULL)
exit(-1);
ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "localhost");
ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); //here
ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); //here
...
ssh_free(my_ssh_session);
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些元素的结构,我在循环中释放了这个结构的内存,大致如下:
for (i = 0; i < teller; i++) {
free((glycan+i)->desc);
}
free(glycan)
Run Code Online (Sandbox Code Playgroud)
我假设指针仍然指向空的内存块,因此我想将它们设置为NULL,如下所示:
for (i = teller; i > 0; i--) {
(glycan+i)->desc = NULL;
}
glycan = NULL;
Run Code Online (Sandbox Code Playgroud)
然而Valgrind告诉我一些我不太懂的东西:
==11783== Invalid write of size 4
==11783== at 0x8048F49: main (spectral_matcher.c:122)
==11783== Address 0x431c070 is 72 bytes inside a block of size 28,000 free'd
==11783== at 0x4027C02: free (vg_replace_malloc.c:366)
==11783== by 0x8048F2C: main (spectral_matcher.c:121)
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释为什么会发生这种警告/错误以及我应该采取哪些不同的方法来解决它?
编辑:我知道我在释放后将指针设置为NULL,释放只将内存标记为空闲,因此指针仍然完整(如果我没有记错),我随后希望将其设置为NULL.
我的源文件似乎给我一个错误,我不明白为什么它是我得到这个错误,因为它似乎有点徒劳.我还想了解的是我能做些什么才能达到同样的效果.我现在将省略细节,但如图所示,我希望这是一个全局对象指针.
码
#include "functions.h"
App::Game* game = new Game;
void display_func() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
game->render_objects();
glFlush();
}
void init(int argc, char** argv) {
glutInit(&argc, argv);
game->init_window();
glutDisplayFunc(display_func);
game->init();
glutMainLoop();
}
Run Code Online (Sandbox Code Playgroud)
编译错误
functions.cpp:3:23: error: expected type-specifier before ‘Game’
functions.cpp:3:23: error: cannot convert ‘int*’ to ‘App::Game*’ in initialization
Run Code Online (Sandbox Code Playgroud)
我知道我无法在头文件中初始化它,所以我有什么选择?
#include <iostream>
using namespace std;
class C {
public:
int isSelf (C& param);
};
bool C::isSelf (C& param)
{
if (¶m == this) return true;
else return false;
}
int main () {
C a;
C* b = &a;
cout << boolalpha << b->isSelf(a) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效.但在我看来 b->isSelf(a)应该真的是b -> isSelf(&a)因为isSelf期望一个类型的地址C?!
[编辑]其他问题:
1)有没有办法isSelf使用pass by value 实现这个功能?2)实现是否通过引用传递并通过指针传递正确?
bool C::isSelf1(const C &c)
{
if (&c == this) { …Run Code Online (Sandbox Code Playgroud)