How do I properly allocate memory in my C program? - c

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.

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.

Related

Why are there two heaps in a newly created windows process?

I discovered this when playing with the standard malloc function and calling HeapWalk to see the size of the allocated block. Found out that malloc does not create blocks in the default process heap but rather in a second heap that I did not create.
Why are there two heaps? Even without calling malloc the process still has two heaps and the second one is not empty. This is the result of HeapWalk on the second heap without any call to malloc or HeapAlloc.
Also because of this behaviour of malloc another question crossed my mind. Is it correct to allocate memory on the default process heap? I never created a new heap for casual programs and did HeapAlloc(GetProcessHeap(), ...) but is this practice harmful or bad in any way?
This is the code i used for testing:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define MAX_HEAP_COUNT 10
INT APIENTRY WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
HANDLE hHeaps[MAX_HEAP_COUNT];
DWORD dwHeaps;
dwHeaps = GetProcessHeaps(MAX_HEAP_COUNT, hHeaps);
LPVOID lpDummyBlocks[20];
for(DWORD i = 0; i < 20; i++)
lpDummyBlocks[i] = malloc(rand()%1000+1);
for(DWORD i = 0; i < dwHeaps; i++)
{
SIZE_T cbSize = HeapCompact(hHeaps[i], IGNORE);
printf("Largest free block in heap %lu is %lu bytes.\n", i, cbSize);
PROCESS_HEAP_ENTRY phe;
phe.lpData = NULL;
DWORD dwTotalBlocks = 0;
while(HeapWalk(hHeaps[i], &phe))
{
printf("Found a block at address [%p]", phe.lpData);
printf(" Block size: %lu", phe.cbData);
for(DWORD i = 0; i < 20; i++)
if (phe.lpData == lpDummyBlocks[i])
{
printf(" This is DummyBlock%lu", i);
break;
}
dwTotalBlocks++;
printf("\n");
}
printf("Total blocks: %lu. \n\n", dwTotalBlocks);
}
for(DWORD i = 0; i < 20; i++)
free(lpDummyBlocks[i]);
return 0;
}
UPDATE
I played with the second heap a little bit more and noticed a strange behaviour. I am not sure if my GetHeapInfo function is correct but i think thats what MSDN said about the .cbData field of the PROCESS_HEAP_ENTRY structure.
struct tagHEAP_INFO
{
DWORD dwBlockCount;
DWORD dwTotalOverhead;
DWORD dwTotalBlockSize;
};
typedef struct tagHEAP_INFO HEAP_INFO, *LPHEAP_INFO;
WINBOOL APIENTRY GetHeapInfo(HANDLE hHeap, LPHEAP_INFO lpHeapInfo)
{
// error checking ...
// ...
// return FALSE;
PROCESS_HEAP_ENTRY phe;
phe.lpData = NULL;
lpHeapInfo->dwBlockCount = 0;
lpHeapInfo->dwTotalBlockSize = 0;
lpHeapInfo->dwTotalOverhead = 0;
while(HeapWalk(hHeap, &phe))
{
lpHeapInfo->dwBlockCount++;
lpHeapInfo->dwTotalBlockSize += phe.cbData;
lpHeapInfo->dwTotalOverhead += phe.cbOverhead;
}
DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_NO_MORE_ITEMS)
return TRUE;
Beep(1000, 1000); // well, its easier this way :)
return FALSE;
}
With this function i counted the total bytes allocated at the beginning of the process, after allocating some blocks and after freeing them. This is the strange result (and i checked it and its not an overflow):
Adding to this, i made the same test with the default process heap. I got strange results here too, this time also the block count did not match (and i checked that all the blocks were allocated).
Am i misinterpreting this? I am using HeapWalk the way this example (enumerating a heap) is showing. This time i only used HeapAlloc for allocation.

How to use critical section

