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 6097759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:04:50+00:00 2026-05-23T13:04:50+00:00

int main() { char** k; k = new char*; char* k1 = abc; char*

  • 0
int main() {

  char** k;

  k = new char*;

  char* k1 = "abc";
  char* k2 = "def";

  *k = k1;
  *(k + 1) = k2;
  delete [] (k + 1);


}

Error: segmentation fault

Could someone explain why I get segmentation fault when freeing (k + 1)? I was able to free k with no problems.

ADD: In the answers it has been said that I can’t delete [] (k + 1) since I haven’t used new on it; But how do you explain the fact that cout<<*(k + 1)<<endl; printed correctly?

  • 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-23T13:04:51+00:00Added an answer on May 23, 2026 at 1:04 pm
    k = new char*;
    

    This allocated storage for only a single char*.

    *(k + 1) = k2;
    

    This tries to pretend there are two char*s allocated. This may not be the site of the segfault, but it is the error.

    delete [] (k + 1);
    

    Here you’re trying to delete[] something you did not new[], another error.

    EDIT: Deep down, memory is allocated in large chunks, such as pages. So when you allocate a small bit of storage, it’s very likely that the memory around it is also valid. It’s still very invalid to access it, though.

    More to the point, when you say something like new char*, this turns into a call to operator new(sizeof(char*)). Let’s say the OS allocates a new 4K page of physical RAM for that at address 0x12340000. The memory manager needs a small structure in there to keep track of the block, something like:

    struct mem_block_info {
        void* next_block;
        size_t block_size;
    };
    

    So it puts this structure at 0x12340000. Immediately after that, it puts the storage you requested, so (assuming this is a 32-bit machine) it returns a pointer of 0x12340008, since sizeof(void*) == sizeof(size_t) == 4. Then it needs to put a header after your storage to track the unused part of that 4K page, so it doesn’t waste memory by allocating another 4K page when you want another char*. That header goes at the address right past the end of your allocated block, 0x1234000C. Once the dust settles, that new char* has put this in memory:

    Address    Data
    0x12340000 0x00000000
    0x12340004 0x00000001
    0x12340008 uninitialized; could be anything
    0x1234000C 0x00000000
    0x12340010 0x00000FF4
    

    The null pointers indicate the end of the allocated and free block linked lists.

    So when you do:

    *(k + 1) = k2;
    

    k + 1 == 0x1234000C is the next_block pointer for the free block, and you just overwrote it with an invalid value (the address of a string in read-only memory, most likely). This does not immediately cause a segmentation fault, but when the memory manager tries to traverse the free block list, it will wind up looking at that string and misinterpreting it as a block header, then going to the next_block from there which is an invalid address, and boom, segfault.

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

Sidebar

Related Questions

#include <cstring> int main() { char *pName = new char[10]; char dummy[] = dummy;
Why does the following code seg fault before returning: int main() { char iD[20];
hi can anybody tell me the error in this? #include<stdio.h> int main() { char
int main() { char *name = new char[7]; name = Dolphin; cout << Your
Let's say I have: sample.c int main (...) { char str*; get s through
#include <stdio.h> #include <string.h> int main() { char* p = new char[10]; memset(p,0,10); printf(%c,*p);
#include<stdio.h> #include<conio.h> int main(){ char i; int c; scanf(%i,&c); scanf(%c,&i);// catch the new line
Here's my program so far: int main() { char choice = 'D'; string inputString;
#include <stdio.h> int main() { char read = ' '; while ((read = getchar())
#include<stdio.h> int main() { char a[5]=hello; puts(a); //prints hello } Why does the code

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.