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

  • SEARCH
  • Home
  • 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 6737453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:15:02+00:00 2026-05-26T11:15:02+00:00

I implemented a tree in C, and I now want to define a wrapper

  • 0

I implemented a tree in C, and I now want to define a wrapper tree set. I have an iterator for my tree in tree.h:

typedef struct tree_iter_t {
    void *current;
    tree_t *tree;
    unsigned char info : 2;
} tree_iter_t;

and a function to get an iterator in tree.c:

tree_iter_t titerator(tree_t *t) {
    tree_iter_t it;
    it.current = t->min;
    if (t->min) it.info = 0;
    else it.info = 3;
    it.tree = t;
    return it;
}

I can compile this with -Wall -O2 with no warnings. For my tree set, I defined my tree set iterator in tset.h as follows:

typedef struct tset_t tset_t;

typedef struct tset_iter_t {
    tree_iter_t iter;
} tset_iter_t;

and a function to obtain it in tset.c.

struct tset_t {
    tree_t *tree;
};

tset_iter_t tsiterator(tset_t *ts) {
    tset_iter_t it;
    it.iter = titerator(ts->tree);
    return it;
}

When I compile with gcc -Wall -c -combine tset.c tree.c, I have no problems, but when I add -O2, I get a warning on the return statement: warning: ‘it.iter.tree’ is used uninitialized in this function. Why does GCC have a problem with this? Am I missing something obvious? It looks initialized to me. I ran gcc -S -O2 tset.c to try to get a sense of what was going on, GCC gave no warning and produced this:

tsiterator:
pushl   %ebp
movl    %esp, %ebp
pushl   %ebx
subl    $36, %esp
movl    12(%ebp), %edx
movl    8(%ebp), %ebx
leal    -20(%ebp), %eax
movl    (%edx), %edx
movl    %eax, (%esp)
movl    %edx, 4(%esp)
call    titerator
movzbl  -12(%ebp), %edx
movzbl  8(%ebx), %eax
andl    $3, %edx
andl    $-4, %eax
orl     %edx, %eax
subl    $4, %esp
movb    %al, 8(%ebx)
movl    -16(%ebp), %eax
movl    %eax, 4(%ebx)
movl    -20(%ebp), %eax
movl    %eax, (%ebx)
movl    %ebx, %eax
movl    -4(%ebp), %ebx
leave
ret $4

I know optimization can generate some strange-looking code, but what the heck is going on here!? All of my other (optimized) wrapper functions are 10-ish lines of assembly (just the usual function call overhead for calling tree’s functions). gcc -O2 -S -combine tset.c tree.c gave me the warning, inlined titerator, and produced this:

tsiterator:
pushl   %ebp
movl    %esp, %ebp
movl    12(%ebp), %edx
pushl   %ebx
movl    8(%ebp), %eax
movl    (%edx), %ecx
movl    4(%ecx), %edx
movl    %ecx, 4(%eax)
cmpl    $1, %edx
movl    %edx, (%eax)
movzbl  8(%eax), %edx
sbbl    %ebx, %ebx
andl    $3, %ebx
andl    $-4, %edx
orl     %ebx, %edx
movb    %dl, 8(%eax)
popl    %ebx
popl    %ebp
ret $4

When I changed the implementation to:

tset_iter_t tsiterator(tset_t *ts) {
    tset_iter_t it;
    tree_iter_t i = titerator(ts->tree);
    it.iter = i;
    return it;
}

it had no problems. What is GCC optimizing (or analyzing) in the first case, and why is it giving me a warning?

Thanks.

  • 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-26T11:15:03+00:00Added an answer on May 26, 2026 at 11:15 am

    I think the warning is a bug. Which gcc are you using? Doesn’t happen for me when I compile (admittedly a single file) with gcc 4.0 and 4.2.

    Here is an annotated version of the optimised assembler. I can’t see anything unassigned here, which is why I think the warning is not right. I’ve guessed about the tree structure.

    tsiterator:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %edx  ; edx has pointer to ts
        pushl   %ebx
        movl    8(%ebp), %eax   ; eax has pointer to retval
        movl    (%edx), %ecx    ; ecx has ts->tree (?)
        movl    4(%ecx), %edx   ; edx has ts->tree->min (?)
        movl    %ecx, 4(%eax)   ; store ts->tree into retval->iter->tree
        cmpl    $1, %edx    
        movl    %edx, (%eax)    ; store ts->tree->min into retval->iter->current
        ;; This and the cmpl instruction above is all
        ;; handling the bitmasking for retval->iter->info.
        ;; Probably would be more efficient to not use a
        ;; bit mask here, as the compiler could remove the
        ;; second "and" and "or" instructions.
        movzbl  8(%eax), %edx   ; get current value of retval->iter->info
        sbbl    %ebx, %ebx  ; ebx = 0 if unsigned edx < 1, else -1
        andl    $3, %ebx    ; mask off new value            
        andl    $-4, %edx   ; mask off old value   
        orl     %ebx, %edx  ; combine old | new    
        movb    %dl, 8(%eax)    ; store combined into retval->iter->info
        popl    %ebx
        popl    %ebp
        ret $4
    

    EDIT: Note that the compiler carefully preserves the upper 6 bits of random uninitialized junk in tree_iter_t.info.

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

Sidebar

Related Questions

I have an own Tree object implemented in PHP. Say we have a tree
I have successfully implemented this method of using the Win32 API to set the
I want my flot graph to update it's data on set intervals. I have
High Guys, I have to define a polymorphic datatype for a tree that can
Hey guys, I have created a tree which is not a binary tree. Now,
I'm implementing a game. I have a state tree and a set<> based priority
I have a user interface with a tree view on the left, and a
Suppose that I have a tree to traverse using a Depth First Search, and
Let's suppose we have a DOM tree: <div id=rootNode> <span data-bind=... > </span> <div>
The question I want to ask is thus: Is casting down the inheritance tree

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.