gpt4 book ai didi

c++ - memcpy() 给我段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:04 24 4
gpt4 key购买 nike

我的程序是一个 OpenGL 程序,当我不使用 memcopy 时编译和运行良好,但是当在程序中使用这个函数时,当我尝试在编译后运行程序时它给我一个段错误,它仍然编译案例,但是当我用这个函数编译程序时给我 seg 错误,并且在 gdb 中检查时的 seg 错误没有显示 memcpy 作为问题而是一个初始化函数 init() 为我创建了 opengl 上下文,但奇怪的是是只有当我在程序中包含 memcpy 并编译它时才会发生,否则 init() 工作正常,我还在另一个文件中测试它以确认它自己工作

我不知道为什么会这样,我确实注意到自从 linux mint 升级了一些软件包后就发生了这种情况,程序在升级前运行良好

这是程序源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glew.h>
#include <GL/glx.h>

#include <time.h>
#include <math.h>

#include "glx2.c"
#include "renderBuffer.h"
#include "new.hpp"

#define WIDTH 1080
#define HEIGHT 720

int main(){

init(WIDTH,HEIGHT);
createShaders();

char name[1][25];

float returned[720][1080][2] = {0.0f};


strcpy(name[0],"ryu2.bmp");

GLuint frameBuffer = createFramebuffer(frameBuffer);

GLuint renderTexture = createRenderTexture(renderTexture, WIDTH,HEIGHT);
//GLuint depth_texture;

//glGenTextures(1, &depth_texture);
//glBindTexture(GL_TEXTURE_2D, depth_texture);
//glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1080, 720);

//glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,depth_texture, 0);

glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,renderTexture,0);

static const GLenum draw_buffers[] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, draw_buffers);


//bind framebuffer
glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
checkFramebuffer();

GLfloat vertices[] = {

//
// X Y U V
//triangle 1
-1.0, -1.0, 0.0, 0.0,
-22.0/27.0, -1.0, 100.0/800.0, 0.0,
-1.0, -41.0/60.0, 0.0, 114.0/342.0,
-22.0/27.0, -41.0/60.0, 100.0/800.0, 114.0/342.0};


GLuint vao1 = createVao();
GLuint vbo1 = createVbo();

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint tex = createTexture(name[0]);

//set up data format in opengl and save in vao
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), 0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(GLfloat), (const GLvoid*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);


bindObject(vao1, tex);

glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glViewport(0,0, WIDTH,HEIGHT);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

//////completed drawing to framebuffer original values

glBindFramebuffer(GL_FRAMEBUFFER,0);

glBindTexture(GL_TEXTURE_2D, renderTexture);

glGetTexImage(GL_TEXTURE_2D, 0,GL_RG,GL_FLOAT,(void *)&returned);

float another[720][1080][2];

memcpy((void *)another, (const void *)returned, sizeof(returned));
//----------------------completed copying original values to array for all comparison

int i = 0,j=0;

//for(j=0; j<114;j++)
// for(i=0; i<100;i++){
//printf("%f %f\n",another[j][i][0], another[j][i][1]);
//}



//from this point on there will be change and comparison

//GLuint disp = glGetUniformLocation(shader_program, "disp");

//glUniform2f(disp, 1.0/540,1.0/360);




glXMakeCurrent( dpy, 0, 0 );
glXDestroyContext( dpy, ctx );
glXDestroyWindow(dpy, glxWin);
XDestroyWindow( dpy, win );
XFreeColormap( dpy, cmap );
XCloseDisplay( dpy );

return 0;

}

当我运行 gdb 时,这是它给出的问题,尽管当我注释掉 memcpy 时它工作正常并且没有给我任何段错误

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402876 in main () at untitled.cpp:22
22 init(WIDTH,HEIGHT);
(gdb)

最佳答案

@BarmakShemirani 指出的答案是 linux 的堆栈限制为 8mb,并且由于两个数组加在一起超过 12mb,因此会覆盖堆栈,因此问题发生在我身上,解决方案是写入/用 malloc() 代替分配给堆

关于c++ - memcpy() 给我段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37760004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com