Hello I would like to write a program with 2 concurrent threads. First thread writes to the array letter 'A' and second one writes 'B'. My question is how to use critical section to gain result with alternately array with only letter A and with only letter B ? Here is my code, but it is not work properly. What is wrong with it?
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <psapi.h>
#define SIZE_TAB 200
volatile char program[SIZE_TAB];
CRITICAL_SECTION CriticalSection;
DWORD WINAPI aa(void *v);
DWORD WINAPI bb(void *v);
int main(int argc, char *argv[])
{
InitializeCriticalSection(&CriticalSection);
HANDLE thread_a = CreateThread(NULL, 0, aa, 0, 0, 0);
HANDLE thread_b = CreateThread(NULL, 0, bb, 0, 0, 0);
while (1)
{
for (int i = 0; i<SIZE_TAB; i++)
printf("%c", program[i]);
Sleep(1000);
printf("\n\n");
}
DeleteCriticalSection(&CriticalSection);
CloseHandle(thread_a);
CloseHandle(thread_b);
return 0;
}
DWORD WINAPI aa(void *v)
{
EnterCriticalSection(&CriticalSection);
for (int i = 0; i < SIZE_TAB; i++)
{
program[i] = 'A';
for (int j = 0; j<8000; j++);
}
LeaveCriticalSection(&CriticalSection);
}
DWORD WINAPI bb(void *v)
{
EnterCriticalSection(&CriticalSection);
for (int i = 0; i<SIZE_TAB; i++)
{
program[i] = 'B';
for (int j = 0; j<8000; j++);
}
LeaveCriticalSection(&CriticalSection);
}
Critical section is a way of protecting data in a multi-threaded program. Once one thread enters a critical section, another thread cannot enter that same critical section until the first thread leaves it.
You have three threads in play here: the main thread, aa and bb. You have ensured that threads aa and bb cannot access the same data at the same time by protecting it with a critical section, but you left it open for the main thread to access it at any time (in the main loop where you print out the array). The main thread is not modifying it, but it is accessing it, so it will print out whatever it finds in the array at the time: the first thread that entered the critical section may have finished modifying the data or it may have not. Furthermore, you have surrounded the entire function body with a critical section in both aa and bb, which means that the first thread to enter it will have fully run through the loop before the other thread gets the chance.

Changing parts of arrays/structs/.. in threads without blocking the whole thing, in pure c

