c++ - memcpy() giving me seg faults -


my program opengl program compiles , runs fine when dont use memcopy, when function used in program gives me seg fault when try run program after compilation, still compiles in both cases gives me seg fault when compile program function, , seg fault when checked in gdb not show memcpy problem initialization function init() creates opengl context me, weird thing happens when include memcpy in program , compile it, otherwise init() works fine , tested in file confirm works on own

i dont know why doing it, did notice has happened since linux mint upgraded packages, program worked fine before upgrade

here program source code

#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 data format in opengl , 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 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 array comparison     int = 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 point on there change , 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;  } 

when run gdb problem gives , despite when comment out memcpy works fine , doesnt give me seg faults

program received signal sigsegv, segmentation fault. 0x0000000000402876 in main () @ untitled.cpp:22 22     init(width,height); (gdb)  

the answer @barmakshemirani pointed out linux has stack limit of 8mb , since 2 arrays on 12mb hence overwrite stack , hence why problem occured me, , solution write to/allocate heap instead malloc()


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -