memcpy()给了我seg错误
memcpy() giving me seg faults
我的程序是OpenGL程序,当我不使用memcopy时,它会编译并运行良好,但当程序中使用此函数时,当我在编译后尝试运行程序时,它给了我一个seg错误,在这两种情况下,它仍然会编译,但当我使用此函数编译程序时,会给我一个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 %fn",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时,它工作得很好,不会给我任何seg错误
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402876 in main () at untitled.cpp:22
22 init(WIDTH,HEIGHT);
(gdb)
@BarmakShemirani指出的答案是,linux的堆栈限制为8mb,由于两个数组在一起超过12mb,因此会覆盖堆栈,因此我会遇到问题,解决方案是用malloc()写入/分配到堆
@BarmakShemirani关于堆栈限制是正确的。您也可以使用setrlimit来增加堆栈限制。在代码中(但最好不在主函数中)。
相关文章:
- C++ - 将元素按升序插入数组的 SEG 错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 字符串上的合并排序上的 Seg 错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- g++ 9.2.1 (Linux) 会导致 seg 错误,但 Windows 上的代码块不会
- 函数抛出seg错误,我不知道为什么
- Microsoft Visual Studio 2019交叉编译CMake库 - 有效,但存在SEG错误
- 由于相同的文件名/类名存在于不同的SO中而导致的SEG错误
- 尝试访问 3D 矢量中的元素时出现 Seg 错误
- 垫子对象数组太大会导致 Seg 错误
- 为什么push_back成矢量<矢量<int>>导致 seg 错误?
- 在向量中存储字符串存在 SEG 错误
- 更改本地类名时出现 seg 错误
- 使用 ->GetString( " ") 时出现 Seg 错误,它位于单独的类中
- 将字符数组转换为 std::string 以传递到 std::bitset seg 错误
- 字符和 int 连接何时会导致 seg 错误?
- 静态数组溢出堆栈(seg 错误 11)仅在类定义内部时,否则不会..?
- .EOF 在读取文件时导致 Seg 错误:FIXXED
- 在 qt5 中使用 QSharedPointer 时出现 Seg 错误
- 为什么我的 if 语句会出现 seg 错误?