I want to modify some (not all) fields of an array (or structs) in multiple threads, with out blocking the rest of the array as the rest of it is being modified in other threads. How is this achieved? I found some answers, but they are for C++ and I want to do it in C.
Here is the code I got so far:
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define ARRAYLENGTH 5
#define TARGET 10000
int target;
typedef struct zstr{
int* array;
int place;
int run;
pthread_mutex_t* locks;
}zstr;
void *countup(void *);
int main(int argc, char** args){
int al;
if(argc>2){
al=atoi(args[1]);
target=atoi(args[2]);
}else{
al=ARRAYLENGTH;
target=TARGET;
}
printf("%d %d\n", al, target);
zstr* t=malloc(sizeof(zstr));
t->array=calloc(al, sizeof(int));
t->locks=calloc(al, sizeof(pthread_mutex_t));
int* rua=calloc(al, sizeof(int));
pthread_t id[4*al];
for(int i=0; i<al; i++)
pthread_mutex_init(&(t->locks[i]), NULL);
for(int j=0; j<4*al; j++){
int st=j%al;
t->run=rua[st]++;
t->place=st;
pthread_create(&id[j], NULL, &countup, t);
}
for(int k=0; k<4*al; k++){
pthread_join(id[k], NULL);
}
for(int u=0; u<al; u++)
printf("%d\n", t->array[u]);
free(rua);
free(t->locks);
free(t->array);
return 0;
}
void *countup(void* table){
zstr* nu=table;
if(!nu->run){
pthread_mutex_lock(nu->locks + nu->place);
}else{
pthread_mutex_trylock(nu->locks + nu->place);
}
while(nu->array[nu->place]<target)
nu->array[nu->place]++;
pthread_mutex_unlock(nu->locks + nu->place);
return NULL;
}
Sometimes this works just fine, but then calculates wrong values and for quiet sort problems (like the default values), it takes super long (strangely it worked once when I handed them in as parameters).
There isn't anything special about part of an array or structure. What matters is that the mutex or other synchronization you apply to a given value is used correctly.
In this case, it seems like you're not checking your locking function results.
The design of the countup function only allows a single thread to ever access the object, running the value all the way up to target before releasing the lock, but you don't check the trylock result.
So what's probably happening is the first thread gets the lock, and subsequent threads on the same mutex call trylock and fail to get the lock, but the code doesn't check the result. Then you get multiple threads incrementing the same value without synchronization. Given all the pointer dereferences the index and increment operations are not guaranteed to be atomic, leading to problems where the values grow well beyond target.
The moral of the story is to check function results and handle errors.
Sorry, don't have enough reputation to comment, yet.
Adding to Brad's comment of not checking the result of pthread_mutex_trylock, there's a misconception that shows many times with Pthreads:
You assume, that pthread_create will start immediately, and receive the values passed (here pointer t to your struct) and it's content read atomically. That is not true. The thread might start any time later and will find the contents, like t->run and t->place already changed by the next iteration of the j-loop in main.
Moreover, you might want to read David Butenhof's book "Programming with Posix Threads" (old, but still a good reference) and check on synchronization and condition variables.
It's not that good style to start that many threads in the first place ;)
As this has come up a few times and might come up again, I have restructured that a bit to issue work_items to the started threads. The code below might be amended by a function, that maps the index into array to always the same area_lock, or by adding a queue to feed the running threads with further work-item...
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
/*
* Macros for default values. To make it more interesting, set:
* ARRAYLENGTH != THREADS
* INCREMENTS != TARGET
* NUM_AREAS != THREADS
* Please note, that NUM_AREAS must be <= ARRAY_LENGTH.
*/
#define ARRAYLENGTH 10
#define TARGET 100
#define INCREMENTS 10
#define NUM_AREAS 2
#define THREADS 5
/* These variables are initialized once in main, then only read... */
int array_len;
int target;
int num_areas;
int threads;
int increments;
/**
* A long array that is going to be equally split into number of areas.
* Each area is covered by a lock. The number of areas do not have to
* equal the length of the array, but must be smaller...
*/
typedef struct shared_array {
int * array;
int num_areas;
pthread_mutex_t * area_locks;
} shared_array;
/**
* A work-item a thread is assigned to upon startup (or later on).
* Then a value of { 0, any } might signal the ending of this thread.
* The thread is working on index within zstr->array, counting up increments
* (or up until the target is reached).
*/
typedef struct work_item {
shared_array * zstr;
int work_on_index;
int increments;
} work_item;
/* Local function declarations */
void * countup(void *);
int main(int argc, char * argv[]) {
int i;
shared_array * zstr;
if (argc == 1) {
array_len = ARRAYLENGTH;
target = TARGET;
num_areas = NUM_AREAS;
threads = THREADS;
increments = INCREMENTS;
} else if (argc == 6) {
array_len = atoi(argv[1]);
target = atoi(argv[2]);
num_areas = atoi(argv[3]);
threads = atoi(argv[4]);
increments = atoi(argv[5]);
} else {
fprintf(stderr, "USAGE: %s len target areas threads increments", argv[0]);
exit(-1);
}
assert(array_len >= num_areas);
zstr = malloc(sizeof (shared_array));
zstr->array = calloc(array_len, sizeof (int));
zstr->num_areas = num_areas;
zstr->area_locks = calloc(num_areas, sizeof (pthread_mutex_t));
for (i = 0; i < num_areas; i++)
pthread_mutex_init(&(zstr->area_locks[i]), NULL);
pthread_t * id = calloc(threads, sizeof (pthread_t));
work_item * work_items = calloc(threads, sizeof (work_item));
for (i = 0; i < threads; i++) {
work_items[i].zstr = zstr;
work_items[i].work_on_index = i % array_len;
work_items[i].increments = increments;
pthread_create(&(id[i]), NULL, &countup, &(work_items[i]));
}
// Let's just do this one work-item.
for (i = 0; i < threads; i++) {
pthread_join(id[i], NULL);
}
printf("Array: ");
for (i = 0; i < array_len; i++)
printf("%d ", zstr->array[i]);
printf("\n");
free(id);
free(work_items);
free(zstr->area_locks);
free(zstr->array);
return 0;
}
void *countup(void* first_work_item) {
work_item * wi = first_work_item;
int inc;
// Extract the information from this work-item.
int idx = wi->work_on_index;
int area = idx % wi->zstr->num_areas;
pthread_mutex_t * lock = &(wi->zstr->area_locks[area]);
pthread_mutex_lock(lock);
for (inc = wi->increments; inc > 0 && wi->zstr->array[idx] < target; inc--)
wi->zstr->array[idx]++;
pthread_mutex_unlock(lock);
return NULL;
}

