Steven
Steven

Reputation: 9

How do I properly allocate memory in my C program?

I am writing a Windows program in C for a homework assignment and I am running into a problem that causes my program to crash with program.exe has stopped working. I believe that this is due to the memory not being allocated correctly.

The program is supposed to start multiple threads to perform a task, I have found an example on MSDN on creating threads. I have added parts of the code into my program.

My program:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <Windows.h>

#define MAX_THREADS 4
#define BUFFER_SIZE 65000

DWORD WINAPI SomeFunction( LPVOID lpParam );

char fileBuffer[BUFFER_SIZE];

typedef struct MyData {
    int val1;
    int val2;
} MYDATA, *PMYDATA;

int main(int argc, char *argv[])
{
    int i = 0;
    int j = 0;
    PMYDATA pDataArray[MAX_THREADS];
    DWORD   dwThreadIdArray[MAX_THREADS];
    HANDLE  hThreadArray[MAX_THREADS]; 


        for (i; i < MAX_THREADS; i++)
        {
            pDataArray[i] = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                    sizeof(MYDATA));

            if( pDataArray[i] == NULL )
            {
               // If the array allocation fails, the system is out of memory
               // so there is no point in trying to print an error message.
               // Just terminate execution.
                ExitProcess(2);
            }

            // Create the thread to begin execution on its own.
            hThreadArray[i] = CreateThread(NULL, 0, SomeFunction, pDataArray[i], 0, &dwThreadIdArray[i]);

            if (hThreadArray[i] == NULL)
            {
                printf("%s\n", "Error creating thread!");
                ExitProcess(3);
            }

        }

        for (j; j < MAX_THREADS; j++)
        {
            printf("%s%d\n", "j=", j);
            WaitForSingleObject(hThreadArray[j], INFINITE);
        }

        //WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
        i = 0;
        for(i; i<MAX_THREADS; i++)
        {
            CloseHandle(hThreadArray[i]);
            if(pDataArray[i] != NULL)
            {
                HeapFree(GetProcessHeap(), 0, pDataArray[i]);
                pDataArray[i] = NULL;    // Ensure address is not reused.
            }
        }

        printf("%s\n", "DONE!");

        return 0;

}

DWORD WINAPI SomeFunction( LPVOID lpParam)
{
    PMYDATA pDataArray;
    int anotherInt;

    anotherInt = pDataArray->val1; // PROBLEM OCCURS HERE!

    printf("%s%d\n", "Printing int ", anotherInt);


    return 0;
}

The program above should be able to start multiple threads which execute SomeFunction(). I have isolated bug to this function, specifically the line anotherInt = pDataArray->val1;. pdataArray is an array of MyData defined in a struct and each element is passed into a thread.

Did I not allocate the memory for the array correctly? If not, how would I access the members of the struct that was passed in as the parameter to SomeFunction()? I have gone over my code a couple of times and could not find anything wrong that I know of. The example I followed on MSDN is here.

Upvotes: 0

Views: 101

Answers (1)

MSalters
MSalters

Reputation: 179779

In MyFunction, PMYDATA pDataArray; doesn't magically become equal to the pDataArray in main. It's an uninitialized pointer, and pDataArray->val1; tries to write to a random memory location.

Hint: you also have a LPVOID lparam which you ignore.

Upvotes: 2

Related Questions