VC编程学习.doc_第1页
VC编程学习.doc_第2页
VC编程学习.doc_第3页
VC编程学习.doc_第4页
VC编程学习.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一, 创建一个win32,应用程序(Win32 Application),包含了,创建SOCKET时出错。引起出错的语句:SOCKET myUDPSock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP),问题现象:编译可以通过,连接时有如下错误,超找帮助发现可能执行函数时,没有连接到库,在Projects-settingslink中加入ws2_32.lib链接,问题解决。(2010-8-8)-Configuration: WINMAIN3 - Win32 Debug-Linking.WINMAIN3.obj : error LNK2001: unresolved external symbol _imp_socket12Debug/WINMAIN3.exe : fatal error LNK1120: 1 unresolved externalsError executing link.exe.WINMAIN3.exe - 2 error(s), 0 warning(s)二、出现预编译文件找不到,问题原因可能是一个工作区包含了多个工程,第一个工程编译编译运行后,就会在工程设置里增加预编译头文件,新建的工程继承了原来工程的设置,所以在运行时会找原来的文件而找不到。解决方法:更改工程设置,去掉这个预编译头文件如图: 2010-8-14三、free和malloc的使用注意事项在一次编程中出现下面的断言错误,经过反复试验,发现是free的指针所指的内存不是malloc分配的,而是一个数组在定义时分配的。结合msdn对free和malloc的解释:The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.可以看出malloc分配的内存是从heap(堆)中获得的,free函数也就只能free堆里的内存,因此,当调用函数去free一快栈里的内存是,就会有下面的错误。用来验证此错误的程序: 2010-8-15#include #include #include typedef struct testchar *pch;test_t;int test_init(test_t * p_test);int test_set(test_t *p_test, char* ch);/int test_set_chrs(test_t *p_test, const char* ch);int test_free(test_t *p_test);int main()test_t *ptr_test;char chrs = tests;/printf(length of chrs %dn,strlen(chrs);char *ptr_string;int length;length = strlen(chrs);ptr_string = (char*)malloc(length+1)*sizeof(char);strcpy(ptr_string,chrs);*(ptr_string+length) = 0;test_init(&ptr_test);/test_set(ptr_test,ptr_string);test_set(ptr_test,chrs); /run this command,then run free may cause a errorprintf(%sn,(ptr_test-pch);test_free(ptr_test);/test_set(&ptr_test,chrs);return 1;int test_init(test_t *p_test)(*p_test) = (test_t*)malloc(sizeof(test_t);(*p_test)-pch = NULL;return 1;int test_set(test_t * p_test,char *ch)p_test-pch = ch;return 1;int test_free(test_t *p_test)free(p_test-pch); /Notice: free function must free memory allocated by malloc()free(p_test);return 1;/*int test_set_chrs(test_t *p_test, const char* ch)if (p_test=NULL)return -1;p_test-pch = (char*)malloc(strlen(ch)*sizeof(char);strcpy(p_test-pch,ch);*/四、堆内存和栈内存的小区别1 、栈区( stack ) 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限.2 、堆区( heap ) 亦称动态内存分配.程序在运行的时候用malloc或new申请任意大小的内存,程序员自己负责在适当的时候用free或delete释放内存。动态内存 的生存期可以由我们决定,如果我们不释放内存,程序将在最后才释放掉动态内存.但是,良好的编程习惯是:如果某动态内存不再使用,需要将其释放掉,否则, 我们认为发生了内存泄漏现象。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表.3、字面常量区 char * pstr = “hello”; pstr0 = c; / 错误,不能修改五、函数编译可以通过,但连接错误,一般原因可能是有了函数的声明,但是没有函数的定义(实现),会导致连接出错。unresolved external symbol int _cdecl application_cb_snd_message(struct osip_transaction *,struct osip_message *,char *,int,int) (?application_cb_snd_messageYAHPAUosip_transactionPAUosip_messagePADHHZ) 六、连接出错unresolved external symbol _GetIfEntryLinking.eXutils.obj : error LNK2001: unresolved external symbol _GetIfEntry4eXutils.obj : error LNK2001: unresolved external symbol _GetIpAddrTable12Debug/exosip.dll : fatal error LNK1120: 2 unresolved externalsError executing link.exe.通过查找msdn,知道这个函数需要包含库Iphlpapi.lib七、对话框中如何响应键盘消息VC对话框里如何响应键盘消息VC学习 2008-03-18 09:48:31 阅读80 评论0 字号:大中小 订阅 在VC中,如果需要在对话框中响应键盘消息,是不可能直接做到的,必须通过添加虚函数才能进行处理。具体过程如下: 1.在所需要响应键盘消息的对话框所对应的类名上单击右键,选择“添加虚函数(Add Virtual Function. )”; 2.在弹出来的对话框中选择PreTranslateMessage并添加; 3.编辑PreTranslateMessage函数; BOOL CDrawVoltDlg:Pre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论