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

  • Home
  • SEARCH
  • 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 6983205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:23:18+00:00 2026-05-27T18:23:18+00:00

My code gives me segfault error: which I don’t understand,the debugger says error comes

  • 0

My code gives me segfault error: which I don’t understand,the debugger says error comes from printing the value from stored_

char *stored_ = NULL;
char testMessage[15];

//strcpy(stored_, testMessage);

for (int a = 0;a < 10; a++)
{
    sprintf(testMessage,"Message::%i\n",a);
    printf("string is:%s;length is %i\n",testMessage,strlen(testMessage));

    stored_ = (char*) realloc (stored_, sizeof(char) * (strlen(testMessage) * (a+1) ));

    strcpy(&stored_[a], testMessage);
} 

for (int b = 0;b < 10; b++)
{
    printf("inside:|%s|\n",stored_[b]);
}
  • 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-27T18:23:19+00:00Added an answer on May 27, 2026 at 6:23 pm

    Fist up, sizeof(char) is always 1, you don’t need to multiply by it.

    Secondly, when you’re allocating room for a string, you have to use:

    malloc (strlen (string) + 1);
    

    In other words, you need room for the null byte at the end.

    Thirdly, you appear to be confused between character pointers and character pointer pointers. stored_ is a single block of characters and stored_[1] is only one byte beyond stored_[0], meaning you won’t have enough room to store the string.

    stored_[n], n=:   0   1   2   3
                    +---+---+---+---+
                    |   |   |   |   |...
                    +---+---+---+---+
                    each of these cells is a single byte.
    

    You’ll either have to manage the single block of characters yourself, leaving enough room for each element (by using sparse indexes), or have a block of character pointers with indexes 0, 1, 2 and so on, but you’ll then have to manage the string allocations separately.

    The following code shows how to do this latter one:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void) {
        // An array of char pointers (C strings).
    
        char **stored_ = NULL;
        char testMessage[15];
        int i;
    
        // Populate them.
    
        for (i = 0; i < 10; i++) {
            sprintf (testMessage,"Message::%i",i);
            printf ("string is:%s;length is %i\n",testMessage,strlen(testMessage));
    
            // Reallocate array of char *, allocate room for string, then store it.
    
            stored_ =  realloc (stored_,sizeof (char*) * (i + 1));
            stored_[i] = malloc (strlen (testMessage) + 1);
            strcpy (stored_[i], testMessage);
        }
    

    That’s the meat of it, the allocation of the array of character pointers separate from the actual arrays of characters forming the C strings.

    Then the code below prints them and cleans up.

        // Print them.
    
        for (i = 0; i < 10; i++) {
            printf("inside:|%s|\n",stored_[i]);
        }
    
        // Free all memory and return.
    
        for (i = 0; i < 10; i++) {
            free (stored_[i]);
        }
        free (stored_);
    
        return 0;
    }
    

    The output being, as expected:

    string is:Message::0;length is 10
    string is:Message::1;length is 10
    string is:Message::2;length is 10
    string is:Message::3;length is 10
    string is:Message::4;length is 10
    string is:Message::5;length is 10
    string is:Message::6;length is 10
    string is:Message::7;length is 10
    string is:Message::8;length is 10
    string is:Message::9;length is 10
    inside:|Message::0|
    inside:|Message::1|
    inside:|Message::2|
    inside:|Message::3|
    inside:|Message::4|
    inside:|Message::5|
    inside:|Message::6|
    inside:|Message::7|
    inside:|Message::8|
    inside:|Message::9|
    

    With this method, each cell is a pointer to an array of characters, separately allocated (which holds the C string):

    stored_[n], n=:   0   1   2   3
                    +---+---+---+---+
                    |   |   |   |   |...
                    +---+---+---+---+
                      |   |   |   |     +----------------------+
                      |   |   |   +---> | character array here |
                      |   |   |         +----------------------+
                      |   |   |         +----------------------+
                      |   |   +-------> | character array here |
                      |   |             +----------------------+
                      |   |             +----------------------+
                      |   +-----------> | character array here |
                      |                 +----------------------+
                      |                 +----------------------+
                      +---------------> | character array here |
                                        +----------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code gives me this error: Cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List'. How
In C++ the following code gives a compiler error: void destruct1 (int * item)
This piece of code gives a syntax error at the colon of elif process.loop(i,
Can anyone explain why this code gives the error: error C2039: 'RT' : is
The code below gives me this mysterious error, and i cannot fathom it. I
The code below gives an error: Property 'Int32 Key' is not defined for type
Following code gives a red squigly under emp.(col.ColummnName). Error is identifier expected foreach (DataColumn
why the below code gives me error of double free or corruption... when i
The following code gives me a really weird error in Firefox: Error: uncaught exception:
This code gives compilation error: import scala.util.continuations._ object CTest { def loop: Nothing =

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.