At this point StrNam2 is set to some indeterminate value. What do
you think passing it to strlen is going to do? The length of WHAT
string?
> sprintf("%s%s", strcat
> ("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FSID%3D",
> "A3D6F20D-7091"));
>
This isn't how sprintf works at all. The first arg to sprintf has
to be a char* that points to some allcoated memory that the printf()
results will be written too.

The second arg is the format string. And you strcat doesn't work
the way you think it should either.

At this point, you probably should stay away from C's hiddeous attempt
to implement strings as character arrays and just use C++'s string type.

What are you trying to do here? 'StrNam2' has no value given to it.
You're trying to find its length by calling 'strlen'. That's the
second warning you get. Anyway, that's not the right way to manage
your memory. If you want to allocate some memory and store the pointer
to it in 'StrNam2', you need to give it some meaningful size. What is
the purpose of 'StrNam2'?
> sprintf("%s%s", strcat
> ("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FSID%3D",
> "A3D6F20D-7091"));

'sprintf' is supposed to have the string where you want the result as
the very first argument. The format is the _second_ argument. RTFM.

'strcat' is used incorrectly. If you just want to have two literals
live on two different lines of code but represent the same string, you
can simply leave whitespace between them:

You've just defined two pointers. Since you did not initialize them,
their values are indetermindate, unknown. I.e. these pointers don't
point anywhere.
>
> StrNam2 = (char *) malloc (strlen (StrNam2) + 1);

Here you pass an ininitalizaed pointer ('StrNam2') to a function
('strlen()'), which expects a valid pointer to a zero-terminated
array of characters. The resulting behavior is undefined. If you
want to compute the length of a string, first you need a string.

You also need to check 'malloc()'s return value to see if it
failed (returns NULL in that case).

You never subsequently use 'StrNam2' in your program anyway,
so I'm not sure what your trying to do. Perhaps if you
explain that, I could offer more specific advice.
> sprintf("%s%s", strcat
> ("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FSID%3D",
> "A3D6F20D-7091"));

Here's another case of undefined behavior. 'strcat()' will attempt
to modify the string pointed to by its first parameter. But you give
a pointer to a string literal, which the language prohibits modifying.

Another 'undefined' aspect of this is that you've given 'printf()'
two format specifiers ('%s'), but only one corresponding argument.

If you want to concatentate two strings, you need a place to store
the resulting string. (This could be the string being lengthened,
if sufficient storage has been allocated for it).
>
>
> return 0;
> }
>
> Compiling...
> Concatenate.cpp
> C:\Program Files\Microsoft Visual
> Studio\MyProjects\dash2hex\Concatenate.cpp(10) : warning C4101:
> 'StrNam1' : unreferenced local variable

This is harmless. It simply means you've created an object an
never used it. Use modern C++ and forget that low-level memory
management you've tried above.

Thanks to you all for you explanations. I'm going through this book
called "C Programming for the Absolute Beginner", which as you can see
is definitely me, but I have this person who trying to get me up to
speed with some exercises. I agree I should study more. If you have
any suggestion of a good C Programming book, please advise.

Ron Natalie wrote:
> Method Man wrote:
> =
> >
> > A couple more suggestions:
> >
> > 1. Don't cast the result of malloc, it can be unsafe.
>
> You have to cast the result of malloc. There is no implicit
> conversion from void* in C++.

Yep, MM is thinking of C. In C++, not only is the cast required but the
"C problem" of implicit declaration of undeclared functions doesn't
exist either.
> > 2. For every malloc(), make sure to have a corresponding free()
> > call. It's better to be safe and manage your own memory.
>
> True, but it would be better to avoid user-managed dynamic allocations
> here entirely. That would have avoided 90% of the problems.

I agree. Newbies to C++ should be starting out with containers. Once a
firm grasp of programming is established, then dynamic memory can be
tackled.

Alzane wrote:
> Thanks to you all for you explanations. I'm going through this book
> called "C Programming for the Absolute Beginner", which as you can see
> is definitely me, but I have this person who trying to get me up to
> speed with some exercises. I agree I should study more. If you have
> any suggestion of a good C Programming book, please advise.

Why are you posting to a C++ newsgroup, when you are attempting to
learn C? Many of the comments you received were inappropriate in the
context of a review of a C program (although many others were the same).

"Default User" <> wrote in message
news:...
> Alzane wrote:
>
> > Thanks to you all for you explanations. I'm going through this book
> > called "C Programming for the Absolute Beginner", which as you can see
> > is definitely me, but I have this person who trying to get me up to
> > speed with some exercises. I agree I should study more. If you have
> > any suggestion of a good C Programming book, please advise.
>
> Why are you posting to a C++ newsgroup, when you are attempting to
> learn C? Many of the comments you received were inappropriate in the
> context of a review of a C program (although many others were the same).
>
> You should be using comp.lang.c.
>

Yes. C and C++ are two different languages and it is a waste of people's
time to analyze code for one language when you are referring to another.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!