[英]Will freeing a structure pointer also free the memory allocated inside of the structure in C?
[英]Will memory be allocated for a typedef structure pointer?
我有以下程序,在這里我有一個結構。 我將為其分配一些值並將其寫入文件。 但是這里的困惑是,我剛剛聲明了一個指向該結構的指針,並且還沒有分配內存。 那么變量分配如何工作? 我能夠從文件“ / home / info”正確檢索值
#include <stdio.h>
#define FILEE "/home/info"
typedef struct my_info
{
int i;
int j;
int k;
int l;
}_my_info;
void main()
{
_my_info *my_info;
int fd;
FILE *fp;
my_info->i=100;
my_info->j=300;
my_info->k=200;
my_info->l=400;
fp = fopen(FILEE,"w");
if (fp == NULL)
printf("Error in opening file\n");
fd=fwrite(my_info, sizeof(_my_info), 1, fp);
if (fd<0)
printf("Error while writing\n");
fclose(fp);
}
當您聲明my_info
_my_info *my_info;
它將具有未定義的值。 您的情況下, my_info
的值在RAM的有效內存地址范圍內。 因此,將對其進行寫入和讀取。
但是,您不知道因此要更改哪個其他內存。 這可能會導致內存損壞,尤其是在較大的程序中。
由於my_info
指針位於堆棧上,因此之前可能使用了相同的位置,並指向一些已分配的內存。 您正在覆蓋其他一些數據,這可能會或可能不會導致程序崩潰。
簡而言之-它偶然地起作用。
由於未初始化指針變量,因此它的值是隨機的,表示指針指向某個隨機地址。 在訪問地址時,根據您的操作系統,它可能會崩潰,或者只是獲取一些垃圾數據。
您當前的代碼存在幾個問題。 在遇到結構問題之前,您似乎將write
和fwrite
的返回值與以下內容混淆了:
fd=fwrite(my_info, sizeof(_my_info), 1, fp);
if (fd<0)
printf("Error while writing\n");
fwrite
返回值的類型為size_t
。 它永遠不能小於0
。 如果您在啟用警告的情況下進行編譯(例如-Wall -Wextra
),則將始終收到有關比較的警告,始終將其測試為false。
關於結構的空間,為什么要聲明一個指針? 除非您的示例中有聲明指針的重要原因,否則只需聲明該結構的靜態實例即可自動保留存儲空間。 如果確實聲明了指針,則必須分配空間(例如_my_info *my_info = malloc (sizeof *my_info);
或_my_info *my_info = calloc (1, sizeof *my_info);
)
對於您的示例,除了要進行動態分配外,沒有其他理由聲明指針,而只是聲明一個實例。 以下是使用實例並從文件寫入/讀取值的簡短示例。 ( 注意:有關int main()
的注釋-以及將默認文件名更改為/tmp/info.bin
):
#include <stdio.h>
#define FILEE "/tmp/info.bin"
typedef struct {
int i;
int j;
int k;
int l;
} _my_info;
/* main is type 'int', has arguments and returns a value */
int main (int argc, char **argv)
{
_my_info my_info, my_info2; /* static instances */
/* write to given filename, or FILEE (default) */
FILE *fp = fopen (argc > 1 ? argv[1] : FILEE, "wb");
if (!fp) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
my_info.i = 100;
my_info.j = 300;
my_info.k = 200;
my_info.l = 400;
if (fwrite (&my_info, sizeof my_info, 1, fp) < 1) {
fprintf (stderr, "error: write to '%s' failed\n",
argc > 1 ? argv[1] : FILEE);
return 1;
}
if (fclose(fp) == EOF) goto errclose;
printf ("\n values written to : %s\n", argc > 1 ? argv[1] : FILEE);
/* reopen file for reading, read values into my_info2 */
fp = fopen (argc > 1 ? argv[1] : FILEE, "rb");
if (fread (&my_info2, sizeof my_info2, 1, fp) < 1) {
fprintf (stderr, "error: read from '%s' failed\n",
argc > 1 ? argv[1] : FILEE);
return 1;
}
if (fclose(fp) == EOF) goto errclose;
printf (" values read from : %s\n\n", argc > 1 ? argv[1] : FILEE);
printf (" my_info2.i : %d\n my_info2.j : %d\n"
" my_info2.k : %d\n my_info2.l : %d\n\n",
my_info2.i, my_info2.j, my_info2.k, my_info2.l);
return 0;
errclose:
fprintf (stderr, "error: EOF returned on stream close.\n");
return 1;
}
產量
$ ./bin/struct_write
values written to : /tmp/info.bin
values read from : /tmp/info.bin
my_info2.i : 100
my_info2.j : 300
my_info2.k : 200
my_info2.l : 400
如果您還有其他問題,請告訴我,如果您迫切需要使用指向結構的指針,請告訴我,我們很樂意為您提供進一步的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.