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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:18:25+00:00 2026-06-14T16:18:25+00:00

Got a huge problem. Let’s say, that I have pointer to a large string

  • 0

Got a huge problem. Let’s say, that I have pointer to a large string (~1500 characters) and I want to print it. What I get in the result is only a part of that string. After the program prints that part it asks me for a newline. After I press enter the program prints the rest of the string and terminates. It happens when I try to run my program in terminal or with IDE. If I try to debug it, it works perfectly. I need it to print whole string without terminating the program. So, here’s the code (there’s lots of mistakes in the code):

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<string.h>

#define SOCKET int        // Apibreziame sinonima 'int' tipui, kad butu
                          // unix ir WIN sistemoms bendras kodas dirbant
                          // su soketu deskriptoriais.
#define closesocket close // Apibreziame sinonima close () funkcijai
                          // closesocket (), kuris yra analogas Windows
                          // tipo sistemose. Tai del kodo suvienodinimo.
#define INVALID_SOCKET -1 // Apibreziame del kodo suvienodinimo, klaidos
#define SOCKET_ERROR -1   // situacijai nusakyti konstantas, kurios WINSOCK'o

#define MAX_CH 255
#define POP3_PORT 110
#define MAXLEN 1024

int main()
{
    SOCKET socketE;
    struct sockaddr_in servaddr;
    struct hostent* h = NULL;

    char* Packet = NULL;

    Packet = (char*)malloc(MAX_CH);
    memset(Packet, 0, sizeof(Packet));

    printf("POP3 server: ");
    gets(Packet);

    /*******{ 
    client connects to the mail server and other stuff
    server sends greeting message
    client receives greeting message 
    }*******/

    if (!strstr(Packet,"+OK")) {
        print_error(Packet);
    }
    else {
        printf("%s", Packet);
        int printedData = 0;
        while (1) {
            Packet = (char*)realloc(NULL, 1);
            memset(Packet, 0, sizeof(Packet));
            gets(Packet);
            strcat(Packet, "\n");
            if (SOCKET_ERROR == SendPacket(&socketE, Packet)) {
                print_error("Error on sending data to mail server.\n");
            }
            memset(Packet, '\0', sizeof(Packet));
            if (SOCKET_ERROR == ReceivePacket(&socketE, &Packet)) {
                print_error("Error on receiving data from mail server.\n");
            }
            if (!strstr(Packet,"+OK")) {
                fwrite(Packet, 1, strlen(Packet), stdout);
                break;
            }
            printf("%s", Packet);  //THIS IS WHERE THE PRINT PART COMES
        }
    }
    close(socketE);
    return 0;
}

Okey, so I have found out that this if clause (I mean, even if this if is false) stops program from printing all the content. If I remove/comment it, I got my program working correctly. Is it possible, that this one causes all the problems?

if (!strstr(Packet,"+OK")) {
    fwrite(Packet, 1, strlen(Packet), stdout);
    break;
}
  • 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-14T16:18:27+00:00Added an answer on June 14, 2026 at 4:18 pm

    This:

    Packet = (char*)realloc(NULL, 1);
    memset(Packet, 0, sizeof(Packet));
    

    causes undefined behavior. It allocates a single byte (why would you ever do this?) and then clears sizeof(Packet) bytes, which on most typical systems today is either 4 or 8 bytes. Thus, it writes outside the allocated memory. Note that sizeof cannot track dynamic allocations, so this whole pattern is wrong.

    Also, please note that dynamic memory allocation can fail; you must check the return value of every call to realloc() before relying on the returned pointer being valid. If it’s NULL, you can’t access through it.

    Further, just like with malloc(), please don’t cast the return value of realloc() in C.

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

Sidebar

Related Questions

Suppose that you have two huge files (several GB) that you want to concatenate
i got the following problem. I want to have an activity thats shows me
I have this GUI that shows, let's say Customer Orders. When my client nailed
I got a huge problem trying to color objects that are flying around in
I've got the following design problem: Say I have 1 million of plain text
I have this huge problem with memory management. The problem : I've got a
I have got huge problem with my Android app and I would like to
Guys i have a little problem , huge for me , so that is
I've got a huge problem. I made a special ID for the things in
I have got a huge tab-seperated file with up to 200 million rows (normally

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.