فهرست سرفصل‌های C
خانه (Home) مقدمه (Intro) شروع کار (Get Started) سینتکس (Syntax) دستورات (Statements) خروجی متن (Print Text) خط های جدید (New Lines) کامنت ها (Comments) ساخت متغیرها (Create Variables) مشخص کننده های فرمت (Format Specifiers) تغییر مقدارها (Change Values) چند متغیر (Multiple Variables) نام گذاری متغیرها (Variable Names) نمونه واقعی متغیرها (Real-Life Examples) انواع داده (Data Types) کاراکترها (Characters) اعداد (Numbers) دقت اعشاری (Decimal Precision) حجم حافظه (Memory Size) مثال واقعی داده ها (Real-Life Example) تبدیل نوع (Type Conversion) انواع پیشرفته (Extended Types) ثابت ها (Constants) عملگرها (Operators) عملگرهای حسابی (Arithmetic) عملگرهای انتساب (Assignment) عملگرهای مقایسه (Comparison) عملگرهای منطقی (Logical) اولویت عملگرها (Precedence) بولین ها (Booleans) مثال واقعی بولین ها (Real-Life Examples) شرط ها – if (if) else else if نوشتار کوتاه if (Short Hand If) if تو در تو (Nested If) عملگرهای منطقی در شرط ها (Logical Operators) مثال واقعی شرط ها (Real-Life Examples) سوئیچ (Switch) حلقه while (While Loop) حلقه do-while (Do/While Loop) مثال واقعی while (Real-Life Examples) حلقه for (For Loop) حلقه های تو در تو (Nested Loops) مثال واقعی for (Real-Life Examples) دستور break/continue (Break/Continue) آرایه ها (Arrays) اندازه آرایه (Array Size) حلقه روی آرایه (Array Loops) مثال واقعی آرایه (Real-Life Example) آرایه چندبعدی (Multidimensional Arrays) رشته ها (Strings) کاراکترهای خاص (Special Characters) توابع رشته (String Functions) ورودی کاربر (User Input) آدرس حافظه (Memory Address) پوینترها (Pointers) پوینتر و آرایه ها (Pointers & Arrays) حساب پوینتر (Pointer Arithmetic) پوینتر به پوینتر (Pointer to Pointer) توابع (Functions) پارامترهای تابع (Function Parameters) حوزه متغیرها (Scope) اعلان تابع (Function Declaration) توابع ریاضی (Math Functions) توابع inline (Inline Functions) بازگشت (Recursion) پوینترهای تابع (Function Pointers) تابع callback (Callback Functions) ایجاد فایل (Create Files) نوشتن در فایل (Write To Files) خواندن فایل (Read Files) ساختارها (Structures) ساختار تو در تو (Nested Structures) ساختار و پوینترها (Structs & Pointers) یونیون ها (Unions) typedef پدینگ ساختار (Struct Padding) انوم ها (Enums) مدیریت حافظه (Memory Management) اختصاص حافظه (Allocate Memory) دسترسی به حافظه (Access Memory) تخصیص مجدد (Reallocate Memory) آزادسازی حافظه (Deallocate Memory) ساختارها و حافظه (Structs and Memory) مثال حافظه (Memory Example) خطاها (Errors) دیباگ (Debugging) مقدار NULL مدیریت خطا (Error Handling) اعتبارسنجی ورودی (Input Validation) تاریخ و زمان (Date) اعداد تصادفی (Random Numbers) ماکروها (Macros) سازماندهی کد (Organize Code) کلاس های ذخیره سازی (Storage Classes) عملگرهای بیتی (Bitwise Operators) اعداد ثابت عرض (Fixed-width Integers) پروژه ها (Projects) مرجع (Reference) کلمات کلیدی (Keywords) stdio.h stdlib.h string.h math.h ctype.h time.h مثال ها (Examples) مثال های واقعی (Real-Life Examples) تمرین ها (Exercises) آزمون (Quiz) کامپایلر آنلاین (Compiler) سرفصل دوره (Syllabus) برنامه مطالعه (Study Plan) گواهینامه (Certificate)
نتیجه‌ای برای جستجو یافت نشد.
C

C — ساختارها و حافظه (Structs and Memory)

