Joseph
Joseph

Reputation: 329

Pointer Problems and Dynamically allocating Memory

I'm working on an exercise that will eventually turn into a pthread experiment for educational purposes. Here I'm taking a list (the alphabet) and trying to get five lists of numbers that I can feed to a pthread to analyze something. For example, numList = 0 5 10 15 20 25 , in the case of the alphabet. Eventually, I'll set it up so pthread will take that list and compare elements at those positions in a list to another thing. I've created a divList function to try and return the above list so I can use it. And though I get the correct results in the function, I'm not returning the list back to main. Is there an obvious error in the way I'm handling the pointer and function?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#define num_threads 5

/* Global variable: accessible to all threads */
char *list = "abcdefghijklmnopqrstuvwxyz";
/* ----------------------------------------- */

int *divList(int MAX, int pos);
void* Hello(void* rank); /* Thread Function */

int main(int argc, char *argv[])
{
    int i, n, thread;
    pthread_t* thread_handles;

    n = strlen(list);
    for (i = 0; i < num_threads; i++){
        if (i <= n % num_threads) // str length / 5 ... remainder 
             //  = 0, 1, 2, 3, 4 
            printf("elements: %d\n", divList(n + 1, i));
        else{
            printf("elements: %d\n", divList(n, i));
        }
    }

    thread_handles = malloc (num_threads*sizeof(pthread_t));

    for (thread = 0; thread < num_threads; thread++){
        pthread_create(&thread_handles[thread], NULL, Hello, 
         (void*) thread);
    }

    printf("Hello from the main thread\n");

    for (thread = 0; thread < num_threads; thread++)
        pthread_join(thread_handles[thread], NULL);
    free(thread_handles);
    return 0;   
}   /* MAIN */

int *divList(int MAX, int pos)
{
    int i = 0, j;
    int *numList = malloc(MAX + 1);

    for (j = pos; j < MAX; (j = j + num_threads)){
        numList[i] = j;
        printf("test list: %d\n", numList[i]);
        i++;
    }
    return numList;
}

void* Hello(void* rank) {
    int my_rank = (int) rank; 
    printf("Hello from  thread %d of %d\n", my_rank, num_threads);

    return NULL;
}   /* Hello */

Upvotes: 2

Views: 54

Answers (2)

EarthDragon
EarthDragon

Reputation: 755

Your divList function returns an int * and you are treating the return value as an int (you're printing it with %d). You should save the returned value in a variable, because you should free that memory after you use it (every call to divList allocates memory and currently you don't free that).

I'm not sure exactly what you want to do with the array divList gives you, but here is how you can access an element in list:

int *arr = divList(n+1, i);
printf("%d\n", arr[5]);

Upvotes: 0

alk
alk

Reputation: 70931

divList() returns a pointer to the 1th element of an array of int with the number of elements equal to what you passed in to devList() as 1st parameter plus 1, which the below code stores in plist.

To access the values you could treat the pointer like an array and for example loop to print its values:

void printList(int * plist, size_t s)
{
  for (size_t i = 0; i < s; ++i)
  {
    printf("element[%zu]: %d\n", i, plist[i];
  }
}

int main(int argc, char *argv[])
{
  int i, n, thread;
  int *plist = NULL;
  pthread_t *thread_handles;

  n = strlen(list);
  for (i = 0; i < num_threads; i++)
  {
    if (i <= n % num_threads)  
    {
      plist = divList(n + 1, i);
      printList(plist, n + 1);  /* divList allocates one more then needed 
                                   but never initialises it, so do not print it. */
    }
    else
    {
      ..

Upvotes: 1

Related Questions