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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:25:17+00:00 2026-06-17T19:25:17+00:00

I have following (reduced) simple application which was built with line: gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include

  • 0

I have following (reduced) simple application which was built with line:

gcc -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -D_GNU_SOURCE      -D_XOPEN_SOURCE -std=c99 -ggdb -O0 main-test.c -L/usr/lib -lm -lglib-2.0 -o main-test

The application itself:

#include <stdio.h>
#include <ctype.h>
#include <alloca.h>
#include <glib.h>
#include <glib/gprintf.h>
#include "main.h"

const char *filter(char *original, pt_search_key key) {
    const char *mname = "ModelName";
    const char *pname = "Product";
    const char *delim = " \t";
    const char *delim_str = "\"";
    const char *end = "\n";
    char *value = NULL;
    char *token;
    char *copied = malloc(sizeof(original)+1);
    strcpy(copied, original);

    // Just delete initial tabs and whitespaces
    while(isblank(*copied)) {
        copied++;
        continue;
    }

    token = strsep(&copied, delim);
    if (!strcmp(token, mname))
    {
        token = strsep(&copied, delim_str);
        if(!strcmp(token, "")) {
            token = strsep(&copied, delim_str);

            printf("[before] Token: %s\n", token);
            printf("[before] Value: %s\n", value);
            //*********** Strange behaviour is here!!! *****//
            value = malloc(strlen(token)+1);
            printf("[after] Token: %s\n", token);
            printf("[after] Value: %s\n", value);

            strcpy(value, token);
            token = strsep(&copied, end);
            while(*token != '\0') {
                if(isblank(*token)) {
                    token++;
                    continue;
                } else {
                    printf("String contains unexpected symbols:\n");
                    printf("%s\n", original);
                    exit(EXIT_FAILURE);
                }
           }
        } else {
              printf("String which contains %s is not correct", original);
              exit(EXIT_FAILURE);
        }
    }
return value;
}

int main(int argc, char **argv)
{
    filter("  ModelName \"Business Inkjet 1000\"\n", PT_MODELNAME);
    return EXIT_SUCCESS;
}

This application gives following results:

./main-test 
[before] Token: Business Inkjet 1000
[before] Value: (null)
[after] Token: Business In!
[after] Value: 0

I completely misunderstand why token has been changed. For me output of token should be: "Business Inkjet 1000". Where is error?

  • 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-17T19:25:18+00:00Added an answer on June 17, 2026 at 7:25 pm

    I think this is only a typo, but sizeof(original) is only the size of the pointer in this case.

      char *copied = malloc(sizeof(original)+1);   ??
    

    You need:

      char *copied = malloc(strlen(original)+1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following form, which I have reduced as much as I can,
I have reduced the problem to the following basic function which should simply print
I've reduced my code down to the following, which is as simple as I
I have the following record (reduced for brevity): [DelimitedRecord(,)] [IgnoreFirst] [IgnoreEmptyLines()] public class ImportRecord
I have the following table structure ( reduced of course ): CREATE TABLE `log`
Using gcc 4.6 with -O3, I have timed the following four codes using the
I have reduced a problem I'm having to the following example code: var inQueue
I have reduced my problem to the following test case: create a new workbook;
I have the following piece of code for a very simple PHP polling script
I have the following problem: I have a multithreaded server-side application, where every request

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.