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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:30:04+00:00 2026-06-05T17:30:04+00:00

this code gives an instant seg fault,I tried placing printf’s all over, I even

  • 0

this code gives an instant seg fault,I tried placing printf’s all over, I even tried to printf something just after the int=0; line but no matter what I did, it does not print anything but a segmentation fault. The file exists, also its location is the same with where I do the execution.
The file includes city names, one name on each line, nothing else, how do I read them and store them in an array :/
what if there was a number after each city, would the reading still be the same?

NewYork 5
LosAngeles 12
California 7

and the code;

    int i=0;

    char **city_names = malloc(sizeof(char*));

    FILE* fp;
    fp = fopen("abc.txt","r");

    while(!feof(fp)){

        city_names[i] = realloc(city_names[i],sizeof(char)*255);
        fscanf(fp,"%s",city_names[i]);
        i++;
    }

    fclose(fp);
  • 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-05T17:30:05+00:00Added an answer on June 5, 2026 at 5:30 pm

    You are only allocating a single char * of memory in your malloc then accessing beyond it in the while loop.

    If you are going to do a 2D malloc array, you need to malloc each pointer, then malloc assign a malloc into each to the max string size (yuck).

    char **city_names = malloc(sizeof(char *) * kNumCities);
    
    for(int i = 0; i &lt kNumCities; i++)
      city_names[i] = malloc(sizeof(char) * kMaxStringSize);
    

    Or do something like char city_name[3][256] instead to get it up and running.

    I’d also like to add that this sort of reading is very unsafe. You are reading an unknown amount of bytes into a fixed buffer size. If the string you read in were more than 255 bytes, you will destroy memory. You’d be better off using an fread() into a fixed size buffer type solution (or ftell() then file and read it all in at once for best efficiency) and then do your reading off the buffer. Not to mention all the overhead of malloc and realloc (they do add up).

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

Sidebar

Related Questions

This code gives me a seg fault, but when I change the x-- to
In this code, for vector size, n >=32767, it gives segmentation fault, but upto
This code gives something like ../movies/1.png , the exact src attribute: $(img).attr(src); How do
This code gives me a leak - over 100 mb by 100 iterations. If
does this code gives me the last inserted id of the record, even on
Just for curiosity: why this code gives an AV instead of throwing an exception?
This code gives me this error:Cannot implicitly convert type ArrayList[] to ArrayList[][] at this
This code gives me an error: uses SDL uses SDLGraphics uses SDLImage screen: unowned
This code gives me an empty result. I expect it to print out the
I am not sure why this code (gives stackoverflow at runtime) compiles: import java.io.*;

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.