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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:19:39+00:00 2026-05-16T23:19:39+00:00

Maybe I’m being stupid but I can’t figure out why a pointer is being

  • 0

Maybe I’m being stupid but I can’t figure out why a pointer is being changed in this function. I have included “printf” and “puts” to show the problem. The left braket pointer is changing and invalidates the pointer arithmetic I want to do for string manipulation.

Another question I have – Is it dangerous to expect char pointers to remain constant for the same string when using C functions and do the pointers always point to addresses in a contiguous manner for strings? I can see problems arising with those concerns.

Edit: “//Changes here” placed to function which appears to change the point under my debugging.

char * py_to_c_expr(char * py_expr,int line,bool do_brakets){
    //Recursively converts python expression for use in C
    /*
     Order of evaluation. Top done last
     lambda - Not implemented yet
     if – else - Not implemented yet
     or
     and
     not
     in, not in, is, is not, <, <=, >, >=, <>, !=, == - in, not in and is not, not implemented yet
     |
     ^
     &  Bitwise AND
     <<, >>
     +, -
     *, /, //, %
     +x, -x, ~x - Positive useless
     **
     x[index], x[index:index], x(arguments...), x.attribute - Not implemented yet
     (expressions...), [expressions...], {key:datum...}, `expressions...` - Not implemented yet
     */
    char * find;
    //Convert "or", "and" and "is" to other characters to prevent conversion output confliction during processing
    char * scan;
    if (do_brakets) {
        py_expr = str_replace(py_expr, "or", "OR");
        py_expr = str_replace(py_expr, "and", "AND");
        py_expr = str_replace(py_expr, "is", "IS");
        //Convert brakets
        int nest = 0; //Amount of brakets gone into for processing
        //Stack to keep track of braket positions
        char ** left_bracket_ptr_stack;
        int left_bracket_ptr_stack_size = 0;
        scan = py_expr;
        while (true) {
            char * prior_scan;
            prior_scan = scan;
            scan = strstr(scan, "(");
            if (scan) {
                scan++;
            }
            if (scan) {
                nest++;
                //Entered braket. Add start position to stack
                left_bracket_ptr_stack = realloc(left_bracket_ptr_stack, sizeof(char *)*left_bracket_ptr_stack_size);
                left_bracket_ptr_stack[left_bracket_ptr_stack_size] = scan;
                printf("During left bracket scan:\n\nLeft braket pointer - %lli\nPython expression pointer - %lli\n",left_bracket_ptr_stack[left_bracket_ptr_stack_size],py_expr);
                left_bracket_ptr_stack_size++;
                printf("Length of stack - %i\n\n\n",left_bracket_ptr_stack_size);
            }else{
                scan = prior_scan;
                scan = strstr(scan, ")");
                if (scan) {
                    scan++;
                }
                if (scan) {
                    if (!nest) {
                        printf("Error: On line %i an expression closes too many brakets",line);
                        exit(1);
                    }
                    nest--;
                    //Exited bracket, process expression for braket
                    puts("During right bracket scan and processing:\n\n");
                    int len = scan - left_bracket_ptr_stack[left_bracket_ptr_stack_size - 1];
                    char * inner_braket = malloc(sizeof(char) * (len - 1));
                    strncpy(inner_braket,left_bracket_ptr_stack[left_bracket_ptr_stack_size - 1],len - 1); //Changes here
                    char * bracket_expression = py_to_c_expr(inner_braket,line,true);
                    char * new_py_expr = malloc(sizeof(char) * (strlen(py_expr) - strlen(inner_braket) + strlen(bracket_expression)));
                    free(inner_braket);
                    printf("Left bracket pointer - %lli\n",left_bracket_ptr_stack[left_bracket_ptr_stack_size -1]);
                    printf("Length of stack - %i\n",left_bracket_ptr_stack_size);
                    printf("Python Expression pointer - %lli\n\n\n",py_expr);
                    strncpy(new_py_expr,py_expr,left_bracket_ptr_stack[left_bracket_ptr_stack_size - 1] - py_expr - 1);
                    new_py_expr[left_bracket_ptr_stack[left_bracket_ptr_stack_size- 1] - py_expr - 1] = '\0';
                    strcat(new_py_expr,bracket_expression);
                    free(bracket_expression);
                    strcat(new_py_expr,scan);
                    free(py_expr);
                    py_expr = new_py_expr;
                }else{
                    scan = prior_scan;
                    if (nest) {
                        printf("Error: On line %i an expression does not close all brakets",line);
                        exit(1);
                    }
                    break;
                }
            }
        }
    }
    //Find operators and replace them with the functions. Recursively process each side.
    char * new_expr;
    scan = py_expr; //Reset scan to begining of the expression now that brakets are done
    scan = strstr(scan, "OR");
    if (scan) {
        char * left_side = malloc(sizeof(char) * (scan - py_expr + 1));
        char * right_side = malloc(sizeof(char) * (strlen(py_expr) - strlen(scan)));
        strncpy(left_side,py_expr,scan - py_expr);
        left_side[scan-py_expr] = '\0';
        strcpy(right_side,scan + 3);
        char * new_left_side = py_to_c_expr(left_side,line,false); //Recursively process left
        char * new_right_side = py_to_c_expr(right_side,line,false); //and right operands
        new_expr = malloc(sizeof(char) * (strlen(new_left_side) + strlen(new_right_side) + 5));
        strcpy(new_expr,"or(");
        strcat(new_expr,new_left_side);
        strcat(new_expr,",");
        strcat(new_expr,new_right_side);
        strcat(new_expr,")");
        return new_expr;
    }
    return py_expr;
}

Output:

During left braket scan:

Left braket pointer - 4296016033
Python expression pointer - 4296016032
Length of stack - 1


During left braket scan:

Left braket pointer - 4296016034
Python expression pointer - 4296016032
Length of stack - 2


During left braket scan:

Left braket pointer - 4296016035
Python expression pointer - 4296016032
Length of stack - 3


During right braket scan and processing:


Left braket pointer - 3473436750496411705
Length of stack - 3
Python Expression pointer - 4296016032


Program received signal:  “EXC_BAD_ACCESS”.

Thank you very much for any answer.

  • 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-16T23:19:39+00:00Added an answer on May 16, 2026 at 11:19 pm

    You’re allocating one less pointer than you need here:

    left_bracket_ptr_stack = realloc(left_bracket_ptr_stack, sizeof(char *)*left_bracket_ptr_stack_size);
    

    (it should be (left_brack_ptr_stack_size + 1) that you multiply by).

    In addition:

    • %p is the correct printf format string for printing pointers, not %lli;
    • strncpy() is not really a “safer strcpy()” – it is designed for filling fixed-length text fields, which means that it does not always nul-terminate the destination.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe this is a dumb question, but I have the following behavior in Visual
Maybe this cannot be done, but please help or suggest how this can be
Maybe I just don't know .NET well enough yet, but I have yet to
Maybe the need to do this is a 'design smell' but thinking about another
Maybe this is a dumb question, but is there any way to convert a
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe world peace would be easier, but I have a problem with the widths
Maybe my understanding of agile development isn't as good as it should be, but
Maybe this applied to other Delphi's (I've only used 7). We've got our code
Maybe this is an overarching question as I've seen similar bugs in Firefox and

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.