Pthread executes unexpectedly in C

I am trying to write a program which gets input files and prints included items into the screen by threads. However, last thread doesn't give any output unexpectedly. I couldn't figure out what the problem is. I am waiting for your help.
Each thread gets 3 parameters :inputFile1, inputFile2 and targetBuf(lets say it is sequence number.) Files are sorted, I just simply try to print their union in order. Only positive numbers are printed.
Command line : merge N outfile file1 file2 file3 .... fileN
N is the number of input files.
If I give 2 input files (means 1 thread), it works. If I give 4 files, then 1st thread give output, 2nd one does not. If I give 6 input files, then 1st and 2nd threads give output but 3rd doesn't.
There are two structs in header file. I pasted them below.
//thread information
struct threadInfo{
pthread_t tid;
pthread_attr_t attr;
};
//Beginning thread input
struct beginThreadInput{
FILE **inputFile1, **inputFile2;
int targetBuf;
};
typedef struct threadInfo THREADINFO;
typedef struct beginThreadInput BEGINT_INPUT;
Main File Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h> //for O_ constants
#include <sys/stat.h>
#include <pthread.h> /* thread library */
#include "merge.h"
int main(int argc, char** argv){
int i, k, N;
/***FILE INITIALIZATION***/
FILE *output;
N = atoi(argv[1]);
output = fopen(argv[2], "w");
FILE **inputFileList = ((FILE**)malloc (N * sizeof(FILE*)));
printf("int N is %d\n", N);
for(i = 0; i<N; i++){
inputFileList[i] = fopen(argv[i + 3], "r");
}
//START THREADS
BEGINT_INPUT **threadInputList = ((BEGINT_INPUT**) malloc ( (N/2)* sizeof(BEGINT_INPUT*)));
THREADINFO **threadInfoList = ((THREADINFO**) malloc ( (N/2) * sizeof(THREADINFO*)));
for(i = 0; i< N/2 ; i++){
threadInputList[i] = (BEGINT_INPUT *) malloc (sizeof(BEGINT_INPUT));
threadInfoList[i] = (THREADINFO *) malloc (sizeof(THREADINFO));
}
pthread_t tid;
pthread_attr_t attr;
for(i = 0, k = 0; i < (N/2); i++){
threadInputList[i]->inputFile1 = &(inputFileList[k]);
threadInputList[i]->inputFile2 = &(inputFileList[++k]);
threadInputList[i]->targetBuf = i;
pthread_attr_init(&(threadInfoList[i]->attr));
pthread_create(&(threadInfoList[i]->tid), &(threadInfoList[i]->attr), runBeginningThreads, (void*)threadInputList[i]);
}
pthread_join((threadInfoList[[(N/2)-1]]->tid), NULL);
for(i = 0; i<N; i++){
fclose(inputFileList[i]);
}
}
void *runBeginningThreads(void *input){
BEGINT_INPUT *myInput = (BEGINT_INPUT *)input;
int first = -1, second = -1, iseof;
printf("Thread number %d\n", myInput->targetBuf);
while((second > -2) && (first > -2)){
//read integer values from files
if(first == -1){
iseof = fscanf(*(myInput->inputFile1), "%d", &first);
if(iseof == EOF){
first = -2; //means end of file
}
else if(first < 0)
first = -1; //means waiting for an integer
}
if(second == -1){
iseof = fscanf(*(myInput->inputFile2), "%d", &second);
if(iseof == EOF){
second = -2;
}
else if(second < 0)
second = -1;
}
//print smaller one
if((first != -1) && (second != -1)){
if(((first < second) || (second == -2)) && (first != -2)){
printf("%d\n", first);
first = -1;
}
else if(second != -2){
printf("%d\n", second);
second = -1;
}
}
}
}
The main problem might be that you only wait for one thread to finish, before you close all files and then exit from the program.
There are a few other extra complications in your program, that could probably be simplified. I mean that you use extra pointer indirection when you don't really need it.
Here's a little simpler version, not using extra pointer-to-pointers where none are needed:
struct beginThreadInput{
FILE *inputFile1, *inputFile2;
int targetBuf;
};
/* ... */
int main(int argc, char** argv){
/* ... */
BEGINT_INPUT *threadInputList = malloc ( (N/2)* sizeof(BEGINT_INPUT));
THREADINFO *threadInfoList = malloc ( (N/2) * sizeof(THREADINFO));
pthread_t tid;
for(i = 0, k = 0; i < (N/2); i++){
threadInputList[i].inputFile1 = inputFileList[k];
threadInputList[i].inputFile2 = inputFileList[++k];
threadInputList[i].targetBuf = i;
pthread_create(&(threadInfoList[i].tid), NULL, runBeginningThreads, &(threadInputList[i]));
}
for(i = 0; i< N/2 ; i++){
pthread_join((threadInfoList[i]->tid), NULL);
}
/* ... */
/* While not strictly needed, you should make it a good
habit of freeing all you allocate */
free(threadInfoList);
free(threadInputList);
return 0; /* You missed this */
}
I solve my question in the following way. As it is seen above, I open the files in main function, then pass them into corresponding threads. I have changed this way and passed the names of the files into threads and open related files in them.
Additionally, while passing files, I was making mistakes, its code is below
threadInputList[i]->inputFile1 = &(inputFileList[k]);
threadInputList[i]->inputFile2 = &(inputFileList[++k]);
This lines are run in a loop. This causes an input sequence like the following ([0,1], [1,2], [2,3], [3,4] ...) becuase I increment k only one time.However, each thread takes two of inputFiles. So the correct code is this:
threadInputList[i]->inputFile1 = &(inputFileList[k++]);
threadInputList[i]->inputFile2 = &(inputFileList[k++]);
I want to point out an issue. I fixed this code for file names, not FILE pointer variables as it is posted there. But to make it clear, I fixed the bug on a code posted there. Thanks for your help, everybody

