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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:28:18+00:00 2026-05-13T06:28:18+00:00

I’ve got a bit problem. My program throws segmentation fault when returning zero in

  • 0

I’ve got a bit problem. My program throws segmentation fault when returning zero in main.

The main function looks like this:

int main(int argc, char* argv[]){
    ifstream fs("test.dat", ios::binary);
    cSendStream sendstr(&fs,20);

    char *zomg=sendstr.data();
    //zomg[20]=0;

    sendstr.read(20);

    cout<<"Buffer: "<<sendstr.data()<<endl;
    cout<<"Remaining: "<<sendstr.dataAvailable()<<endl;

    sendstr.read(2);
    cout<<"Buffer: "<<zomg<<endl;
    cout<<"Remaining: "<<sendstr.dataAvailable()<<endl;

    sendstr.read(10);
    cout<<"Buffer: "<<zomg<<endl;
    cout<<"Remaining: "<<sendstr.dataAvailable()<<endl;
    cout<<"end..."<<endl;
    return 0;
}

The commented zomg part is the point which makes the program crash. zomg is pointing on char[20]. My point on that line is to set the end of the array because somehow if I don’t do that the stream reads more data than 20 bytes, but it prints just one unnecessary symbol though.

Funny thing is that even if I write there some additional code between this and return 0 it throws fault first when returning.

Just for the case you wanted to see the cSendStream class:

cSendStream.h:

class cSendStream{
  public:
    cSendStream(std::istream*, int streamsize);
    int read(int);
    int dataAvailable();
    char* data();
  private:
    void shift(int);

    std::istream *source;
    int streamsize;
    char* buffer;
};

and the cSendStream.cpp:

#include "cSendStream.h"

cSendStream::cSendStream(std::istream *src, int size){
    source=src;
    streamsize=size;
    buffer=new char[streamsize];
    memset(buffer,0,streamsize);
}

int cSendStream::read(int i){
    if(dataAvailable()<1 || i<=0){
        return 0;
    }
    if(i>dataAvailable()){
        i=dataAvailable()-1;
    }
    if(i>streamsize){
        i=streamsize;
    }

    shift(i);
    source->read(&(buffer[streamsize-i]),i);
    return i;
}

int cSendStream::dataAvailable(){
    int current=source->tellg();
    source->seekg (0, std::ios::end);
    int available = (int)(source->tellg())-current;
    source->seekg (current);

    return available;
}

char* cSendStream::data(){
    return buffer;
}

void cSendStream::shift(int i){
    char tmp[2048];
    memcpy(tmp,buffer,streamsize);
    memcpy(&(buffer[0]),&(tmp[i]),streamsize-i);
}
  • 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-13T06:28:18+00:00Added an answer on May 13, 2026 at 6:28 am

    zomg[20]=0 is writing one past the end of the allocated array, but it is difficult to guess why the segfault is occurring. My guess is that your clever compiler is using alloca for the allocation and you are scribbling on the return address.

    It might be fun to look at the assembly (usually -S) to see what’s happening.

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

Sidebar

Ask A Question

Stats

  • Questions 278k
  • Answers 278k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer _canExecute would be a Predicate<object>, whereas _execute would be an… May 13, 2026 at 3:12 pm
  • Editorial Team
    Editorial Team added an answer Quoting from the PHP Manual on Language Operators The +… May 13, 2026 at 3:12 pm
  • Editorial Team
    Editorial Team added an answer Execute the search command, supplying the bug ID as the… May 13, 2026 at 3:12 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.