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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:23:29+00:00 2026-06-04T09:23:29+00:00

I’m writing a program that scans a text file for the byte offset of

  • 0

I’m writing a program that scans a text file for the byte offset of the beginning of each line. The byte offsets are stored in an array of long ints.

The text file is this:

123456
123
123456789
12345
123
12
1
12345678
12345

For testing purposes, the size of the array starts at 2, so that I can test to see if my Realloc function works.

I scan line by line, calculating the length of each line, and storing it as a byte offset. Assume everything before this loop is declared/initialized:

while(fgets(buffer, BUFSIZ, fptr) != NULL)
    {
        otPtr[line] = offset; 
        len = strlen(buffer)*sizeof(char);
        offset += len;
        printf("%03d %03d %03d %s", line, otPtr[line], len, buffer);
        line++;
        if(line == cursize) resize(table, &cursize);
    }

if the line number == the size of the array, the loop is about to use the last array element, so I call resize. In resize(), I want to double the current size of the array of byte offset values.

It’s worth noting that I’m using “Realloc()” rather than “realloc()” because I have a wrapper library that checks for realloc error then exists upon failure.

int resize(long int* table, int* cursize)
{
    long int* ptr;
    *cursize *= 2;
    printf("Reallocating to %d elements...\n", *cursize);
    ptr = (long int*)Realloc(table, (*cursize)*sizeof(long int));
    if(ptr != NULL)
        table = ptr;
}

(Also, is it redundant to wrap realloc inside another function, then wrap it in yet another function? Or is this fine for this purpose? Or should I code the if(ptr!=NULL) into the wrapper?)

My output looks something like this,

Building offset table...
Sizeof(char) in bytes: 1
001 000 008 123456
002 008 005 123
003 013 011 123456789
Reallocating to 8 elements...
004 024 007 12345
005 031 005 123
006 036 004 12
007 040 003 1
Reallocating to 16 elements...
Realloc error
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

Where the three columns are just line number//byte offset//byte length of that line, and the last line is just the printout of the text of that line (this output is from the part where the loop first scans through the file to calculate the offsets, in case it’s not clear, I just printout the buffer to be sure it’s working.)

Why would I get a Realloc error?

Here is the implementation of Realloc:

void *Realloc(void *ptr, size_t numMembers)
{
     void *newptr;

     if ((newptr = (void *) realloc(ptr, numMembers)) == NULL)
     {
         printf("Realloc error");
         exit(1);
     }
     return newptr;
}

Here’s a minimal test-case. Never written one of these before, but I think this is “correct.”

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

void *Realloc(void *ptr, size_t numMembers);

int main(void)
{
    void resize(int** table, int* size);
    int size = 2;
    int j = 15;
    int i = 0;
    int* table;

    table = (int*)calloc(size, sizeof(int));

    while(i<j)
    {
        printf("Give number: ");
        scanf("%d", &table[i]);
        i++;
        if(i == size) resize(&table, &size);
    }
    i = 0;
    printf("Printing table...\n");
    while(i < j)
    {
        printf("%d ", table[i]);
        i++;
    }
}

void *Realloc(void *ptr, size_t numMembers)
{
     void *newptr;

     if ((newptr = (void *) realloc(ptr, numMembers)) == NULL)
     {
         printf("Realloc error");
         exit(1);
     }
     return newptr;
}

void resize(int** table, int* size)
{
        int* ptr;
        *size *= 2;
        printf("Reallocating to %d...\n", *size);
        ptr = (int*)Realloc(*table, *size*sizeof(int));
        if(ptr != NULL)
            table = ptr;
}

What I’ve found is that when I start with size = 2, I get a crash early on. But if I start bigger, say, at 3, it’s very hard to replicate the error. Maybe if I try j = a higher value?

  • 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-04T09:23:31+00:00Added an answer on June 4, 2026 at 9:23 am

    From the realloc man page:

    …returns a pointer to the newly allocated memory, which is
    suitably aligned for any kind of variable and may be different from
    ptr
    , or NULL if the request fails.

    I believe the problem is that you’re throwing away the return value of realloc, which could be an entirely different pointer from what you passed in.

    I see that in your resize function you’re trying to use the return value, but you have a bug in that function that ends up throwing the return value on the floor. Your resize needs to be:

    int resize(long int** pTable, int* cursize)
    {
        long int* ptr;
        *cursize *= 2;
        printf("Reallocating to %d elements...\n", *cursize);
        ptr = (long int*)Realloc(*pTable, (*cursize)*sizeof(long int));
        if(ptr != NULL)
            *pTable = ptr;
    }
    

    and then call it this way:

    if (line == cursize) resize(&table, &cursize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.