Passing Data to Multi Threads

I study this code from some book:
#include <pthread.h>
#include <stdio.h>
/* Parameters to print_function. */
struct char_print_parms {
/* The character to print. */
char character;
/* The number of times to print it. */
int count;
};
/* Prints a number of characters to stderr, as given by PARAMETERS,
which is a pointer to a struct char_print_parms. */
void* char_print(void* parameters) {
/* Cast the cookie pointer to the right type. */
struct char_print_parms* p = (struct char_print_parms*) parameters;
int i;
for (i = 0; i < p->count; ++i)
fputc(p->character, stderr);
return NULL;
}
/* The main program. */
int main() {
pthread_t thread1_id;
pthread_t thread2_id;
struct char_print_parms thread1_args;
struct char_print_parms thread2_args;
/* Create a new thread to print 30,000 ’x’s. */
thread1_args.character = 'x';
thread1_args.count = 30000;
pthread_create(&thread1_id, NULL, &char_print, &thread1_args);
/* Create a new thread to print 20,000 o’s. */
thread2_args.character = 'o';
thread2_args.count = 20000;
pthread_create(&thread2_id, NULL, &char_print, &thread2_args);
usleep(20);
return 0;
}
after running this code, I see different result each time. and some time corrupted result. what is wrong and what the correct way to do that?
Add:
pthread_join( thread1_id, NULL);
pthread_join( thread2_id, NULL);
to the bottom of your code, before the return in main. Your prosess ends before your threads can complete. A sleep of 20 micro seconds is not enough to let your threads complete executing. Safer to wait for the threads to return.

Resources