why it does not work

B

bright116

It's strange! I wrote such fragment:

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";
printf("message body: %s\n", messageBody);
strcat(messageBody, "Signal= ");
printf("message body: %s\n", messageBody);
strcat(messageBody, CCDTMFMap[digit]);
printf("message body: %s\n", messageBody);
strcat(messageBody, CRLF);
printf("message body: %s\n", messageBody);
strcat(messageBody, "Duration= 160");
printf("message body: %s\n", messageBody);
strcat(messageBody, 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.
 
Z

Zara

It's strange! I wrote such fragment:

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";
printf("message body: %s\n", messageBody);
strcat(messageBody, "Signal= ");
printf("message body: %s\n", messageBody);
strcat(messageBody, CCDTMFMap[digit]);
printf("message body: %s\n", messageBody);
strcat(messageBody, CRLF);
printf("message body: %s\n", messageBody);
strcat(messageBody, "Duration= 160");
printf("message body: %s\n", messageBody);
strcat(messageBody, 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.
What did you expect it to do, that is not done?
 
B

bright116

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.
 
K

Kenneth Brody

It's strange! I wrote such fragment:

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";
printf("message body: %s\n", messageBody);
strcat(messageBody, "Signal= ");
printf("message body: %s\n", messageBody);
strcat(messageBody, CCDTMFMap[digit]);
printf("message body: %s\n", messageBody);
strcat(messageBody, CRLF);
printf("message body: %s\n", messageBody);
strcat(messageBody, "Duration= 160");
printf("message body: %s\n", messageBody);
strcat(messageBody, 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.

Define "works" and "fails to work".

What is MAX_DTMF_MESSAGE_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:[email protected]>
 
E

Emmanuel Delahaye

It's strange! I wrote such fragment:
and it works

No. Doesn't compile at all.

main.c:4: error: `MAX_DTMF_MESSAGE_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
 
K

Keith Thompson

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.

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.com, 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_MESSAGE_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.
 
B

bright116

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_MESSAGE_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(CCREF cid, CCDTMF digit)
{
CCSTATUS status;
UINT32 stackHandle;
MX_NS CSipExtraHeaders *extraHeaders;
MX_NS CSipContentInfo *contentInfo;
CCCONTENT *content;

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";

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

callgcb.criticalSection->Enter();

content->type = "application";
content->subtype = "dtmf-relay";
content->data = (UINT8*)messageBody;
content->length = strlen((char *)content->data);

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

status = callSendBasicRequest(cid, CCMETHOD_INFO, content, NULL);

callgcb.criticalSection->Exit();
return status;
}


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

bright116

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

Flash Gordon

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.

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.
#define CRLF "\r\n" /* Carriage Return and Line Feed */
#define MAX_DTMF_MESSAGE_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(CCREF cid, CCDTMF digit)
{
CCSTATUS status;
UINT32 stackHandle;
MX_NS CSipExtraHeaders *extraHeaders;
MX_NS CSipContentInfo *contentInfo;
CCCONTENT *content;

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";

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

You've not checked digit in *this* function, so possibly it is out of
range (i.e. outside the range 0 to 12).
printf("message body: %s\n", messageBody);
strcat(messageBody, CRLF);
printf("message body: %s\n", messageBody);
strcat(messageBody, "Duration= 160");
printf("message body: %s\n", messageBody);
strcat(messageBody, CRLF);
printf("message body: %s\n", messageBody);

callgcb.criticalSection->Enter();

content->type = "application";
content->subtype = "dtmf-relay";
content->data = (UINT8*)messageBody;
content->length = strlen((char *)content->data);

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

status = callSendBasicRequest(cid, CCMETHOD_INFO, content, NULL);

callgcb.criticalSection->Exit();
return status;
}

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

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.
 
D

David Resnick

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_MESSAGE_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(CCREF cid, CCDTMF digit)
{
CCSTATUS status;
UINT32 stackHandle;
MX_NS CSipExtraHeaders *extraHeaders;
MX_NS CSipContentInfo *contentInfo;
CCCONTENT *content;

char messageBody[MAX_DTMF_MESSAGE_SIZE] = "";

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

callgcb.criticalSection->Enter();

content->type = "application";
content->subtype = "dtmf-relay";
content->data = (UINT8*)messageBody;
content->length = strlen((char *)content->data);

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
 
L

Lawrence Kirby

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top