对近段时间遇到的c问题总结.

来源:互联网 发布:java聊天系统源码 编辑:程序博客网 时间:2024/05/19 07:07

对近段时间遇到的c问题总结.
1.指针和数组
   有程序段:
 char *getmemory(void){
  char pcBuf[]="hello world/r/n";
  return pcBuf;
 }
 int main()
 {
  char *str=NULL;
  str=getmemory();
  printf("%s",str);
          return 0;
 }
在输出的时候是hell*(*是一个圆脸)
如果把char pcBuf[]="hello world/r/n";
改成char *pcBuf="hello world/r/n";
就能输出hello world了.
经过csdn上各位大侠的指教,现在还是明白一二了.

具体是:
内存分配有两种方式,一种是在堆上分配,也就是malloc和new,一个是用于C,一个是用于C++,在堆上

分配的内存要用free和delete来释放.
另一种分配内存的方式是在栈上分配,就比如char p[10],就会在栈上开辟十个字节的空间,在栈上分配

的空间会在其作用域不存在时自动释放,比如你在一个函数中声明一个这样的变量,当这个函数退出时

,这个变量所指向的空间会被收回;
所以你在子函数中声明一个char *变量,并用malloc分配空间后,并没有手动释放,那么在其它函数访

问它就是合法的,但程序结束时就手动释放它,不然的话,就有内存泄漏的问题
你在子函数中声明一个数组,它会在栈上分配空间,当此子函数结束后,些内存中的内容会被自动回收
所以你在调用的函数中就引用了一个被释放了的地址,当然要出错了.

char pcBuf[]="hello world/r/n";
使用的是栈内存,一旦函数结束,这块内存就不可用了;
char *pcBuf="hello world/r/n";
令pcBuf指向一个常量字符串的首地址,常量字符串是用全局变量存放的,函数结束后依然有效!

所以,也可以在getmemory中用malloc分配一段内存来达到效果.

2.C Run-Time Error R6002
floating-point support not loaded
有程序段:
 void main(){
  float m;
  scanf("%f",&m); 
 }
在运行的时候,就会在输入的时候提示为R6002错误.意思是浮点库没有加载.经过查阅msdn,有原话:
The program needs the floating-point library, but the library was not linked to the

program.

One of the following may have occurred:

The program was compiled or linked with an option (such as /FPi87) that required a

coprocessor, but the program was run on a machine that did not have a coprocessor

installed.


A format string for a printf or scanf function contained a floating-point format

specification, and the program did not contain any floating-point values or variables.


The compiler minimizes a program's size by loading floating-point support only when

necessary. The compiler cannot detect floating-point format specifications in format

strings, so it does not load the necessary floating-point routines.


Use a floating-point argument to correspond to the floating-point format specification, or

perform a floating-point assignment elsewhere in the program. This causes floating-point

support to be loaded.


In a mixed-language program, a C library was specified before a FORTRAN library when the

program was linked. Relink and specify the C library last.

网上有大侠总结了一下,只要出现浮点常量,浮点变量初始化,浮点数当作实参调用,连接器就会将那些

浮点函数连接上去.此也为解决的方法.^_^

3.bool和BOOL的区别
 用sizeof来查看他们的大小,bool为1,而BOOL为4.
 在C++编程里,BOOL是MFC中的逻辑变量,出现的要比C++中的bool出现得要早一些.TRUE,FALSE就是BOOL

类型数据的两可能的值.而true,false就是bool数据的值.一般在平常的编程里还是用bool的多,因为用

BOOL的话,一是要包含头文件,二是除了MFC中的函数,其他函数都不支持BOOL.但在MFC编程里是要用BOOL

的.

4.define定义中的dowhile(0)-----多出现linux源码中
http://www.kernelnewbies.org/FAQ/DoWhile0

Why do a lot of #defines in the kernel use do { ... } while(0)?

There are a couple of reasons:

(from Dave Miller) Empty statements give a warning from the compiler so this is why you see #define FOO do { } while(0).

(from Dave Miller) It gives you a basic block in which to declare local variables.

(from Ben Collins) It allows you to use more complex macros in conditional code. Imagine a macro of several lines of code like:

#define FOO(x) /
        printf("arg is %s/n", x); /
        do_something_useful(x);
Now imagine using it like:

if (blah == 2)
        FOO(blah);
This interprets to:

if (blah == 2)
        printf("arg is %s/n", blah);
        do_something_useful(blah);;
As you can see, the if then only encompasses the printf(), and the do_something_useful() call is unconditional (not within the scope of the if), like you wanted it. So, by using a block like do { ... } while(0), you would get this:

if (blah == 2)
        do {
                printf("arg is %s/n", blah);
                do_something_useful(blah);
        } while (0);
Which is exactly what you want.

(from Per Persson) As both Miller and Collins point out, you want a block statement so you can have several lines of code and declare local variables. But then the natural thing would be to just use for example:

#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The following code is meant to be an if-statement with two branches:

if (x > y)
        exch(x,y);          // Branch 1
else 
        do_something();     // Branch 2
But it would be interpreted as an if-statement with only one branch:

if (x > y) {                // Single-branch if-statement!!!
        int tmp;            // The one and only branch consists
        tmp = x;            // of the block.
        x = y;
        y = tmp;
}
;                           // empty statement
else                        // ERROR!!! "parse error before else"
        do_something();
The problem is the semi-colon (;) coming directly after the block. The solution for this is to sandwich the block between do and while (0). Then we have a single statement with the capabilities of a block, but not considered as being a block statement by the compiler. Our if-statement now becomes:

if (x > y)
        do {
                int tmp;
                tmp = x;
                x = y;
                y = tmp;
        } while(0);
else
        do_something();

原创粉丝点击