why does my array get bigger?

This is a discussion on why does my array get bigger? within the C Programming forums, part of the General Programming Boards category; here is a C program for windows Code: #include <windows.h> #include <gl/gl.h> #include <stdlib.h> #include <stdio.h> LRESULT CALLBACK WindowProc(HWND, UINT, ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    7

    why does my array get bigger?

    here is a C program for windows
    Code:
    #include <windows.h>
    #include <gl/gl.h>
    #include <stdlib.h>
    #include <stdio.h>
    LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
    void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
    void DisableOpenGL(HWND, HDC, HGLRC);
    int true=1;
    struct block
    {
    float x;
    float y;
    int exists;
    float trecy;
    };
    
    struct block *block_array;
    
    void add_to_array(struct block array_pointer[],struct block*obj_pointer)
    {
        float array_size=sizeof(&array_pointer);
        printf("array size is %f",array_size);
        float obj_size=sizeof(&obj_pointer);
        printf("object size is %f",obj_size);
    
    }
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine,
                       int nCmdShow)
    {
        WNDCLASSEX wcex;
        HWND hwnd;
        HDC hDC;
        HGLRC hRC;
        MSG msg;
        BOOL bQuit = FALSE;
        float theta = 0.0f;
    
        /* register window class */
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style = CS_OWNDC;
        wcex.lpfnWndProc = WindowProc;
        wcex.cbClsExtra = 0;
        wcex.cbWndExtra = 0;
        wcex.hInstance = hInstance;
        wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wcex.lpszMenuName = NULL;
        wcex.lpszClassName = "GLSample";
        wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
    
    
        if (!RegisterClassEx(&wcex))
            return 0;
    
        /* create main window */
        hwnd = CreateWindowEx(0,
                              "GLSample",
                              "OpenGL Sample",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              800,
                              600,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        ShowWindow(hwnd, nCmdShow);
    
        /* enable OpenGL for the window */
        EnableOpenGL(hwnd, &hDC, &hRC);
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        glClearColor(0, 0, 0, 0);
        glOrtho (0,800,600 , 0, 0, 1);
        glDisable(GL_DEPTH_TEST);
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.375, 0.375, 0);
        glEnable(GL_BLEND);
        glEnable(GL_TEXTURE_2D);
       glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
       srand(1);
    block_array = malloc(1 * sizeof(struct block));
    
       add_to_array(block_array,&block_array[0]);
       float x=0;
       float y=0;
                int block_iter=0;
                for (x=40;x<800;x=x+80)
                {
                for (y=30;y<600;y=y+60)
                {
                   block_array[block_iter].exists=true;
                   block_array[block_iter].x=x;
                   block_array[block_iter].y=y;
                   block_iter++;
                }
                }
        /* program main loop */
        while (!bQuit)
        {
            /* check for messages */
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                /* handle or dispatch messages */
                if (msg.message == WM_QUIT)
                {
                    bQuit = TRUE;
                }
                else
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
            else
            {
                /* OpenGL animation code goes here */
    
                glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                glClear(GL_COLOR_BUFFER_BIT);
    
                //glPushMatrix();
                //glRotatef(theta, 0.0f, 0.0f, 1.0f);
                float x=0;
                float y=0;
                int block_iter=0;
                for (x=40;x<800;x=x+80)
                {
                for (y=30;y<600;y=y+60)
                {
    
                 glLoadIdentity();
        glTranslatef(0.375, 0.375, 0);
        glTranslatef(block_array[block_iter].x,block_array[block_iter].y,0);
        glRotatef(block_array[block_iter].x,0,0,1);
    
                glBegin(GL_QUADS);
    
                    glColor3f(.3f, .3f, .3);
                       glVertex2f(-40,-30);
                    glColor3f(.60f, .60f, .60f);
                    glVertex2f(40, -30);
                    glColor3f(0.9f, 0.9f, 0.9f);
                    glVertex2f(40,30);
                    glColor3f(0.60f, 0.60f, 0.60f);
                    glVertex2f(-40,30);
    
                glEnd();
                block_iter++;
                }
                }
    
                //?//glPopMatrix();
    
                SwapBuffers(hDC);
    
                theta += 1.0f;
                Sleep (1);
            }
        }
    
        /* shutdown OpenGL */
        DisableOpenGL(hwnd, hDC, hRC);
    
        /* destroy the window explicitly */
        DestroyWindow(hwnd);
    
        return msg.wParam;
    }
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
            case WM_CLOSE:
                PostQuitMessage(0);
            break;
    
            case WM_DESTROY:
                return 0;
    
            case WM_KEYDOWN:
            {
                switch (wParam)
                {
                    case VK_ESCAPE:
                        PostQuitMessage(0);
                    case 0x41: //a
                        block_array[1].y++;//'=block_array[10].y+20.0;
                        printf("%f\n",block_array[10].y);
                        break;
                    case 0x51: //q
    
                    break;
                }
            }
            break;
    
            default:
                return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
    
        return 0;
    }
    
    void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
    {
        PIXELFORMATDESCRIPTOR pfd;
    
        int iFormat;
    
        /* get the device context (DC) */
        *hDC = GetDC(hwnd);
    
        /* set the pixel format for the DC */
        ZeroMemory(&pfd, sizeof(pfd));
    
        pfd.nSize = sizeof(pfd);
        pfd.nVersion = 1;
        pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        pfd.iPixelType = PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PFD_MAIN_PLANE;
    
        iFormat = ChoosePixelFormat(*hDC, &pfd);
    
        SetPixelFormat(*hDC, iFormat, &pfd);
    
        /* create and enable the render context (RC) */
        *hRC = wglCreateContext(*hDC);
    
        wglMakeCurrent(*hDC, *hRC);
    }
    
    void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
    {
        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(hRC);
        ReleaseDC(hwnd, hDC);
    }
    its supposed to crash because the array is only 1 element long, if you comment out the call to 'add_to_array' it will crash as its supposed to.
    Why does add_to_array grow the array?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by slenkar
    its supposed to crash because the array is only 1 element long, if you comment out the call to 'add_to_array' it will crash as its supposed to.
    No, it's supposed to result in undefined behavior and that's exactly what it's doing. Quit trying to figure out why undefined behavior is behaving unpredictably and just fix the problem.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    thanks for the reply

    what is wrong with the add_to_array function that would cause this behaviour?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,976
    Quote Originally Posted by slenkar
    what is wrong with the add_to_array function that would cause this behaviour?
    You say that your program is supposed to crash. Therefore, the root of the problem is not the add_to_array function: it is with what you want.

    The add_to_array function looks wrong, certainly. It is a misnomer, and then it does not do even what it claims to do in its output, i.e., despite bring named add_to_array, it does not add to any array, and despite printing text with "array size" and "object size" labels, it does not actually print any array size or object size (other than pointer sizes). But these are wrong in a different way from what you are asking.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    yes, originally I wanted to write a function that added an object to the first empty space in the array, or if the array was full...create a new larger array and copy the objects to the new larger array (and dispose of the old one)

    so then , just to make sure I understood the language I decided to reduce the array size to 1 and then it didnt crash so I got worried.

    What Im worried about is 'the future' what If I make a mistake and I am not warned...instead the program will do 'undefined behaviour' and I wont have a clue what is going wrong.

    It would help me to know what is causing the error in the (badly named) function

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by slenkar View Post
    What Im worried about is 'the future' what If I make a mistake and I am not warned...instead the program will do 'undefined behaviour' and I wont have a clue what is going wrong.

    It would help me to know what is causing the error in the (badly named) function
    That's true. Troubleshooting memory management errors is notoriously difficult. There are things out there like Valgrind that help make it a little easier though.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    I am guessing that the sizeof is not returning what you expect.

    I created a test program below that my help if the above is the problem.

    Tim S.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct block
    {
     float x;
     float y;
     int exists;
     float trecy;
    };
    
    struct block *block_array;
    
    int main()
    {
    
        block_array = malloc(1 * sizeof(struct block));
    
        printf("sizeof: %d \n", sizeof(struct block));
        printf("sizeof block_array: %d \n", sizeof(block_array));
        printf("sizeof block_array: %d \n", sizeof(*block_array));
        printf("sizeof block_array: %d \n", sizeof(*block_array)/sizeof(struct block));
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21