LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   invalid pointer error (https://www.linuxquestions.org/questions/linux-enterprise-47/invalid-pointer-error-824382/)

rahulvishwakarma 08-05-2010 10:57 AM

invalid pointer error
 
/*

hey dude error in gnu c pointer please solve this

I am using Red Hat Enterprise Linux 5.0.

*/

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

int main()
{
char * cp = NULL;

cp = (char*)malloc(11);

cp = "abcdefgh";

printf(" cp = %s \n",cp);

free(cp);

return 0;
}
________________________________________________________________________




gives an error of stack overflow problem :-
_______________________________

*** glibc detected *** ./pointer: free(): invalid pointer: 0x08048510 ***
======= Backtrace: =========
/lib/libc.so.6[0x9a0b16]
/lib/libc.so.6(cfree+0x90)[0x9a4030]
./pointer[0x8048430]
/lib/libc.so.6(__libc_start_main+0xdc)[0x94ddec]
./pointer[0x8048331]
======= Memory map: ========
00110000-00111000 r-xp 00110000 00:00 0 [vdso]
00915000-0092f000 r-xp 00000000 03:05 2524560 /lib/ld-2.5.so
0092f000-00930000 r-xp 00019000 03:05 2524560 /lib/ld-2.5.so
00930000-00931000 rwxp 0001a000 03:05 2524560 /lib/ld-2.5.so
00938000-00a75000 r-xp 00000000 03:05 2524561 /lib/libc-2.5.so
00a75000-00a77000 r-xp 0013d000 03:05 2524561 /lib/libc-2.5.so
00a77000-00a78000 rwxp 0013f000 03:05 2524561 /lib/libc-2.5.so
00a78000-00a7b000 rwxp 00a78000 00:00 0
00d2b000-00d36000 r-xp 00000000 03:05 2524570 /lib/libgcc_s-4.1.2-20080102.so.1
00d36000-00d37000 rwxp 0000a000 03:05 2524570 /lib/libgcc_s-4.1.2-20080102.so.1
08048000-08049000 r-xp 00000000 03:02 32980 /softwares/data/C/pointer
08049000-0804a000 rw-p 00000000 03:02 32980 /softwares/data/C/pointer
09755000-09776000 rw-p 09755000 00:00 0
b7e00000-b7e21000 rw-p b7e00000 00:00 0
b7e21000-b7f00000 ---p b7e21000 00:00 0
b7fde000-b7fe0000 rw-p b7fde000 00:00 0
b7ff5000-b7ff6000 rw-p b7ff5000 00:00 0
bfdd7000-bfdec000 rw-p bfdd7000 00:00 0 [stack]
Aborted



http://stackoverflow.com/questions/ask/submit

paulsm4 08-05-2010 11:25 AM

Code:

/*
 * Hey, dude - Movie recommendation
 */
  // 1) Declare character pointer - good!
  char * cp = NULL;

  // 2) Allocate space to hold up to 10 characters - good!
  cp = (char*)malloc(11);

  // 3) Assign a static string to the pointer.
  //    OK .. but you just undid step 2).  Why?
  //    And you just ORPHANED your allocated block - bad.  Memory leak!
  acp = "abcdefgh";

  // 4) Print the string - OK
  printf(" cp = %s \n",cp);

  // 5) Free the string - bad!
  //    cp now points to the static string - NOT the allocated block!
  free(cp);


unSpawn 08-06-2010 11:18 AM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.


All times are GMT -5. The time now is 01:19 PM.