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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:28:06+00:00 2026-06-04T01:28:06+00:00

I have a weird problem and I’m not sure how to approach it. I’m

  • 0

I have a weird problem and I’m not sure how to approach it.

I’m reading a 50×50 chars textfile and want to write it into a 50×51 dynamic array (filling the 51th slot with ‘\0’).

Afterwards I’m printing the entire array onto the console.
It should show 50 lines with 50 chars each, as this was the input.

It works quite well too – with the exception of the first line. For some reason is always wrong.

#define FIELD_SIZE 50
int main(int argc, char** args){
    char* data = ReadFile("start.txt");

    char** map = (char**) malloc( FIELD_SIZE );

    if(map==NULL)
        __debugbreak();

    {
        int i;
        for(i = 0; i < FIELD_SIZE; i+=1){
            map[i] = (char*) malloc(FIELD_SIZE+1);

            if(map[i]==NULL)
                __debugbreak();
            //(FIELD_SIZE+1) in order to skip the '\n' at the end of each line.
            memcpy( &map[i][0], &data[i*(FIELD_SIZE+1)], FIELD_SIZE);
            map[i][FIELD_SIZE] = '\0';
        }
    }


    {
        int i;
        for(i = 0; i < FIELD_SIZE; i+=1){
            printf("%s\n", map[i]); //<-- prints something bad for i==0
        }
    }

    free(data);

    return 0;
}

Here’s how my console looks like after program execution:
enter image description here
The first line is supposed to be “aaaaaaaaaaaaa…” too.
So it seems like a bad pointer or something.

If I’m reducing FIELD_SIZE to 20 instead (and reading in a 20×20 text file respectively) it works fine, however.

I don’t see a connection between the array size and the first index not working though. Since malloc never returned 0 there’s no problem with allocation.

I’m using VS2010 C++ to compile the program, but I have to limit myself to the C subset.

  • 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-06-04T01:28:08+00:00Added an answer on June 4, 2026 at 1:28 am

    char** map = (char**) malloc( FIELD_SIZE );

    char** map = (char**) malloc( FIELD_SIZE * sizeof(char*));

    I don’t know if this would be the bug in your code.
    You allocate 50 bytes, but need 50 bytes x 4 bytes/pointer in map.

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

Sidebar

Related Questions

I have this weird problem when trying to INSERT a row into my mysql
I have a weird problem, I want to add a global query using context
I have a weird problem. I have a script that loads images into the
I have a weird problem where after setting nocheck on a foreign constraint and
I have this weird problem with setting up cookies with PHP. Everything worked fine
I have a weird problem that i can't figure out a solution for: I've
I have a weird problem, and I don't know if this is the default
I have a weird problem in converting a string to double in .NET 3.5.
I have a weird problem. I'm using pscp.exe from within a C# program (with
I have a weird problem with my HTML and CSS code: <header> <h1>title</h1> </header>

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.