-2

I am writing a code and i hope this summon my problem. Any help is appreciated>

I created an array by:

    intermediateBinCounts = (int *)malloc(bin_count * thread_count * sizeof(int));

and want it to increment value in another function:

  for(int i=privateStartingIndex; i<privateEndingIndex; i++){
    int returnedBinIndex = findBin(data[i]);
    int intermBinCountIndex = (threadIndex*bin_count)+returnedBinIndex;
    intermediateBinCounts[intermBinCountIndex]++;
    printf("\nthis is value %d\n", intermediateBinCounts[intermBinCountIndex]);
}

intermBinCountIndex is integer which is returned from the function and their value in my case are among 0, 1, 2, 3 only.

but my printf is returning:

this is value -1163005938

this is value -1163005937

this is value -1163005938

I don't know if my question is able to articulate my issue if it is not able to please let me know so that I could edit it again my best.

I want that intermediateBinCounts to be initialized to zero and increment the value at that index which value is returned from the function call i.e findBin.

Pravin Poudel
  • 1,433
  • 3
  • 16
  • 38
  • 2
    Do you remember to initialize the elements of `intermediateBinCounts`? The `malloc` function will not initialize the memory it allocates, its contents will be *indeterminate*. – Some programmer dude Oct 04 '21 at 06:35
  • 2
    Please, post a [mcve]. There is not much which can fail with an increment of an `int` but I have some doubts about your array index. – Scheff's Cat Oct 04 '21 at 06:36
  • 1
    Also please don't tag multiple languages, unless your question really is about all the languages tagged. If you program in a single language, only tag that single language. Especially considering that the behavior of using indeterminate data differs between C and the very different language C++. – Some programmer dude Oct 04 '21 at 06:36
  • 2
    And in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Oct 04 '21 at 06:37
  • "I want that intermediateBinCounts to be initialized to zero" Please make sure that your MRE ( [mre] ) includes the code where you do that. You seem to have programmed it in a way that the program ... um ... did not. – Yunnosch Oct 04 '21 at 06:40
  • 1
    Try `intermediateBinCounts = calloc(bin_count * thread_count, sizeof(int));` – user3386109 Oct 04 '21 at 06:40
  • yes, i have initialized as int* intermediateBinCounts; – Pravin Poudel Oct 04 '21 at 06:40
  • 2
    `int* intermediateBinCounts;` only defines the pointer, does not even malloc memory to it (though you do that later it seems, which at least initialises the pointer, but still not the allocated memory) and does not initialise the memory. Please consider to show a MRE here, so that we can see your code and verify whether you only do not know which part of it initialises or actually do not initialise at all. Please show a [mre] by [edit]ing it into your question. – Yunnosch Oct 04 '21 at 06:42
  • 1
    Memory allocated via `malloc` is not initializted to zero. You can use [`calloc`](https://www.cplusplus.com/reference/cstdlib/calloc/) that does initialize to 0: `intermediateBinCounts = calloc(bin_count * thread_count, sizeof(int));` – Jabberwocky Oct 04 '21 at 06:46

2 Answers2

2

I want that intermediateBinCounts to be initialized to zero

Well, you don't want that. intermediateBinCounts is a pointer and you want to set it to the value returned by malloc.

What you want to set to zero is the memory that intermediateBinCounts points to!

You can use a simple loop after the malloc- like:

int * intermediateBinCounts = malloc(bin_count * thread_count * sizeof(int));
for (int i=0; i < bin_count * thread_count; ++i)
{
    intermediateBinCounts[i] = 0;
}

or use calloc instead of malloc as calloc zero initialize the allocated memory:

int* intermediateBinCounts = calloc(bin_count * thread_count, sizeof(int));
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

This declares a pointer to int, without already defining it, this is for headers
(though doing this implies global variables which many users recommend against):

extern int* pointer;

This defines that pointer, without initialising it; this is for exactly one code file:

int* pointer;

This defines and initialises the pointer; use it instead of above:

int* pointer=NULL;

This allocates some memory to the pointer:

pointer = malloc(sizeof(int));

This initialises the allocated memory:

*pointer=0;

This uses the initialised value pointed to by the pointer and increments it; doing this before above means undefined behaviour and would explain your unexpected values:

*pointer = *pointer +1;

Please check your code for what you are actually doing when.
Most likely you did not do the initialising.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54