Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3627278
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:52:06+00:00 2026-05-18T23:52:06+00:00

The code: #include <stdio.h> #include <stdlib.h> int main() { char * cp = malloc(sizeof

  • 0

The code:

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

int main()
{
   char * cp = malloc(sizeof * cp * 20);
   cp="Hello\0";
   free(cp);

    return 0;
}

The error:

*** glibc detected *** ./mallocTest: free(): invalid pointer: 0x000000000040069c ***
======= Backtrace: =========
/lib/libc.so.6(+0x71496)[0x7f92ee448496]
/lib/libc.so.6(cfree+0x6c)[0x7f92ee44d29c]
./mallocTest[0x40059e]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f92ee3f5c3d]
./mallocTest[0x4004b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:08 3162120                            /home/gabriel/Programming/C/mallocTest
00600000-00601000 rw-p 00000000 08:08 3162120                            /home/gabriel/Programming/C/mallocTest
01327000-01348000 rw-p 00000000 00:00 0                                  [heap]
7f92e8000000-7f92e8021000 rw-p 00000000 00:00 0 
7f92e8021000-7f92ec000000 ---p 00000000 00:00 0 
7f92ee3d7000-7f92ee52a000 r-xp 00000000 08:08 2452227                    /lib/libc-2.12.2.so
7f92ee52a000-7f92ee729000 ---p 00153000 08:08 2452227                    /lib/libc-2.12.2.so
7f92ee729000-7f92ee72d000 r--p 00152000 08:08 2452227                    /lib/libc-2.12.2.so
7f92ee72d000-7f92ee72e000 rw-p 00156000 08:08 2452227                    /lib/libc-2.12.2.so
7f92ee72e000-7f92ee733000 rw-p 00000000 00:00 0 
7f92ee733000-7f92ee748000 r-xp 00000000 08:08 33223                      /usr/lib/libgcc_s.so.1
7f92ee748000-7f92ee948000 ---p 00015000 08:08 33223                      /usr/lib/libgcc_s.so.1
7f92ee948000-7f92ee949000 rw-p 00015000 08:08 33223                      /usr/lib/libgcc_s.so.1
7f92ee949000-7f92ee9ca000 r-xp 00000000 08:08 2449430                    /lib/libm-2.12.2.so
7f92ee9ca000-7f92eebc9000 ---p 00081000 08:08 2449430                    /lib/libm-2.12.2.so
7f92eebc9000-7f92eebca000 r--p 00080000 08:08 2449430                    /lib/libm-2.12.2.so
7f92eebca000-7f92eebcb000 rw-p 00081000 08:08 2449430                    /lib/libm-2.12.2.so
7f92eebcb000-7f92eecb7000 r-xp 00000000 08:08 33213                      /usr/lib/libstdc++.so.6.0.14
7f92eecb7000-7f92eeeb6000 ---p 000ec000 08:08 33213                      /usr/lib/libstdc++.so.6.0.14
7f92eeeb6000-7f92eeebe000 r--p 000eb000 08:08 33213                      /usr/lib/libstdc++.so.6.0.14
7f92eeebe000-7f92eeec0000 rw-p 000f3000 08:08 33213                      /usr/lib/libstdc++.so.6.0.14
7f92eeec0000-7f92eeed5000 rw-p 00000000 00:00 0 
7f92eeed5000-7f92eeef3000 r-xp 00000000 08:08 2449712                    /lib/ld-2.12.2.so
7f92ef0be000-7f92ef0c3000 rw-p 00000000 00:00 0 
7f92ef0f1000-7f92ef0f2000 rw-p 00000000 00:00 0 
7f92ef0f2000-7f92ef0f3000 r--p 0001d000 08:08 2449712                    /lib/ld-2.12.2.so                                           
7f92ef0f3000-7f92ef0f4000 rw-p 0001e000 08:08 2449712                    /lib/ld-2.12.2.so                                           
7f92ef0f4000-7f92ef0f5000 rw-p 00000000 00:00 0                                                                                      
7fff6b352000-7fff6b373000 rw-p 00000000 00:00 0                          [stack]                                                     
7fff6b3ff000-7fff6b400000 r-xp 00000000 00:00 0                          [vdso]                                                      
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]                                                  
Aborted

I’m taking that the pointer is getting corrupted somehow before freeing it, but I have no idea how. Doing nothing with it, and freeing it immediately after mallocing it works fine, so I take it I’m putting the string value in wrong.

Sorry to be such a newb. Any help is greatly appreciated.

Oh, and I have no idea why the standard C++ library is being included in that error, but that’s not the problem. I tried compiling it manually is gcc and got the same error, minus the libstdc++. Code::Blocks is being stupid.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:52:06+00:00Added an answer on May 18, 2026 at 11:52 pm
    char * cp = malloc(sizeof * cp * 20);
    

    Here cp is declared as a char* and initialized to point to a dynamically allocated array of 20 char.

    cp="Hello\0";
    

    Now you assign cp to point to a fixed string, losing the previous value of cp. This is where a memory leak is caused.

    free(cp);
    

    This now attempts to free the fixed string, but this is illegal. You can only free memory allocated by malloc (or calloc or realloc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.