آخرین بروزرسانی: 1404/08/14

ساختارها و حافظه (Structs and Memory)

اینجا با «ساختار (Struct)» و «حافظه پویا (Dynamic Memory)» کار می کنیم. وقتی تعداد داده ها معلوم نیست، با حافظه پویا فقط به اندازه لازم فضا می گیریم. مثل لیست خرید که بسته به نیاز بزرگ یا کوچک می شود.

اختصاص حافظه برای یک ساختار

با malloc() می توانیم برای یک اشاره گرِ ساختار فضا بگیریم. سپس مقداردهی کنیم و در پایان آزاد کنیم. «اشاره گر (Pointer)» فقط آدرس حافظه را نگه می دارد.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Car {
  char brand[50];
  int year;
};

int main() {
  struct Car *ptr;
  ptr = (struct Car *) malloc(sizeof(struct Car));
  if (ptr == NULL) {
    printf("Memory allocation failed.\n");
    return 1;
  }
  strcpy(ptr->brand, "Honda");
  ptr->year = 2022;
  printf("Brand: %s\n", ptr->brand);
  printf("Year: %d\n", ptr->year);
  free(ptr);
  return 0;
}

مشاهده در ادیتور

آرایه ای از ساختارها

می توانیم یک آرایه پویا از ساختارها بسازیم. سپس هر عضو را مقداردهی کنیم. اگر گرفتن حافظه شکست خورد، پیام بده و خارج شو.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Car {
  char brand[50];
  int year;
};

int main() {
  struct Car *cars;
  cars = (struct Car *) malloc(3 * sizeof(struct Car));
  if (cars == NULL) {
    printf("Memory allocation failed.\n");
    return 1;
  }
  strcpy(cars[0].brand, "Ford");
  cars[0].year = 2015;
  strcpy(cars[1].brand, "BMW");
  cars[1].year = 2018;
  strcpy(cars[2].brand, "Volvo");
  cars[2].year = 2023;
  for (int i = 0; i < 3; i++) {
    printf("%s - %d\n", cars[i].brand, cars[i].year);
  }
  free(cars);
  return 0;
}

مشاهده در ادیتور

بزرگ کردن آرایه با realloc()

وقتی عنصر بیشتر می خواهی، از realloc() استفاده کن. نتیجه را در «اشاره گر موقت» بگذار. اگر شکست خورد، آدرس قبلی را از دست نمی دهی.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Car {
  char brand[50];
  int year;
};

int main() {
  int count = 2;
  struct Car *cars;
  cars = (struct Car *) malloc(count * sizeof(struct Car));
  if (cars == NULL) {
    printf("Initial allocation failed.\n");
    return 1;
  }
  strcpy(cars[0].brand, "Toyota");
  cars[0].year = 2010;
  strcpy(cars[1].brand, "Audi");
  cars[1].year = 2019;
  int newCount = 3;
  struct Car *tmp;
  tmp = (struct Car *) realloc(cars, newCount * sizeof(struct Car));
  if (tmp == NULL) {
    free(cars);
    printf("Reallocation failed.\n");
    return 1;
  }
  cars = tmp;
  strcpy(cars[2].brand, "Kia");
  cars[2].year = 2022;
  for (int i = 0; i < newCount; i++) {
    printf("%s - %d\n", cars[i].brand, cars[i].year);
  }
  free(cars);
  return 0;
}

مشاهده در ادیتور

نکته: حافظه تازه اضافه شده با realloc مقداردهی نشده است. قبل از استفاده، مقدار بده.

گام های عملی مدیریت ساختار و حافظه

  1. با malloc یا calloc حافظه بگیر.
  2. مقدار بده و از داده ها استفاده کن.
  3. برای رشد، از realloc با متغیر موقت استفاده کن.
  4. در پایان، free را فراموش نکن.

برای ادامه مسیر «ساختارها و حافظه»، صفحات اختصاص حافظه، دسترسی به حافظه و تخصیص مجدد را ببین.

جمع بندی سریع

  • ساختار را پویا بساز تا منعطف بمانی.
  • نتیجه malloc و realloc را بررسی کن.
  • بعد از کار، همیشه free بزن.
  • با متغیر موقت از گم شدن آدرس جلوگیری کن.