CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Posts
    11

    Im getting a 'memory corruption' with malloc

    Hi.
    Im trying to malloc memory so that I can create an array of characters, making a word, but when the execution gets to my malloc line, it complains hardcore. (This is C btw, not C++)

    Here is the portion of code it gets too, and here is the output dump it gives. Any ideas?:

    counter = 0;
    nullChecker = 1;
    while( nullChecker != 0 )
    {
    nullChecker = wordArray[codeWord+offset+counter+1];
    counter++;
    }
    printf("counter value: %d\n",counter);
    // execution gets here, setting counter to 6 (observed from printf above)
    unsigned char *string = malloc(counter + 1);
    // using gdb, the program gets here, and suddenly breaks after executing the previous line...
    int k;
    for(k = 0; k < counter; k++){
    unsigned int letterTmp;
    letterTmp = wordArray[codeWord+offset+k+1];
    printf("string: $s", letterTmp);
    string[k] = (char)letterTmp;
    }





    the output is:
    *** glibc detected *** ./a.out: malloc(): memory corruption: 0x09eee098 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x4f6016]
    /lib/libc.so.6(__libc_malloc+0x95)[0x4f7765]
    ./a.out[0x8048c5f]
    ./a.out[0x80492aa]
    /lib/libc.so.6(__libc_start_main+0xe5)[0x49a6e5]
    ./a.out[0x80484f1]
    ======= Memory map: ========
    001b5000-001b6000 r-xp 001b5000 00:00 0 [vdso]
    0045f000-0047f000 r-xp 00000000 08:03 1815090 /lib/ld-2.9.so
    00480000-00481000 r--p 00020000 08:03 1815090 /lib/ld-2.9.so
    00481000-00482000 rw-p 00021000 08:03 1815090 /lib/ld-2.9.so
    00484000-005f2000 r-xp 00000000 08:03 1815105 /lib/libc-2.9.so
    005f2000-005f4000 r--p 0016e000 08:03 1815105 /lib/libc-2.9.so
    005f4000-005f5000 rw-p 00170000 08:03 1815105 /lib/libc-2.9.so
    005f5000-005f8000 rw-p 005f5000 00:00 0
    00dab000-00db8000 r-xp 00000000 08:03 1815184 /lib/libgcc_s-4.3.2-20081105.so.1
    00db8000-00db9000 rw-p 0000c000 08:03 1815184 /lib/libgcc_s-4.3.2-20081105.so.1
    08048000-0804a000 r-xp 00000000 08:13 16499129 /home/csu/sbb27/cs520/Prog2/a.out
    0804a000-0804b000 rw-p 00001000 08:13 16499129 /home/csu/sbb27/cs520/Prog2/a.out
    09eee000-09f0f000 rw-p 09eee000 00:00 0 [heap]
    b7f00000-b7f21000 rw-p b7f00000 00:00 0
    b7f21000-b8000000 ---p b7f21000 00:00 0
    b8090000-b8092000 rw-p b8090000 00:00 0
    b80a3000-b80a4000 rw-p b80a3000 00:00 0
    bfb3a000-bfb4f000 rw-p bffeb000 00:00 0 [stack]
    Abort




    Thank you in advanced...

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Im getting a 'memory corruption' with malloc

    You're corrupting the heap at some point before that line. Can't tell you where, but the most probable how is that you're writing past the end of a heap-allocated array somewhere.

  3. #3
    Join Date
    Sep 2009
    Posts
    11

    Re: Im getting a 'memory corruption' with malloc

    Im kind of new to malloc, my professor pretty much just told us to use it earlier in the program for a different case, so i took what we did there, and just changed around some values... Im confused however on... why would this be giving me an error at this particular line where all im doing is setting a variable *string to an allocated memory space?

    i tried:
    printf("--debugg tester!--\n");
    unsigned char *string = malloc(counter + 1);
    printf("--debugg tester!!--\n");

    and it only printed out the top statement, thats how i pretty much narrowed it down to this line :-\

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Im getting a 'memory corruption' with malloc

    If you write to memory you don't own accidentally, then you could be destroying vital data structures which the heap manager needs.

    However, this damage isn't detected until you try to do a malloc() or free() call in many cases.

    If you were on Linux I'd say run Valgrind since that's really good at detecting these sorts of errors, but since you're in the Visual C++ forum, I'm guessing you're on Windows. If you show us the entire program then maybe we could help you find the trouble.

  5. #5
    Join Date
    Sep 2009
    Posts
    11

    Re: Im getting a 'memory corruption' with malloc

    Hmm. actually I think i took a different route from the malloc method.. rather than making a string, i just simply printed 1 character at a time, visually doing the same thing my program needs to.

    however, it seg faults when i try to print the characters... any idea? I have a feeling its because im using printf("%s") but who knows.

    here is the code:
    case 248: //newstr
    printf("%s%d newstr ", indent, offset);
    counter = 0;
    nullChecker = 1;
    while( nullChecker != 0 ) //checks for when the end of string null character comes.
    {
    nullChecker = wordArray[codeWord+offset+counter+1];
    counter++;
    }

    int k;
    for(k = 0; k < counter-1; k++){
    unsigned char letterTmp;
    letterTmp = wordArray[codeWord+offset+k+1];
    printf("%s", letterTmp);
    }
    printf("debugg tester\n");
    offset += counter;
    break;



    Thanks again,
    Sean

  6. #6
    Join Date
    Sep 2009
    Posts
    11

    Re: Im getting a 'memory corruption' with malloc

    im sorry, i just found out i had to use %c :-D

    Thank you so much for all the help Lindley

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Im getting a 'memory corruption' with malloc

    Quote Originally Posted by UNHCoder View Post
    Hi.
    Im trying to malloc memory so that I can create an array of characters, making a word, but when the execution gets to my malloc line, it complains hardcore. (This is C btw, not C++)
    What is a "seg fault"? What is a "glibc"? What is this ".so" file?

    I'm asking this, since this is the Visual C++ forum. If you're not using Visual C++, please post in the non-Visual C++ forum.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured