why it does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bright116@gmail.com

    #1

    why it does not work

    It's strange! I wrote such fragment:

    char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";
    printf("message body: %s\n", messageBody);
    strcat(messageB ody, "Signal= ");
    printf("message body: %s\n", messageBody);
    strcat(messageB ody, CCDTMFMap[digit]);
    printf("message body: %s\n", messageBody);
    strcat(messageB ody, CRLF);
    printf("message body: %s\n", messageBody);
    strcat(messageB ody, "Duration= 160");
    printf("message body: %s\n", messageBody);
    strcat(messageB ody, CRLF);
    printf("message body: %s\n", messageBody);

    and it works, but if I withdraw all the printf, it fails to work, can
    anybody tell me why? Thanks very much.

  • Zara

    #2
    Re: why it does not work

    bright116@gmail .com wrote:[color=blue]
    > It's strange! I wrote such fragment:
    >
    > char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";
    > printf("message body: %s\n", messageBody);
    > strcat(messageB ody, "Signal= ");
    > printf("message body: %s\n", messageBody);
    > strcat(messageB ody, CCDTMFMap[digit]);
    > printf("message body: %s\n", messageBody);
    > strcat(messageB ody, CRLF);
    > printf("message body: %s\n", messageBody);
    > strcat(messageB ody, "Duration= 160");
    > printf("message body: %s\n", messageBody);
    > strcat(messageB ody, CRLF);
    > printf("message body: %s\n", messageBody);
    >
    > and it works, but if I withdraw all the printf, it fails to work, can
    > anybody tell me why? Thanks very much.
    >[/color]
    What did you expect it to do, that is not done?

    Comment

    • bright116@gmail.com

      #3
      Re: why it does not work

      I have to fill the message body for SIP, and when I filled it in
      without printf, it simply report errors. But I was wondering why printf
      here makes the differece.

      Comment

      • Emmanuel Delahaye

        #4
        Re: why it does not work

        bright116@gmail .com wrote on 06/09/05 :[color=blue]
        > It's strange! I wrote such fragment:[/color]
        <...>[color=blue]
        > and it works[/color]

        No. Doesn't compile at all.

        main.c:4: error: `MAX_DTMF_MESSA GE_SIZE' undeclared here (not in a
        function)
        main.c:5: error: syntax error before string constant
        main.c:5: warning: type defaults to `int' in declaration of `printf'
        main.c:5: warning: conflicting types for built-in function 'printf'
        main.c:5: warning: data definition has no type or storage class

        main.c:6: error: syntax error before string constant
        main.c:6: warning: type defaults to `int' in declaration of `strcat'
        main.c:6: warning: conflicting types for built-in function 'strcat'
        main.c:6: warning: data definition has no type or storage class
        main.c:7: error: syntax error before string constant
        main.c:7: warning: type defaults to `int' in declaration of `printf'
        main.c:7: warning: data definition has no type or storage class
        main.c:8: error: syntax error before '[' token
        main.c:8: warning: type defaults to `int' in declaration of `strcat'
        main.c:8: warning: data definition has no type or storage class
        main.c:9: error: syntax error before string constant
        main.c:9: warning: type defaults to `int' in declaration of `printf'
        main.c:9: warning: data definition has no type or storage class
        main.c:10: warning: type defaults to `int' in declaration of `strcat'
        main.c:10: warning: parameter names (without types) in function
        declaration
        main.c:10: warning: data definition has no type or storage class
        main.c:11: error: syntax error before string constant

        main.c:11: warning: type defaults to `int' in declaration of `printf'
        main.c:11: warning: data definition has no type or storage class
        main.c:12: error: syntax error before string constant

        main.c:12: warning: type defaults to `int' in declaration of `strcat'
        main.c:12: warning: data definition has no type or storage class
        main.c:13: error: syntax error before string constant

        main.c:13: warning: type defaults to `int' in declaration of `printf'
        main.c:13: warning: data definition has no type or storage class
        main.c:14: warning: type defaults to `int' in declaration of `strcat'

        main.c:14: warning: parameter names (without types) in function
        declaration
        main.c:14: warning: data definition has no type or storage class
        main.c:15: error: syntax error before string constant
        main.c:15: warning: type defaults to `int' in declaration of `printf'
        main.c:15: warning: data definition has no type or storage class
        main.c:4: error: storage size of `messageBody' isn't known

        Please provide a compiling source for a deeper analysis. We are not
        psychic.

        --
        Emmanuel
        The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
        The C-library: http://www.dinkumware.com/refxc.html

        ..sig under repair


        Comment

        • Kenneth Brody

          #5
          Re: why it does not work

          "bright116@gmai l.com" wrote:[color=blue]
          >
          > It's strange! I wrote such fragment:
          >
          > char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";
          > printf("message body: %s\n", messageBody);
          > strcat(messageB ody, "Signal= ");
          > printf("message body: %s\n", messageBody);
          > strcat(messageB ody, CCDTMFMap[digit]);
          > printf("message body: %s\n", messageBody);
          > strcat(messageB ody, CRLF);
          > printf("message body: %s\n", messageBody);
          > strcat(messageB ody, "Duration= 160");
          > printf("message body: %s\n", messageBody);
          > strcat(messageB ody, CRLF);
          > printf("message body: %s\n", messageBody);
          >
          > and it works, but if I withdraw all the printf, it fails to work, can
          > anybody tell me why? Thanks very much.[/color]

          Define "works" and "fails to work".

          What is MAX_DTMF_MESSAG E_SIZE?

          When it "works", how long is the resulting messageBody string, including
          the terminating '\0'?

          --
          +-------------------------+--------------------+-----------------------------+
          | Kenneth J. Brody | www.hvcomputer.com | |
          | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
          +-------------------------+--------------------+-----------------------------+
          Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

          Comment

          • Keith Thompson

            #6
            Re: why it does not work

            "bright116@gmai l.com" <bright116@gmai l.com> writes:[color=blue]
            > I have to fill the message body for SIP, and when I filled it in
            > without printf, it simply report errors. But I was wondering why printf
            > here makes the differece.[/color]

            Don't assume your readers can easily see to to article to which you're
            replying. You need to provide some context so each followup can
            be read on its own.

            If you want to post a followup via groups.google.c om, don't use
            the broken "Reply" link at the bottom of the article. Click on
            "show options" at the top of the article, then click on the
            "Reply" at the bottom of the article headers.

            We don't know what "SIP" is (and I suspect that knowing what SIP
            stands for wouldn't help).

            You say "it simply report errors", but you don't tell us what "it" is
            (the compiler? your program?), and you don't tell us what errors it
            reports.

            We can't try your code ourselves because we don't have the definitions
            of MAX_DTMF_MESSAG E_SIZE, CCDTMFMap, digit, or CRLF. We have no idea
            what you mean when you say it "fails to work".

            If you provide a small, complete, compilable program that illustrates
            your problem, along with an explicit description of what it actually
            does and what it's supposed to do, we might be able to help. For a
            program to be complete, it can't depend on any headers that aren't
            part of standard C.

            I might guess that you're overflowing your buffer, but of course I
            can't be sure.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • bright116@gmail.com

              #7
              Re: why it does not work

              Sorry for the incomplete codes. Actually it is inside a big project so
              I cannot provide a compilable one. Now I write the relatively complete
              code as below.


              #define CRLF "\r\n" /* Carriage Return and Line Feed */
              #define MAX_DTMF_MESSAG E_SIZE 27

              typedef enum CCDTMF
              {
              CCDTMF_0, /* DTMF 0 */
              CCDTMF_1, /* DTMF 1 */
              CCDTMF_2, /* DTMF 2 */
              CCDTMF_3, /* DTMF 3 */
              CCDTMF_4, /* DTMF 4 */
              CCDTMF_5, /* DTMF 5 */
              CCDTMF_6, /* DTMF 6 */
              CCDTMF_7, /* DTMF 7 */
              CCDTMF_8, /* DTMF 8 */
              CCDTMF_9, /* DTMF 9 */
              CCDTMF_S, /* DTMF * */
              CCDTMF_H, /* DTMF # */
              CCDTMF_MAX /* Number of DTMF digits */
              } CCDTMF;

              const char *CCDTMFMap[] =
              {
              "0", // DTMF 0
              "1", // DTMF 1
              "2", // DTMF 2
              "3", // DTMF 3
              "4", // DTMF 4
              "5", // DTMF 5
              "6", // DTMF 6
              "7", // DTMF 7
              "8", // DTMF 8
              "9", // DTMF 9
              "*", // DTMF *
              "#" // DTMF #
              };

              typedef struct CCCONTENT
              {
              char *type; /* Content type: e.g. application, text */
              char *subtype; /* Content sub-type: e.g. multipart, plain
              */
              UINT32 length; /* Length of the content in octets */
              UINT8 *data; /* Pointer to the content */
              }CCCONTENT;

              CCSTATUS callSendDTMF(CC REF cid, CCDTMF digit)
              {
              CCSTATUS status;
              UINT32 stackHandle;
              MX_NS CSipExtraHeader s *extraHeaders;
              MX_NS CSipContentInfo *contentInfo;
              CCCONTENT *content;

              char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";

              printf("message body: %s\n", messageBody);
              strcat(messageB ody, "Signal= ");
              printf("message body: %s\n", messageBody);
              strcat(messageB ody, CCDTMFMap[digit]);
              printf("message body: %s\n", messageBody);
              strcat(messageB ody, CRLF);
              printf("message body: %s\n", messageBody);
              strcat(messageB ody, "Duration= 160");
              printf("message body: %s\n", messageBody);
              strcat(messageB ody, CRLF);
              printf("message body: %s\n", messageBody);

              callgcb.critica lSection->Enter();

              content->type = "applicatio n";
              content->subtype = "dtmf-relay";
              content->data = (UINT8*)message Body;
              content->length = strlen((char *)content->data);

              TRACE2(("After filling the message body: %s", (char
              *)content->data));

              status = callSendBasicRe quest(cid, CCMETHOD_INFO, content, NULL);

              callgcb.critica lSection->Exit();
              return status;
              }


              //The message body should be 26 bytes not including the '\0'.

              Comment

              • bright116@gmail.com

                #8
                Re: why it does not work

                By SIP I mean Session Initiation Protocol, but I don't think it would
                be much of help.

                Comment

                • Flash Gordon

                  #9
                  Re: why it does not work

                  bright116@gmail .com wrote:[color=blue]
                  > Sorry for the incomplete codes. Actually it is inside a big project so
                  > I cannot provide a compilable one. Now I write the relatively complete
                  > code as below.[/color]

                  You can either cut out bits of code from your project and reduce it down
                  to a small compilable example, or construct a test harness and add in
                  the code until you reach a point that shows the problem.

                  Basically, unless you have found the problem (in which case you would
                  not be asking) or you post a complete example that exhibits the problem,
                  then the actual error could be *anywhere* in the code you have not
                  posted. For example, a buffer overflow in any piece of code could cause
                  a failure somewhere completely unrelated.
                  [color=blue]
                  > #define CRLF "\r\n" /* Carriage Return and Line Feed */
                  > #define MAX_DTMF_MESSAG E_SIZE 27
                  >
                  > typedef enum CCDTMF
                  > {
                  > CCDTMF_0, /* DTMF 0 */
                  > CCDTMF_1, /* DTMF 1 */
                  > CCDTMF_2, /* DTMF 2 */
                  > CCDTMF_3, /* DTMF 3 */
                  > CCDTMF_4, /* DTMF 4 */
                  > CCDTMF_5, /* DTMF 5 */
                  > CCDTMF_6, /* DTMF 6 */
                  > CCDTMF_7, /* DTMF 7 */
                  > CCDTMF_8, /* DTMF 8 */
                  > CCDTMF_9, /* DTMF 9 */
                  > CCDTMF_S, /* DTMF * */
                  > CCDTMF_H, /* DTMF # */
                  > CCDTMF_MAX /* Number of DTMF digits */
                  > } CCDTMF;
                  >
                  > const char *CCDTMFMap[] =
                  > {
                  > "0", // DTMF 0
                  > "1", // DTMF 1
                  > "2", // DTMF 2
                  > "3", // DTMF 3
                  > "4", // DTMF 4
                  > "5", // DTMF 5
                  > "6", // DTMF 6
                  > "7", // DTMF 7
                  > "8", // DTMF 8
                  > "9", // DTMF 9
                  > "*", // DTMF *
                  > "#" // DTMF #
                  > };
                  >
                  > typedef struct CCCONTENT
                  > {
                  > char *type; /* Content type: e.g. application, text */
                  > char *subtype; /* Content sub-type: e.g. multipart, plain
                  > */
                  > UINT32 length; /* Length of the content in octets */
                  > UINT8 *data; /* Pointer to the content */
                  > }CCCONTENT;
                  >
                  > CCSTATUS callSendDTMF(CC REF cid, CCDTMF digit)
                  > {
                  > CCSTATUS status;
                  > UINT32 stackHandle;
                  > MX_NS CSipExtraHeader s *extraHeaders;
                  > MX_NS CSipContentInfo *contentInfo;
                  > CCCONTENT *content;
                  >
                  > char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";
                  >
                  > printf("message body: %s\n", messageBody);
                  > strcat(messageB ody, "Signal= ");
                  > printf("message body: %s\n", messageBody);
                  > strcat(messageB ody, CCDTMFMap[digit]);[/color]

                  You've not checked digit in *this* function, so possibly it is out of
                  range (i.e. outside the range 0 to 12).
                  [color=blue]
                  > printf("message body: %s\n", messageBody);
                  > strcat(messageB ody, CRLF);
                  > printf("message body: %s\n", messageBody);
                  > strcat(messageB ody, "Duration= 160");
                  > printf("message body: %s\n", messageBody);
                  > strcat(messageB ody, CRLF);
                  > printf("message body: %s\n", messageBody);
                  >
                  > callgcb.critica lSection->Enter();
                  >
                  > content->type = "applicatio n";
                  > content->subtype = "dtmf-relay";
                  > content->data = (UINT8*)message Body;
                  > content->length = strlen((char *)content->data);
                  >
                  > TRACE2(("After filling the message body: %s", (char
                  > *)content->data));
                  >
                  > status = callSendBasicRe quest(cid, CCMETHOD_INFO, content, NULL);
                  >
                  > callgcb.critica lSection->Exit();
                  > return status;
                  > }
                  >
                  > //The message body should be 26 bytes not including the '\0'.[/color]

                  You seem to have counted that correctly and allocated enough space, so
                  the root problem is probably somewhere else causing digit to be an
                  illegal value.
                  --
                  Flash Gordon
                  Living in interesting times.
                  Although my email address says spam, it is real and I read it.

                  Comment

                  • Lawrence Kirby

                    #10
                    Re: why it does not work

                    On Tue, 06 Sep 2005 06:33:22 -0700, bright116@gmail .com wrote:
                    [color=blue]
                    > I have to fill the message body for SIP, and when I filled it in
                    > without printf, it simply report errors. But I was wondering why printf
                    > here makes the differece.[/color]

                    There's something in your program that invokes undefined behaviour,
                    probably writing somewhere it shouldn't e.g. an uninitialised pointer,
                    writing outside the bounds of an array e.g. with an unterminated string.
                    The presence of the printf calls may affect how data is laid out in memory
                    what data exists in memory at any point or the code used to access it.
                    When you get undefined behaviour anything can happen and that anything can
                    change in surprising ways.

                    Lawrence

                    Comment

                    • David Resnick

                      #11
                      Re: why it does not work


                      bright116@gmail .com wrote:[color=blue]
                      > Sorry for the incomplete codes. Actually it is inside a big project so
                      > I cannot provide a compilable one. Now I write the relatively complete
                      > code as below.
                      >
                      >
                      > #define CRLF "\r\n" /* Carriage Return and Line Feed */
                      > #define MAX_DTMF_MESSAG E_SIZE 27
                      >
                      > typedef enum CCDTMF
                      > {
                      > CCDTMF_0, /* DTMF 0 */
                      > CCDTMF_1, /* DTMF 1 */
                      > CCDTMF_2, /* DTMF 2 */
                      > CCDTMF_3, /* DTMF 3 */
                      > CCDTMF_4, /* DTMF 4 */
                      > CCDTMF_5, /* DTMF 5 */
                      > CCDTMF_6, /* DTMF 6 */
                      > CCDTMF_7, /* DTMF 7 */
                      > CCDTMF_8, /* DTMF 8 */
                      > CCDTMF_9, /* DTMF 9 */
                      > CCDTMF_S, /* DTMF * */
                      > CCDTMF_H, /* DTMF # */
                      > CCDTMF_MAX /* Number of DTMF digits */
                      > } CCDTMF;
                      >
                      > const char *CCDTMFMap[] =
                      > {
                      > "0", // DTMF 0
                      > "1", // DTMF 1
                      > "2", // DTMF 2
                      > "3", // DTMF 3
                      > "4", // DTMF 4
                      > "5", // DTMF 5
                      > "6", // DTMF 6
                      > "7", // DTMF 7
                      > "8", // DTMF 8
                      > "9", // DTMF 9
                      > "*", // DTMF *
                      > "#" // DTMF #
                      > };
                      >
                      > typedef struct CCCONTENT
                      > {
                      > char *type; /* Content type: e.g. application, text */
                      > char *subtype; /* Content sub-type: e.g. multipart, plain
                      > */
                      > UINT32 length; /* Length of the content in octets */
                      > UINT8 *data; /* Pointer to the content */
                      > }CCCONTENT;
                      >
                      > CCSTATUS callSendDTMF(CC REF cid, CCDTMF digit)
                      > {
                      > CCSTATUS status;
                      > UINT32 stackHandle;
                      > MX_NS CSipExtraHeader s *extraHeaders;
                      > MX_NS CSipContentInfo *contentInfo;
                      > CCCONTENT *content;
                      >
                      > char messageBody[MAX_DTMF_MESSAG E_SIZE] = "";
                      >
                      > printf("message body: %s\n", messageBody);
                      > strcat(messageB ody, "Signal= ");
                      > printf("message body: %s\n", messageBody);
                      > strcat(messageB ody, CCDTMFMap[digit]);
                      > printf("message body: %s\n", messageBody);
                      > strcat(messageB ody, CRLF);
                      > printf("message body: %s\n", messageBody);
                      > strcat(messageB ody, "Duration= 160");
                      > printf("message body: %s\n", messageBody);
                      > strcat(messageB ody, CRLF);
                      > printf("message body: %s\n", messageBody);
                      >
                      > callgcb.critica lSection->Enter();
                      >
                      > content->type = "applicatio n";
                      > content->subtype = "dtmf-relay";
                      > content->data = (UINT8*)message Body;
                      > content->length = strlen((char *)content->data);
                      >[/color]

                      content is not created prior to use, it is an uninitialized
                      pointer with no memory allocated.

                      You should either malloc it, or have it be an automatic struct
                      and use the dot operator. Since it doesn't need to exist
                      outside of the function, I'd choose the second option.

                      -David

                      Comment

                      Working...