Hello everyone!! I have a problem about memory allocation, i use calloc() to initialize all elements in array to be zero.

This is my code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define sizes 1200



 main()
{
    double** arr;
    int m[sizes];
    int i,j,k;

    srand((unsigned)time(NULL));

    arr= (double **) calloc(sizes,sizeof(double*));
    for(k=0; k< sizes; k++){
    arr[k]=(double*)calloc(sizes,sizeof(double));
    }

    for(j=0; j< sizes; j++){        
        m[j]= 0;
        while(m[j] <30){
            i= (int) rand()%1200;
            if (arr[i][j] == 0.0) {
                arr[i][j]= (double) rand()/RAND_MAX;
                if(arr[i][j] > 0){
                    m[j]++;
                    printf("arr[%d][%d] = %lft", i, j, arr[i][j]);
                    printf("n");
                }
            }
        }
    }
    // FREEING MEMORY        
//  for (k = 0; k<sizes; i++)    {        
//      free(arr[k]);        
//  }   
    free(arr);  

    getch();

}

My problem is the result of this code was show only some data not the hold data. Could anyone plz help me what's wrong with my code?

Thanks in advance!

Dani AI

Generated

Three quick diagnostics that explain why you only see “some” results:

  • The print statements in the posted code use literal "t" and "n" instead of the escape sequences "\t" and "\n", so output doesn’t break into readable lines (it prints letters, not newlines/tabs). That alone makes the console output look truncated or garbled.
  • The approach that picks a random row i and accepts it only if arr[i][j] == 0.0 gets progressively slower as a column fills. When few empty slots remain you get lots of repeats and long loops (and fewer visible new writes per time).
  • There’s also a bug in the commented free loop (for (k = 0; k < sizes; i++)) — if enabled that would loop incorrectly. Free the inner blocks with k++, or use a single contiguous allocation to simplify lifetime management.

Immediate, practical fixes

  • Fix printing: use a correct format and escapes, for example:
    printf("arr[%d][%d] = %f\n", i, j, arr[i][j]);
  • Avoid the repeated-try approach. Use sampling without replacement (Fisher–Yates) to pick 30 distinct row indices per column. Example pattern:
    int idx[sizes];
    for (i = 0; i < sizes; ++i) idx[i] = i;
    for (i = sizes - 1, count = 0; count < 30; --i, ++count) {
      int p = rand() % (i + 1);
      int tmp = idx[p]; idx[p] = idx[i]; idx[i] = tmp;
      int row = idx[i];
      arr[row][j] = rand() / (double)RAND_MAX;
    }

    This guarantees 30 unique fills per column and runs in linear time.

Additional tips and cautions

  • To simplify allocation/freeing and improve cache locality, allocate a single block: double *A = calloc((size_t)sizes * sizes, sizeof(double)); and index with A[i*sizes + j]. Free once with free(A).
  • If you stick with double**, free like:
    for (k = 0; k < sizes; ++k) free(arr[k]);
    free(arr);

    (not i++).

  • Print to a file when you need to inspect thousands of values. As suggested, writing output to a file makes verification easier. These changes address both the visible-output issue and the underlying sampling inefficiency that was seeing.

Recommended Answers

All 6 Replies

Not sure what you are trying to accomplish with that code, but the reason that you don't see all the data is probably due to those two if conditions. Remove them and just set each element to some random number.

for(i = 0; i < sizes; i++)
{
    for(j = 0; j < sizes; j++)
        arr[i][j] = rand();
}

Thanks for your reply sir, i can't do like you suggest because i don't want to random the hold elament of columns but i want to random with the fix m[j] . Do you have any other way to slove this problem?

delete this if statement: if(arr[i][j] > 0){

and this one: if(arr[i][j] > 0){

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define sizes 1200



 main()
{
    double** arr;
    int m[sizes];
    int i,j,k;

    srand((unsigned)time(NULL));

    arr= (double **) calloc(sizes,sizeof(double*));
    for(k=0; k< sizes; k++){
    arr[k]=(double*)calloc(sizes,sizeof(double));
    }

    for(j=0; j< sizes; j++){        

        for(m[j] = 0; m[j] <30, m[j]++){
            i= (int) rand()%1200;
            arr[i][j]= (double) rand()/RAND_MAX
            printf("arr[%d][%d] = %lft", i, j, arr[i][j]);
            printf("n");
        }
    }
    // FREEING MEMORY        
  for (k = 0; k<sizes; i++)    {        
      free(arr[k]);        

  }   
    free(arr);  

    getch();

}

I got the same result sir :(

It is showing you all the rows/columns that it populates. If you display them all then you will see that there are lots of cells that were not populated. Print them to a text file instead of on the screen so that you can easily see what cells were or were not populated.

ok, Thanks for your help sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.