الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في تحرير الذاكره لمؤشر struct tm

بدأه mhewedy في 10 يوليو 2010 · 4 رد · 406 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم...

عندي مشكله عند تحرير مؤشر ل struct tm

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

int main(void){

	int year=1990, mon=1, day=1;
	struct tm* age_tm;
	time_t curr_time_t, age_t;
	printf("%p\n", age_tm);

	time(&curr_time_t);

	age_tm = localtime(&curr_time_t);
	printf("%p\n", age_tm);

	age_tm->tm_year = year -1900;
	age_tm->tm_mon = mon-1;
	age_tm->tm_mday = day;

	age_t = mktime(age_tm);

	age_t = curr_time_t - age_t;

	if (age_t < 0){
		printf("INVALID DATE ENTRED!\n");
		exit(-1);
	}

	printf("%p\n", age_tm);

	free(age_tm);


	age_tm = localtime(&age_t);

	printf("you have %i year(s), %i month(s) and %i day(s)\n", (age_tm->tm_year)-70, age_tm->tm_mon, age_tm->tm_mday);


	return 0;
}

فكيف أستطيع أن أحرر المؤشر؟؟

الناتج :

0xd2b324
0xd2e6a0
0xd2e6a0
*** glibc detected *** a.out: free(): invalid pointer: 0x00d2e6a0 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xc40591]
/lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0xc41de8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xc44ecd]
a.out[0x8048609]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xbebbd6]
a.out[0x8048481]
======= Memory map: ========
0081c000-00837000 r-xp 00000000 08:07 997497 	/lib/ld-2.11.1.so
00837000-00838000 r--p 0001a000 08:07 997497 	/lib/ld-2.11.1.so
00838000-00839000 rw-p 0001b000 08:07 997497 	/lib/ld-2.11.1.so
00bd5000-00d28000 r-xp 00000000 08:07 1022751	/lib/tls/i686/cmov/libc-2.11.1.so
00d28000-00d29000 ---p 00153000 08:07 1022751	/lib/tls/i686/cmov/libc-2.11.1.so
00d29000-00d2b000 r--p 00153000 08:07 1022751	/lib/tls/i686/cmov/libc-2.11.1.so
00d2b000-00d2c000 rw-p 00155000 08:07 1022751	/lib/tls/i686/cmov/libc-2.11.1.so
00d2c000-00d2f000 rw-p 00000000 00:00 0 
00e72000-00e8f000 r-xp 00000000 08:07 997555 	/lib/libgcc_s.so.1
00e8f000-00e90000 r--p 0001c000 08:07 997555 	/lib/libgcc_s.so.1
00e90000-00e91000 rw-p 0001d000 08:07 997555 	/lib/libgcc_s.so.1
00f5e000-00f5f000 r-xp 00000000 00:00 0      	[vdso]
08048000-08049000 r-xp 00000000 08:09 1121464	/home/mhewedy/c_work/a.out
08049000-0804a000 r--p 00000000 08:09 1121464	/home/mhewedy/c_work/a.out
0804a000-0804b000 rw-p 00001000 08:09 1121464	/home/mhewedy/c_work/a.out
09962000-09983000 rw-p 00000000 00:00 0      	[heap]
b7700000-b7721000 rw-p 00000000 00:00 0 
b7721000-b7800000 ---p 00000000 00:00 0 
b78ad000-b78ae000 rw-p 00000000 00:00 0 
b78c4000-b78c7000 rw-p 00000000 00:00 0 
bfd52000-bfd67000 rw-p 00000000 00:00 0      	[stack]
Aborted
#2

اظن ان localtime() تقوم بارجاع const للموشر وليس قيمة عادية يمكن تحريرها .

تم تعديل هذه المشاركة بواسطة apex في 10 يوليو 2010 في 18:55

name : mohamedyosry

#3

http://cplusplus.com/reference/clibrary/ctime/localtime/

طبقا للرابط, فأعتقد أنه لا, لا ترجع const ..

وحتي لو أنها ترجع const , فهلا لا أستطيع أن أحرره ؟؟؟!

#4

السلام عليكم ...

أخ هويدي, انظر لتعريف الدالة هنا:

http://cplusplus.com/reference/clibrary/ctime/localtime/

Return Value

A pointer to a tm structure with the time information filled in.

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

بمعنى أبسط, العائد من الدالة عبارة عن static variable في الـ function نفسها. و هذا يعني أنك كمبرمج لست مسؤولاً عن الذاكرة العائدة, المفروض أن تقوم بالاستخدام فقط. أي أنك تقوم بتنفيذها فتقوم الدالة بتغيير الـ structure الذي هو عبارة عن static variable في الدالة, و من ثم تقوم بإرجاع مؤشر له.

تم تعديل هذه المشاركة بواسطة Khaled.Alshaya في 10 يوليو 2010 في 21:37

1
#5

شكرا أخي خالد على التوضيح...

حقيقة قرأتها ولم أفهمها....

جزاك الله خيرا و غفر لك..

مواضيع مشابهة