且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

未引用的局部变量

更新时间:2023-02-22 18:08:34

Alzane写道:
我是C ++编程的新手。我有一个练习,我已经编写了代码但收到警告。我可以得到一些帮助吗?

#include" stdafx.h"


BOGUS包含文件。

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

{
char * StrNam1;
你确实不会在任何地方使用它,导致第一个警告。

char * StrNam2;

StrNam2 =(char *)malloc(strlen(StrNam2) )+ 1);


此时,StrNam2被设置为某个不确定的值。

你认为将它传递给strlen会怎样? WHAT

字符串的长度?

sprintf("%s%s",strcat
(" ..%2F ..%2FCommon%2FASP %2Fquot_choose_lob.asp%3FS ID%3D",
" A3D6F20D-7091"));
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"));



这不是sprintf的工作方式。 sprintf的第一个参数是

是一个char *,指向一些所有涂层的内存,printf()

结果也会被写入。


第二个arg是格式字符串。并且你的strcat不会像你认为的那样工作




此时,你可能应该远离C'' hiddeous尝试

将字符串实现为字符数组,只使用C ++的字符串类型。


#include< string>

int main(){

std :: string StrNam2 =" ..%2F..dkafjkjdflasdkjf&quot ;;

StrNam2 + =" A3D6F20D-7091&quot ;;


}


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";

}


Alzane写道:
我是C ++编程的新手。我有一个练习,我已经编写了代码但收到警告。我可以得到一些帮助吗?

#include" stdafx.h"
#include< string.h>
#include< stdio.h>
#include< stdlib.h>

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

{* char * StrNam1;


你声明指针''StrNam1''但是从不在程序中使用它。

这就是编译器警告你的原因。

char * StrNam2;

StrNam2 =(char *)malloc(strlen(StrNam2)+ 1);


你想在这做什么? ''StrNam2''没有任何价值。

你试图通过调用''strlen'来找到它的长度。这是你得到的第二次警告。无论如何,这不是管理你的记忆的正确方法。如果你想在''StrNam2'中分配一些内存并将指针存储到它上面,你需要给它一些有意义的大小。

''StrNam2''的目的是什么?

sprintf("%s%s",strcat
(" ..%2F。 。%2FCommon%2FASP%2Fquot_choose_lob.asp%3FS ID%3D",
" A3D6F20D-7091"));


''sprintf''应该是你想要结果的字符串

的第一个参数。格式为_second_参数。 RTFM。


''strcat''使用不当。如果你只想要两个文字

生活在两行不同的代码上,但是代表相同的字符串,你就可以在它们之间留下空格:


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

" A3D6F20D-7091"
>
没有逗号,没有''strcat''来电。 ''strcat''要求

的第一个参数是_resulting_ buffer。如果你传递一个文字作为第一个参数,

你要求麻烦。 RTFM。


返回0;
}

编译......
Concatenate.cpp
C:\Program Files \ Mysoftoft Visual
Studio \ MyProjects\dash2hex\Concatenate.cpp(10):警告C4101:
''StrNam1'':未引用的局部变量
C:\Program Files \ Mysoftoft Visual
Studio \ MyProjects\dash2hex\Concatenate.cpp(13):警告C4700:本地
变量''StrNam2''未经初始化使用
链接。 ..

Concatenate.exe - 0个错误,2个警告
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



V




" Alzane" &LT; AZ ****** @ juno.com&GT;在消息中写道

news:37 ************************** @ posting.google.c om ...

"Alzane" <az******@juno.com> wrote in message
news:37**************************@posting.google.c om...
我是C ++编程的新手。我有一个练习,我已经编写了代码但收到警告。我可以得到一些帮助吗?

#include" stdafx.h"


这是一个非标准的标题。请在此处发布

代码中省略。无论如何你不需要这个。

#include< string.h>
#include< stdio.h>
#include< stdlib.h>

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

{* char * StrNam1;
char * StrNam2;


你刚刚定义了两个指针。由于你没有初始化它们,

它们的值是不确定的,未知。即这些指针在任何地方都没有点b $ b。

StrNam2 =(char *)malloc(strlen(StrNam2)+ 1);


在这里你将一个ininitalizaed指针(''StrNam2'')传递给一个函数

(''strlen()''),它要求有效指向零终止

字符数组的指针。结果行为未定义。如果你想要计算一个字符串的长度,你需要一个字符串。


你还需要检查''malloc()'s返回值以查看是否
失败(在这种情况下返回NULL)。


您无论如何都不会在程序中使用''StrNam2',

所以我不确定你想做什么。也许如果你说b $ b解释,我可以提供更具体的建议。

sprintf("%s%s",strcat
(&#。%) 2F ..%2FCommon%2FASP%2Fquot_choose_lob.asp%3FS ID%3D",
" A3D6F20D-7091"));


这是另一个未定义行为的案例。 ''strcat()''将尝试

来修改其第一个参数指向的字符串。但是你给了一个字符串文字

a指针,该语言禁止修改。


另一个''undefined''方面是你的给''printf()''

两个格式说明符(''%s''),但只有一个对应的参数。


如果你想连接两个字符串,你需要一个存储的地方

结果字符串。 (这可能是字符串被加长,

如果已经分配了足够的存储空间)。


返回0;
} >
编译......
Concatenate.cpp
C:\Program Files\Microsoft Visual
Studio \ MyProjects \dash2hex \Concatenate.cpp(10):警告C4101:
''StrNam1'':未引用的局部变量


这是无害的。它只是意味着你创造了一个物品,而b / b $ b从未使用它。使用现代C ++并忘记上面尝试过的低级内存

管理。

-Mike

C:\Program Files \微软Visual
Studio \ MyProjects\dash2hex \Concatenate.cpp(13):警告C4700:本地
变量''StrNam2''未经初始化使用
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




这是真正的问题。

既然你上面所写的内容对我来说都没有足够的意义

试着猜猜你是什么重新尝试,我只能提供这个简单的C ++程序,它输出连接结果

两个字符串。

#include< iostream>

#include< string>


int main()

{

std :: string StrNam1(" Hello");

std :: string StrNam2(" world");

std :: cout<< StrNam1 + StrNam2<< ''\ n'';

返回0;

}


-Mike



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