472,371 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

unreferenced local variable

I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])

{
char *StrNam1;
char *StrNam2;

StrNam2 = (char *) malloc (strlen (StrNam2) + 1);
sprintf("%s%s", strcat
("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FS ID%3D",
"A3D6F20D-7091"));
return 0;
}

Compiling...
Concatenate.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(10) : warning C4101:
'StrNam1' : unreferenced local variable
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(13) : warning C4700: local
variable 'StrNam2' used without having been initialized
Linking...

Concatenate.exe - 0 error(s), 2 warning(s)
Jul 22 '05 #1
11 11571
Alzane wrote:
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
BOGUS include file.


int main(int argc, char* argv[])

{
char *StrNam1; You indeed don't use this anywhere, causing the first warning.
char *StrNam2;

StrNam2 = (char *) malloc (strlen (StrNam2) + 1);
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%3FS ID%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.

#include <string>
int main() {
std::string StrNam2 = "..%2F..dkafjkjdflasdkjf";
StrNam2 += "A3D6F20D-7091";

}
Jul 22 '05 #2
Alzane wrote:
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])

{
char *StrNam1;
You declared the pointer 'StrNam1' but never use it in the program.
That's why the compiler warns you.
char *StrNam2;

StrNam2 = (char *) malloc (strlen (StrNam2) + 1);
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%3FS ID%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:

"..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FSI D%3D"
"A3D6F20D-7091"

No commas, no 'strcat' calls. 'strcat' requires the first argument to
be the _resulting_ buffer. If you pass a literal as the first argument,
you're asking for trouble. RTFM.


return 0;
}

Compiling...
Concatenate.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(10) : warning C4101:
'StrNam1' : unreferenced local variable
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(13) : warning C4700: local
variable 'StrNam2' used without having been initialized
Linking...

Concatenate.exe - 0 error(s), 2 warning(s)


V
Jul 22 '05 #3

"Alzane" <az******@juno.com> wrote in message
news:37**************************@posting.google.c om...
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
This is a nonstandard header. Please omit such from
code posted here. You don't need this anyway.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])

{
char *StrNam1;
char *StrNam2;
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%3FS ID%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.
-Mike
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(13) : warning C4700: local
variable 'StrNam2' used without having been initialized


This is the real problem.
Since none of what you wrote above enough sense for me to
try to guess what you're trying to do, I can only offer this
simple C++ program which outputs the result of concatenating
two strings.
#include <iostream>
#include <string>

int main()
{
std::string StrNam1("Hello ");
std::string StrNam2("world");
std::cout << StrNam1 + StrNam2 << '\n';
return 0;
}

-Mike
Jul 22 '05 #4
Alzane wrote:
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
The above header is non-standard.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
While technically C++ headers, they are provided mostly for
compatibility with C. You should probably not be using them.

int main(int argc, char* argv[])
As you don't use the args in main(), you should leave them out.
{
char *StrNam1;
char *StrNam2;
It's a poor idea for a beginner to use pointers in learning programs.
StrNam2 = (char *) malloc (strlen (StrNam2) + 1);
StrNam2 was not initialized. What did you think the result of getting
the string length of it would be? What it is in Undefined Behavior, a
Very Bad Thing.

Also, malloc() is generally not used in C++ programming.
sprintf("%s%s", strcat
("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FS ID%3D",
"A3D6F20D-7091"));
You don't know how to use sprintf() at all. The arg count is wrong.
Also, your use of strcat() modifies a string literal. Undefined
Behavior again.

return 0;
You got this right.
}

Compiling...
Concatenate.cpp
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(10) : warning C4101:
'StrNam1' : unreferenced local variable
You didn't use StrNam1.
C:\Program Files\Microsoft Visual
Studio\MyProjects\dash2hex\Concatenate.cpp(13) : warning C4700: local
variable 'StrNam2' used without having been initialized


This is the far more dangerous problem.
What book are you using? This is a mishmashed, dangerous piece of
essentially C code.

Get a good book, one using modern C++, then read it. Otherwise, you're
wasting your time and ours.

Brian

Jul 22 '05 #5

"Alzane" <az******@juno.com> wrote in message
news:37**************************@posting.google.c om...
I'm new to C++ programming. I have an exercise that I have written
code for but getting warnings. Can I get some help?
#include "stdafx.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])

{
char *StrNam1;
char *StrNam2;

StrNam2 = (char *) malloc (strlen (StrNam2) + 1);
sprintf("%s%s", strcat
("..%2F..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FS ID%3D",
"A3D6F20D-7091"));
return 0;
}

A couple more suggestions:

1. Don't cast the result of malloc, it can be unsafe.
2. For every malloc(), make sure to have a corresponding free() call. It's
better to be safe and manage your own memory.

Note: The above looks like C code. A good book on C or C++ would help you
with your exercise much more.
Jul 22 '05 #6
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++.

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.
Jul 22 '05 #7
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.
Jul 22 '05 #8
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.

Brian
Jul 22 '05 #9
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.


Brian
Jul 22 '05 #10

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...
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.


Thanks, I wasn't aware of that. Personally, I've never had to use malloc()
in a C++ program yet.
Jul 22 '05 #11

"Default User" <fi********@boeing.com.invalid> wrote in message
news:I8********@news.boeing.com...
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.
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
2
by: Halfdan Holger Knudsen | last post by:
goodday all - here's a snippet of code that just doesn't seem to work...at all! But I haven't got the faintest idea why I keep getting an unref. local var. error. Take a look - I know I can get...
1
by: Tzu-Chien Chiu | last post by:
I know many linkers can remove unreferenced functions while linking the object files, but I need a tool/script to list all unreferenced symbols. -- Tzu-Chien Chiu XGI Technology, Inc - Extreme...
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
22
by: John Fisher | last post by:
void f(int p) { } Many (most?) compilers will report that p is unreferenced here. This may not be a problem as f may have to match some common prototype. Typically pointers to functions are...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
9
by: dotnetismylife | last post by:
Hi all, I have a question, in the following code: using System; using System.Diagnostics; namespace ConsoleApplication1 { class TestClass {
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.