I already asked a question regarding a program. I got an answer but I have a new issue. The program I wrote is not working and I don't know why.
我已经问了一个关于程序的问题。我得到了答案,但我有一个新问题。我写的程序不起作用,我不知道为什么。
The function is working fine (I guess) but I want to be sure by printing the values of each element of the array and see if they are correct (I will need this for other purposes).
该函数工作正常(我猜)但我想确保打印数组的每个元素的值,看看它们是否正确(我将需要它用于其他目的)。
I tried with a simple for instruction for a printf, but the array seems to be empty. The problem may be related to the memory address of the value I want to print.
我尝试了一个简单的printf指令,但数组似乎是空的。问题可能与我要打印的值的内存地址有关。
Please have a look, any advice will be welcome!
请看一下,欢迎任何建议!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int * rand_gen(int N, float fl_value); // Function declaration
int main() // Main function
{
int N; // Declaration: Number of trials
printf("Number of trials: ");
scanf("%d", &N); // Asks for Number of trials
float alpha = 0.3;
float beta = 0.4;
float gamma = 0.5;
float delta = 0.6;
int i; // Index
int seed = time(NULL); // Random number generator seed (based on current time)
srand(seed);
// Populate arrays
float *bin_array_alpha[] = { rand_gen( N, alpha ) };
float *bin_array_beta[] = { rand_gen( N, beta ) };
float *bin_array_gamma[] = { rand_gen( N, gamma ) };
float *bin_array_delta[] = { rand_gen( N, delta ) };
// Here I would like to print the elements of the arrays, something like:
for ( i=0 ; i<N ; i++)
{
printf("\nbin_array_alpha[%d] = %f",i,bin_array_alpha[i]);
printf("\nbin_array_beta[%d] = %f",i,bin_array_beta[i]);
printf("\nbin_array_gamma[%d] = %f",i,bin_array_gamma[i]);
printf("\nbin_array_delta[%d] = %f",i,bin_array_delta[i]);
printf("\n");
}
// Free the memory
static const size_t m = sizeof(bin_array_alpha)/sizeof(bin_array_alpha[0]);
for ( size_t i = 0 ; i < m ; ++i )
{
free(bin_array_alpha[i]);
free(bin_array_beta[i]);
free(bin_array_gamma[i]);
free(bin_array_delta[i]);
bin_array_alpha[i] = NULL;
bin_array_beta[i] = NULL;
bin_array_gamma[i] = NULL;
bin_array_delta[i] = NULL;
}
printf("\n");
return(0);
}
// Function: generate an array populated by: array[j] = rand()*fl_value
int * rand_gen(int N, float fl_value)
{
int *array;
array = (int *)malloc(sizeof(float)*N);
if(array == NULL)
{
printf("\nRun out of memory!\n");
exit(1);
}
int j;
float x;
for( j = 0 ; j < N ; j++ )
{
x = rand(); // Generates a random number
x = x/RAND_MAX; // 0 < x < 1
x = x * fl_value;
array[j] = x; // array[j] = x * fl_value
}
return array;
}
The result is always the same (for example N=3):
结果始终相同(例如N = 3):
bin_array_alpha[0] = 0.000000
bin_array_beta[0] = 0.000000
bin_array_gamma[0] = 0.000000
bin_array_delta[0] = 0.000000
bin_array_alpha[1] = 0.000000
bin_array_beta[1] = 0.000000
bin_array_gamma[1] = 0.000000
bin_array_delta[1] = 0.000000
bin_array_alpha[2] = 0.000000
bin_array_beta[2] = 0.000000
bin_array_gamma[2] = 0.000000
bin_array_delta[2] = 0.000000
Thank you so much again!
再次感谢你!
1 个解决方案
#1
2
your problem is in the allocation of the arrays of float that you not simply confused with int but the technique of allocation is wrong:
你的问题是浮点数组的分配,你不是简单地与int混淆,但分配技术是错误的:
float **rand_gen(int N, float fl_value)
{
float **array;
array = (float **)malloc(sizeof(float *) * N + 1);
if (array == NULL)
{
printf("\nRun out of memory!\n");
exit(1);
}
int i;
for (i = 0; i < N; i++)
{
array[i] = (int *)malloc(sizeof(float));
array[i][0] = 0;
}
array[N] = NULL;
int j;
float x;
for (j = 0; j < N; j++)
{
x = rand(); // Generates a random number
x = x / RAND_MAX; // 0 < x < 1
x = x * fl_value;
*(array[j]) = x; // array[j] = x * fl_value
}
return array;
}
An array of float is a pointer to a memory area where each element is a pointer to a float.
float数组是指向内存区域的指针,其中每个元素都是指向float的指针。
This means you have to correct the declaration:
这意味着您必须更正声明:
float **rand_gen(int N, float fl_value); // Function declaration
and the usage in the main:
以及主要用途:
float **bin_array_alpha = rand_gen(N, alpha);
float **bin_array_beta = rand_gen(N, beta);
float **bin_array_gamma = rand_gen(N, gamma);
float **bin_array_delta = rand_gen(N, delta);
The same happens for the printf:
printf也是如此:
printf("\nbin_array_alpha[%d] = %f", i, *bin_array_alpha[i]);
Instead to free the memory you need to add the free not only of each float but also of the memory base:
而不是释放内存,你需要添加每个浮点数以及内存基数的免费:
free(bin_array_alpha
#1
2
your problem is in the allocation of the arrays of float that you not simply confused with int but the technique of allocation is wrong:
你的问题是浮点数组的分配,你不是简单地与int混淆,但分配技术是错误的:
float **rand_gen(int N, float fl_value)
{
float **array;
array = (float **)malloc(sizeof(float *) * N + 1);
if (array == NULL)
{
printf("\nRun out of memory!\n");
exit(1);
}
int i;
for (i = 0; i < N; i++)
{
array[i] = (int *)malloc(sizeof(float));
array[i][0] = 0;
}
array[N] = NULL;
int j;
float x;
for (j = 0; j < N; j++)
{
x = rand(); // Generates a random number
x = x / RAND_MAX; // 0 < x < 1
x = x * fl_value;
*(array[j]) = x; // array[j] = x * fl_value
}
return array;
}
An array of float is a pointer to a memory area where each element is a pointer to a float.
float数组是指向内存区域的指针,其中每个元素都是指向float的指针。
This means you have to correct the declaration:
这意味着您必须更正声明:
float **rand_gen(int N, float fl_value); // Function declaration
and the usage in the main:
以及主要用途:
float **bin_array_alpha = rand_gen(N, alpha);
float **bin_array_beta = rand_gen(N, beta);
float **bin_array_gamma = rand_gen(N, gamma);
float **bin_array_delta = rand_gen(N, delta);
The same happens for the printf:
printf也是如此:
printf("\nbin_array_alpha[%d] = %f", i, *bin_array_alpha[i]);
Instead to free the memory you need to add the free not only of each float but also of the memory base:
而不是释放内存,你需要添加每个浮点数以及内存基数的免费:
free(bin_array_alpha