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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:24:51+00:00 2026-06-16T22:24:51+00:00

I am debugging a C code where I have a pointer inside a pointer

  • 0

I am debugging a C code where I have a pointer inside a pointer to a struct named board. There is a function where I am printing the board:

static void board_print(board *b){
    int i,j;
    char data;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            data = b->data[i * size + j];
            if(data){
                printf("X ");
            }else{
                printf("O ");
            }
        }
        printf("\n");
    }
}

Here is the weird part. When I hit my breakpoint initially at the beginning of first for loop, everything is OK, my data is correct, all the pointers work etc. as seen below:

first

Then, I step into the loop for the first time, with i and j equal to zero, and b->data[0] should be perfectly valid, as it was two steps ago. Suddenly, as soon as I step on the line data = b->data[i * size + j];, the data pointer changes into a null pointer. When I execute the line, I (obviously) get a bad access error, as seen below:

second

What could be the reason? I’ve used C before, and I’ve pretty got a grasp of it, but I’ve never seen a pointer value changing suddenly to null before while stepping in a single-threaded simple C program. I am using Apple LLVM Compiler 4.1 to compile and lldb to debug, which are the defaults with XCode 4.5.

Update: The same behavior observed with compiling with gcc and debugging with gdb. Almost hundred percent an arror at my side, but I have no idea what is wrong with the code..

Update #2: I’ve noticed something even stranger on gcc/gdb now. The just before executing the line data = b->data[i * size + j];, I can access everything from the debugger with no issues. Right after executing that line, I can’t access b->data entirely, including the values that I’ve accessed right before stepping:

third

After the $4 = ... line which executed successfully in debugger, I’ve stepped over the line. Then I’ve got various addressing errors as see above. I really have no idea what’s going on…

Update #3: I’ve noticed something very weird. Here, first look at the fix that I’ve implemented. This one started to work with no problem when I got rid of the variable named data completely:

fourth

Now, look closely at the screenshot that I’ve uploaded with the Update #2: Right after I assign a value to the local variable named data, also b->data gets its address changed. It looks like a side effect of the assignment. But I have no idea what is the reason behind it.

  • 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-16T22:24:52+00:00Added an answer on June 16, 2026 at 10:24 pm

    Obviously something / somebody changes the property “data” of the board structure.

    Why ? I can only see 3 reasons :

    • Your application is multi-threaded and an other thread updates the data pointer to NULL (which is not your case, you are using only one thread, sorry didn’t notice).

    • The board structure is allocated from the stack but the content is not any more valid… For example : The pointer of a local variable is returned, but the variable (the struct) is destroyed (because this is a local variable) when returning the pointer…

    • The board structure is allocated from the heap then freed, and finally this freed pointer is still used : the heap memory is corrupted by something else…

    My guess : The second point which is a frequent mistake

    So a simple question : The board struct is stored in HEAP or STACK memory ?

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

Sidebar

Related Questions

I have some debugging code that if executed while running with GBD attached should
I have this code in my program: (I included the cout statements for debugging
I have been staring at this code for few hours, tried walkthrough,debugging with autos
Im trying to get debugging working without an app.config. I have the following code:
There I was happily debugging my code, when I did a restart, and even
Is there any substantial optimization when omitting the frame pointer? If I have understood
I have been playing around with some debugging and wrote some C code that
I am debugging a defect and have narrowed it down to the vtable pointer
I am debugging a third-party DLL for which I don't have the source code.
i get this dialog while debugging code in visual studio 2008. (your step-into 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.