title: C: glibc detected malloc(): memory corruption: ... segfault
body: <p>I am trying to run a program I have in C.  I have a stack ADT I made as part of this program and when I try to make a new stack (which is only done once), I get <code>*** glibc detected *** ./lcs_prog: malloc(): memory corruption: 0x00000000007e10c0 ***</code></p>
  
  <p>the function being called is newStack() defined as</p>
  
  <pre><code>StackP newStack(void)
  {
      StackP temp;
      temp = (StackP)malloc(sizeof(StackT));
      temp-&gt;size = 0;
      return temp;
  }
  </code></pre>
  
  <p>where StackP is defined in a header file as <code>typedef struct StackT *StackP;</code> and the stucture is </p>
  
  <pre><code>typedef struct StackT
  {
      StackItem content[MAX_SIZE];
      int size;
  }StackT;
  </code></pre>
  
  <p>I use malloc in three other places (two char* and one int*) in the program which successfully complete by the time this error occurs.  the program immediately segfaults after a call to newStack().</p>
  
  <p><strong>Output from valgrind:</strong></p>
  
  <pre><code>[user@cs-ssh1 student-p5]$ valgrind --leak-check=yes ./lcs_prog
  ==13885== Memcheck, a memory error detector
  ==13885== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
  ==13885== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
  ==13885== Command: ./lcs_prog
  ==13885==
  ==13885==
  ==13885== Warning: client switching stacks?  SP change: 0x7fee17d60 --&gt; 0x7ff0001f0
  ==13885==          to suppress, use: --max-stackframe=2000016 or greater
  First string:  ABCBDAB
  Second string: BDCABA
  ==13885== Invalid write of size 4
  ==13885==    at 0x400F54: lengthLCS (part1.c:61)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b408 is 0 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid read of size 4
  ==13885==    at 0x401071: lengthLCS (part1.c:70)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b408 is 0 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid write of size 4
  ==13885==    at 0x4010B1: lengthLCS (part1.c:71)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b40c is 4 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid write of size 4
  ==13885==    at 0x40102A: lengthLCS (part1.c:68)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b418 is 16 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid read of size 4
  ==13885==    at 0x401025: lengthLCS (part1.c:68)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b408 is 0 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid read of size 4
  ==13885==    at 0x40104F: lengthLCS (part1.c:70)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b410 is 8 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid read of size 4
  ==13885==    at 0x4010AF: lengthLCS (part1.c:71)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b410 is 8 bytes after a block of size 168 alloc'd
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400F2B: lengthLCS (part1.c:58)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==
  ==13885== Invalid read of size 4
  ==13885==    at 0x40112E: lengthLCS (part1.c:77)
  ==13885==    by 0x40121B: printResults (part1.c:100)
  ==13885==    by 0x40139D: main (main.c:18)
  ==13885==  Address 0x4c1b43c is not stack'd, malloc'd or (recently) free'd
  ==13885==
  Length of LCS: 4
  --13885-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting
  --13885-- si_code=1;  Faulting address: 0x304C1B42A;  sp: 0x402f08dd0
  
  valgrind: the 'impossible' happened:
     Killed by fatal signal
  ==13885==    at 0x3803EC04: vgPlain_arena_malloc (m_mallocfree.c:291)
  ==13885==    by 0x38003C34: vgMemCheck_new_block (mc_malloc_wrappers.c:263)
  ==13885==    by 0x3800409A: vgMemCheck_malloc (mc_malloc_wrappers.c:301)
  ==13885==    by 0x3807A58A: vgPlain_scheduler (scheduler.c:1665)
  ==13885==    by 0x380A5A19: run_a_thread_NORETURN (syswrap-linux.c:103)
  
  sched status:
    running_tid=1
  
  Thread 1: status = VgTs_Runnable
  ==13885==    at 0x4A069EE: malloc (vg_replace_malloc.c:270)
  ==13885==    by 0x400B2D: newStack (stack.c:32)
  ==13885==    by 0x401153: stringLCS (part1.c:85)
  ==13885==    by 0x401236: printResults (part1.c:101)
  ==13885==    by 0x40139D: main (main.c:18)
  
  
  Note: see also the FAQ in the source distribution.
  It contains workarounds to several common problems.
  In particular, if Valgrind aborted or crashed after
  identifying problems in your program, there's a good chance
  that fixing those problems will prevent Valgrind aborting or
  crashing, especially if it happened in m_mallocfree.c.
  
  If that doesn't help, please report this bug to: www.valgrind.org
  
  In the bug report, send all the above text, the valgrind
  version, and what OS and version you are using.  Thanks.
  </code></pre>
  
  <p>The lengthLCS malloc errors are likely from me accessing an integer array that was made using malloc (successfully).</p>
  
  <p>Any help in this issue would be greatly appreciated.  Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After reviewing my code, I found I had an off-by-one error in my lengthLCS function (not posted) I was going outside the bounds of a malloc'd array of integers.  Fixing this has fixed the problem.  Thank you for your time and comments.</p>