Solved

What's wrong with this???

Posted on 1998-03-26
21
462 Views
Last Modified: 2008-02-01
What's wrong with this???

VOID os_printf(char *ctl, ...)
  {
    va_list argList;

    va_start(argList, ctl + 1);
    myPrintf("os_printf - ");
    myPrintf(ctl, argList);
    va_end(argList);
    myPrintf("\n");
  }
0
Comment
Question by:tflai
  • 11
  • 10
21 Comments
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What are you trying to do with the "+1" in the in va_start()?  What are you trying to do period?
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
Basically just to port the os_printf() to my printf() functions.
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
I tried without +1 also...
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Is myprintf() overloaded?  Your first call has only one parameter.  I assume there is a myprintf() that takes a va_list as its second argument.  That should be fine, but I think you are initializing "arglst" incorrectly.  You should not be adding 1.  Try

VOID os_printf(char *ctl, ...)
      {
        va_list argList;

        va_start(argList, ctl);
        myPrintf("os_printf - ");
        myPrintf(ctl, argList);
        myPrintf("\n");
      }

note you shouldn't need the va_end, either.



0
 
LVL 4

Author Comment

by:tflai
Comment Utility
To make long story short, I've been trying to port a TCP/IP stack to our embedded system environment.  It uses os_printf() which the developer would have to replace it with os-dependent printf() command.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Opps, I hadn't seen you message that you tried without +1, when I posted last. What does the myprintf() code look like?
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
myprintf() basically takes in (char * ctl, ...) and goes through each % identifier and format the message with the corresponding argument.  It function just like printf().

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Wait a second.  It is not declared with a ... arguement is it?  It should take a va_list as a second argument.
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
Yeh, it's declared with ... as second argument.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
when you call myprintf() like

    myPrintf(ctl, argList);

you are not passing the parameters that were passed to os_printf().  You are passing (as the second parameter) a pointer to where those arguments are stored on the stack.  so myPrintf() is getting only two parameters (regardless of what was passed to os-printf()) and the second parameter is a pointer to a parameter in another function, not the regular parameter.  

If this is what you are doing, this can be made to work. You have to change myprintf() a little.  I'll wait for your response before I give details, in case II'm wrong about your approach.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
Well, that was a short wait.  Do you need help fixing it?
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
YES, I think you're on the right track.  So, how should I make it so that myPrintf() gets the exact same paramters passed in as os_printf()?  Thanks.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
You can't exacly.  You can give myprinf() ACCESS to os_printf()'s variable parameters, but you can't PASS it the parameters.  The difference is that myprintf() will not be a variable argument function.  You cannot call it in other cases with a variable number or parameters.  It will always take two parameters.  (If that is a problem, you can get around it by writing an interface procedure that does take a variable number of parameters and then calls myprintf() with only two parameters.)

What you need to do is make the second parameter to myprintf() a va_list.  (and then don't have a "..." parameter).  Then in myprintf() use va_arg() to get parameters using the passed va_list.  Note that this is unusual, but will work.  va-arg is usually used to get a procedures own arguments, but it can get another procedure's arguments as well.  Are you familiar with va_arg or do you need help with that?  if so, try to post your code.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Apparently, you are good to go.  So I'm going to go.  I'll check in a few hours if you need more.
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
Okay, I got it:
The hardware dependent printf() looked like this:
myPrintf(LPSTR format, ...)
{
va_list ap;
va_start(ap, format);
format(PutChar, format, ap);
va_end(ap);
}

So, my os_printf() should be:
VOID os_printf(char *ctl, ...)
{
va_list ArgList;
va_start(ArgList, ctl);
format(PutChar, ctl, ArgList);
va_end(ArgList);
}
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
That looks right.  Does it work?  Note, you shouldn't need the va_end() for this, at least not for what you've shown.
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
It still don't work...  This is weird!
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What does not work?  Compiler or run-time?  which procedure/what behavior?

Am I right in assuming that
(1) format() is existing code that you are trying to make use of,
(2) OS_printf() is code written to use format() that does work, and
(3) myprintf() is something you are writing modeling os_printf()?

If the code is too long to post, you could email it to me.  nietod@theshop.net.  If you do that, include a note pointing me to the right place.
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
1) format() is a chunk of code already provided by our kernel wrapper around AMX RTOS kernel.
2) os_printf() is used by FUSION TCP/IP stack that has to be defined for os-dependent output.
3) hwprintf() is another function provided by our kernel wrapper.  It will completely mimic printf()'s functionality.

Nietod, thanks for the help and the patience.  This is driving me crazy.  With the extremely tight deadline to start out with and that I'm solely responsible for making the IP network to work between PC side and the embedded side, it's really frustrating.  I've done a lot on the PC side already, like TDI-client kernel-mode drivers, IP handshake protocol driver.  But I've just been introduced to the embedded side recently...  sigh...
0
 
LVL 4

Author Comment

by:tflai
Comment Utility
Well, I did a quick fix by:
#define os_printf Dprintf
(But this technique doesn't allow me to distinguish between os_printf, os_warn, or os_panic,...)
The va_list thing is still bothering me, but I have more to solve with this tight deadline.  I guess I'll leave it as it.  But I still really appreciate what you, nietod,  has helped.  Thanks.  :-)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Well, when you get back to it.  Post your questions/progress here.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now