我正在嘗試自學 C,但我正在為它看起來非常基本的東西而苦苦掙扎。我有一個帶有 char 指標的結構,我想動態分配記憶體并在完成后釋放它。我的理解是,我需要為結構和字符指標都分配記憶體,但它看起來像我失去了一些東西,因為我得到一個分割故障就sprintf行。
作為理解和使用mallocand的練習free,我正在嘗試構建一個簡單的程式,該程式將 aint作為輸入并輸出一個表,其中包含從 0 到輸入的所有數字的階乘。每個表條目都有其索引 ( int)、階乘 ( long long int) 的結果和結果,但采用字符格式的陣列(這是有問題的)。
這是我到目前為止所擁有的:
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define LIMIT 20
struct entry
{ /* Definition of each table entry */
int n;
long long int lli;
char *str;
};
void shout(char *m)
{
fprintf (stderr, "%s\n", m);
exit (0);
}
int main (int argc, char *argv[])
{
int n;
int i;
struct entry *fact_table;
if (argc != 2)
shout("wrong parameters");
n = atoi (argv[1]);
if (n < 0)
shout("n too small");
if (n > LIMIT)
shout("n too big");
// Allocate memory for struct
fact_table = (struct entry*) malloc((n 1) * sizeof(struct entry));
if (fact_table == NULL) {
shout("malloc");
}
// Compute factorials
fact_table[0].n = 0;
fact_table[0].lli = fact_table[1].n = fact_table[1].lli = 1; // 0! = 1! = 1
fact_table[0].str = fact_table[1].str = "1";
for (i=2; i<=n; i ) {
fact_table[i].n = i;
fact_table[i].lli = i * fact_table[i-1].lli; // N! = N*(N-1)!
int digits = log10(fact_table[i].lli) 1; // get number of digits of the result to allocate memory in consequence
fact_table[i].str = malloc((digits 1)*sizeof(char));
if (fact_table[i].str = NULL) {
shout("malloc");
}
sprintf(fact_table[i].str, "%lld", fact_table[i].lli); // convert to string. ERROR HERE
}
// print factorial table
for (i= 0; i<=n; i )
{
printf ("%d %lld %s\n", fact_table[i].n, fact_table[i].lli, fact_table[i].str);
}
// Free memory
for (i=0; i<=n; i ) {
free(fact_table[i].str);
}
free(fact_table);
return 0;
}
在為 分配記憶體時,我可能遺漏了一些非常明顯的內容并犯了一個愚蠢的錯誤fact_table[i].str,但我正在努力使其作業。
uj5u.com熱心網友回復:
除了關于為 n >= 2 釋放記憶體的注釋之外,如果您在 malloc 之后查看您的測驗:
if (fact_table[i].str = NULL) {
您正在將指標設定為 NULL。相反,你應該寫:
fact_table[i].str = malloc((digits 1)*sizeof(char));
if ( fact_table[i].str == NULL) {
shout("malloc");
}
這里只是一些小建議。如果您使用-Wall以下命令進行編譯,此錯誤會在大多數編譯器上生成警告:
fact.c: In function ‘main’:
fact.c:53:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (fact_table[i].str = NULL) {
^~~~~~~~~~
此外,為了確保編譯器在您忘記 a 時會抱怨=,您可以比較變數和常量的值,將常量放在左側:如果您撰寫if (NULL = fact_table[i].str),您的編譯器會抱怨并通知您錯誤。
uj5u.com熱心網友回復:
我在我的設定中重現了這個問題。在程式運行正常之前,我們需要解決兩個問題。
將“fact_table[i].str = NULL”更改為“fact_table[i].str == NULL”。您之前的命令直接將 'str' 設定為 NULL,這是意料之中的。這就是您遇到段錯誤的原因。
在Free memory部分,你需要從index 2開始,你不能釋放不屬于你的記憶體。
下面的代碼可以在我的設定中很好地作業。希望這可以幫助你。
**
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define LIMIT 20
struct entry
{ /* Definition of each table entry */
int n;
long long int lli;
char *str;
};
void shout(char *m)
{
fprintf (stderr, "%s\n", m);
exit (0);
}
int main (int argc, char *argv[])
{
int n;
int i;
struct entry *fact_table;
if (argc != 2)
shout("wrong parameters");
n = atoi (argv[1]);
if (n < 0)
shout("n too small");
if (n > LIMIT)
shout("n too big");
// Allocate memory for struct
fact_table = (struct entry*) malloc((n 1) * sizeof(struct entry));
if (fact_table == NULL) {
shout("malloc");
}
// Compute factorials
fact_table[0].n = 0;
fact_table[0].lli = fact_table[1].n = fact_table[1].lli = 1; // 0! = 1! = 1
fact_table[0].str = fact_table[1].str = "1";
for (i=2; i<=n; i ) {
fact_table[i].n = i;
fact_table[i].lli = i * fact_table[i-1].lli; // N! = N*(N-1)!
int digits = log10(fact_table[i].lli) 1; // get number of digits of the result to allocate memory in consequence
fact_table[i].str = malloc((digits 1)*sizeof(char));
if (fact_table[i].str == NULL) {
shout("malloc");
}
sprintf(fact_table[i].str, "%lld", fact_table[i].lli); // convert to string. ERROR HERE
}
// print factorial table
for (i= 0; i<=n; i )
{
printf ("%d %lld %s\n", fact_table[i].n, fact_table[i].lli, fact_table[i].str);
}
// Free memory
for (i=2; i<=n; i ) {
free(fact_table[i].str);
}
free(fact_table);
return 0;
}
**
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338881.html
上一篇:C中的無限正弦生成
