Recursive post-order traversal and sprintf in C












0















I am traversing binary tree using this function. I am pretty sure the proper saving of new values (skip to 'current output') to nodes keeps failing on sprintf(buffer, "var%i", counter): i wonder why.



static int counter = 1;

void postorder(tASTPointer* Root) {

if (Root == NULL)
return;
postorder(Root->LeftPointer);
postorder(Root->RightPointer);


if((!strcmp(Root->ID,"*")) || (!strcmp(Root->ID,"+"))) {

printf("DEFVAR var%in",counter);

if(!strcmp(Root->ID,"*")) // multiplication
printf("MUL var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);
else if(!strcmp(Root->ID,"+")) // addition
printf("ADD var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);


char buffer[25];
for (int i = 0; i < 25; i++)
buffer[i] = '';

sprintf(buffer, "var%i", counter);
Root->content->name = buffer;

//for (int i = 0; i < 25; i++)
// buffer[i] = '';

counter++;

printf("Root contains: %sn", Root->content->name);
printf("LeftPointer contains: %sn", Root->LeftPointer->content->name);
printf("RightPointer contains: %snn", Root->RightPointer->content->name);

}
}


More information



I am processing binary tree created by leaf nodes - numbers and operation nodes, in this case * and +. My goal is to change every operation_node->name to original id.



Original tree looks like:



            +
| |
* *
| | | |
1 2 3 4


What I am trying for:



           var3
| |
var1 var2
| | | |
1 2 3 4


Desired output (assembler-like):



DEFVAR var1
MUL var1 1 2 // 1*2, save to var1

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var1 var2 // var1 + var2, save to var3


Current output:



DEFVAR var1
MUL var1 1 2

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var2 var2 // something wrong with buffer?


Question



If anyone would care to explain why this keeps happening (and possibly provide some solution), I would be grateful.










share|improve this question























  • Undefined behavior for accessing an object after the end of its lifetime.

    – EOF
    Nov 23 '18 at 0:01
















0















I am traversing binary tree using this function. I am pretty sure the proper saving of new values (skip to 'current output') to nodes keeps failing on sprintf(buffer, "var%i", counter): i wonder why.



static int counter = 1;

void postorder(tASTPointer* Root) {

if (Root == NULL)
return;
postorder(Root->LeftPointer);
postorder(Root->RightPointer);


if((!strcmp(Root->ID,"*")) || (!strcmp(Root->ID,"+"))) {

printf("DEFVAR var%in",counter);

if(!strcmp(Root->ID,"*")) // multiplication
printf("MUL var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);
else if(!strcmp(Root->ID,"+")) // addition
printf("ADD var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);


char buffer[25];
for (int i = 0; i < 25; i++)
buffer[i] = '';

sprintf(buffer, "var%i", counter);
Root->content->name = buffer;

//for (int i = 0; i < 25; i++)
// buffer[i] = '';

counter++;

printf("Root contains: %sn", Root->content->name);
printf("LeftPointer contains: %sn", Root->LeftPointer->content->name);
printf("RightPointer contains: %snn", Root->RightPointer->content->name);

}
}


More information



I am processing binary tree created by leaf nodes - numbers and operation nodes, in this case * and +. My goal is to change every operation_node->name to original id.



Original tree looks like:



            +
| |
* *
| | | |
1 2 3 4


What I am trying for:



           var3
| |
var1 var2
| | | |
1 2 3 4


Desired output (assembler-like):



DEFVAR var1
MUL var1 1 2 // 1*2, save to var1

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var1 var2 // var1 + var2, save to var3


Current output:



DEFVAR var1
MUL var1 1 2

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var2 var2 // something wrong with buffer?


Question



If anyone would care to explain why this keeps happening (and possibly provide some solution), I would be grateful.










share|improve this question























  • Undefined behavior for accessing an object after the end of its lifetime.

    – EOF
    Nov 23 '18 at 0:01














0












0








0








I am traversing binary tree using this function. I am pretty sure the proper saving of new values (skip to 'current output') to nodes keeps failing on sprintf(buffer, "var%i", counter): i wonder why.



static int counter = 1;

void postorder(tASTPointer* Root) {

if (Root == NULL)
return;
postorder(Root->LeftPointer);
postorder(Root->RightPointer);


if((!strcmp(Root->ID,"*")) || (!strcmp(Root->ID,"+"))) {

printf("DEFVAR var%in",counter);

if(!strcmp(Root->ID,"*")) // multiplication
printf("MUL var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);
else if(!strcmp(Root->ID,"+")) // addition
printf("ADD var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);


char buffer[25];
for (int i = 0; i < 25; i++)
buffer[i] = '';

sprintf(buffer, "var%i", counter);
Root->content->name = buffer;

//for (int i = 0; i < 25; i++)
// buffer[i] = '';

counter++;

printf("Root contains: %sn", Root->content->name);
printf("LeftPointer contains: %sn", Root->LeftPointer->content->name);
printf("RightPointer contains: %snn", Root->RightPointer->content->name);

}
}


More information



I am processing binary tree created by leaf nodes - numbers and operation nodes, in this case * and +. My goal is to change every operation_node->name to original id.



Original tree looks like:



            +
| |
* *
| | | |
1 2 3 4


What I am trying for:



           var3
| |
var1 var2
| | | |
1 2 3 4


Desired output (assembler-like):



DEFVAR var1
MUL var1 1 2 // 1*2, save to var1

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var1 var2 // var1 + var2, save to var3


Current output:



DEFVAR var1
MUL var1 1 2

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var2 var2 // something wrong with buffer?


Question



If anyone would care to explain why this keeps happening (and possibly provide some solution), I would be grateful.










share|improve this question














I am traversing binary tree using this function. I am pretty sure the proper saving of new values (skip to 'current output') to nodes keeps failing on sprintf(buffer, "var%i", counter): i wonder why.



static int counter = 1;

void postorder(tASTPointer* Root) {

if (Root == NULL)
return;
postorder(Root->LeftPointer);
postorder(Root->RightPointer);


if((!strcmp(Root->ID,"*")) || (!strcmp(Root->ID,"+"))) {

printf("DEFVAR var%in",counter);

if(!strcmp(Root->ID,"*")) // multiplication
printf("MUL var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);
else if(!strcmp(Root->ID,"+")) // addition
printf("ADD var%i %s %sn", counter,
Root->LeftPointer->content->name,
Root->RightPointer->content->name);


char buffer[25];
for (int i = 0; i < 25; i++)
buffer[i] = '';

sprintf(buffer, "var%i", counter);
Root->content->name = buffer;

//for (int i = 0; i < 25; i++)
// buffer[i] = '';

counter++;

printf("Root contains: %sn", Root->content->name);
printf("LeftPointer contains: %sn", Root->LeftPointer->content->name);
printf("RightPointer contains: %snn", Root->RightPointer->content->name);

}
}


More information



I am processing binary tree created by leaf nodes - numbers and operation nodes, in this case * and +. My goal is to change every operation_node->name to original id.



Original tree looks like:



            +
| |
* *
| | | |
1 2 3 4


What I am trying for:



           var3
| |
var1 var2
| | | |
1 2 3 4


Desired output (assembler-like):



DEFVAR var1
MUL var1 1 2 // 1*2, save to var1

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var1 var2 // var1 + var2, save to var3


Current output:



DEFVAR var1
MUL var1 1 2

DEFVAR var2
MUL var2 3 4

DEFVAR var3
ADD var3 var2 var2 // something wrong with buffer?


Question



If anyone would care to explain why this keeps happening (and possibly provide some solution), I would be grateful.







c recursion printf postorder






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 23:48









Adam PlšekAdam Plšek

73




73













  • Undefined behavior for accessing an object after the end of its lifetime.

    – EOF
    Nov 23 '18 at 0:01



















  • Undefined behavior for accessing an object after the end of its lifetime.

    – EOF
    Nov 23 '18 at 0:01

















Undefined behavior for accessing an object after the end of its lifetime.

– EOF
Nov 23 '18 at 0:01





Undefined behavior for accessing an object after the end of its lifetime.

– EOF
Nov 23 '18 at 0:01












1 Answer
1






active

oldest

votes


















-1














This is guess that as the counter keeps incrementing as recursively function stack keeps increasing counter has to hold old value of previous recursive call. To verify just decrement counter on return from recursive call.






share|improve this answer
























  • I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

    – Adam Plšek
    Nov 24 '18 at 23:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439180%2frecursive-post-order-traversal-and-sprintf-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














This is guess that as the counter keeps incrementing as recursively function stack keeps increasing counter has to hold old value of previous recursive call. To verify just decrement counter on return from recursive call.






share|improve this answer
























  • I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

    – Adam Plšek
    Nov 24 '18 at 23:40
















-1














This is guess that as the counter keeps incrementing as recursively function stack keeps increasing counter has to hold old value of previous recursive call. To verify just decrement counter on return from recursive call.






share|improve this answer
























  • I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

    – Adam Plšek
    Nov 24 '18 at 23:40














-1












-1








-1







This is guess that as the counter keeps incrementing as recursively function stack keeps increasing counter has to hold old value of previous recursive call. To verify just decrement counter on return from recursive call.






share|improve this answer













This is guess that as the counter keeps incrementing as recursively function stack keeps increasing counter has to hold old value of previous recursive call. To verify just decrement counter on return from recursive call.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 14:13









anandanand

1375




1375













  • I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

    – Adam Plšek
    Nov 24 '18 at 23:40



















  • I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

    – Adam Plšek
    Nov 24 '18 at 23:40

















I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

– Adam Plšek
Nov 24 '18 at 23:40





I resolved this by implementing external queue which holds data I was originally saving through the buffer back to tree. When I need this data I just get() from queue as much items as needed and since it I am doing postorder(), it works.

– Adam Plšek
Nov 24 '18 at 23:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439180%2frecursive-post-order-traversal-and-sprintf-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'