Hello all,
This is my first post...I'm just starting to learn C and going through some of the beginner tutorials at cprogramming.com. The following code is giving me an error at run time
I don't receive any error messages when compiling with the command "gcc -Wall -o ex2 ex2.c" but when attempting to run, I get the following error message:Code:#include <stdio.h> #include <stdlib.h> int main() { int x,z; int *p; /* initialize a pointer to an integer */ int *p2 = malloc(sizeof(*p2)); /* dynamic allocation of memory */ x = 12; z = 29; p = &x; p2 = &z; printf("The value from pointer p at memory address %p is %d\n\n", p, *p); printf("The value from pointer p2 at memory address %p is %d\n\n", p2, *p2); /* release dynamic memory allocation and set pointers to 0 */ free(p2); p = 0; p2 = 0; getchar(); return 0; }
Any ideas where I'm going wrong? Once I comment out the free() function and re-compile, the program will run fine. I'm running on Fedora 10, 64 bit.Code:*** glibc detected *** ./ex2: free(): invalid pointer: 0x00007fffaa879b38 *** ======= Backtrace: ========= /lib64/libc.so.6[0x31b8877ec8] /lib64/libc.so.6(cfree+0x76)[0x31b887a486] ./ex2[0x40061b] /lib64/libc.so.6(__libc_start_main+0xe6)[0x31b881e576] ./ex2[0x4004e9] ======= Memory map: ======== 00400000-00401000 r-xp 00000000 fd:00 5865751 /Develop/c/practice/ex2 00600000-00601000 rw-p 00000000 fd:00 5865751 /Develop/c/practice/ex2 00857000-00878000 rw-p 00857000 00:00 0 [heap] 31b8400000-31b8420000 r-xp 00000000 fd:00 8511798 /lib64/ld-2.9.so 31b861f000-31b8620000 r--p 0001f000 fd:00 8511798 /lib64/ld-2.9.so 31b8620000-31b8621000 rw-p 00020000 fd:00 8511798 /lib64/ld-2.9.so 31b8800000-31b8968000 r-xp 00000000 fd:00 8511800 /lib64/libc-2.9.so 31b8968000-31b8b68000 ---p 00168000 fd:00 8511800 /lib64/libc-2.9.so 31b8b68000-31b8b6c000 r--p 00168000 fd:00 8511800 /lib64/libc-2.9.so 31b8b6c000-31b8b6d000 rw-p 0016c000 fd:00 8511800 /lib64/libc-2.9.so 31b8b6d000-31b8b72000 rw-p 31b8b6d000 00:00 0 31c3a00000-31c3a16000 r-xp 00000000 fd:00 8511828 /lib64/libgcc_s-4.3.2-20081105.so.1 31c3a16000-31c3c16000 ---p 00016000 fd:00 8511828 /lib64/libgcc_s-4.3.2-20081105.so.1 31c3c16000-31c3c17000 rw-p 00016000 fd:00 8511828 /lib64/libgcc_s-4.3.2-20081105.so.1 7f3d9c000000-7f3d9c021000 rw-p 7f3d9c000000 00:00 0 7f3d9c021000-7f3da0000000 ---p 7f3d9c021000 00:00 0 7f3da2860000-7f3da2862000 rw-p 7f3da2860000 00:00 0 7f3da287a000-7f3da287d000 rw-p 7f3da287a000 00:00 0 7fffaa867000-7fffaa87c000 rw-p 7ffffffea000 00:00 0 [stack] 7fffaa9fe000-7fffaa9ff000 r-xp 7fffaa9fe000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall] Aborted
Thanks in advance for your time!



LinkBack URL
About LinkBacks



