当前位置:网站首页>Summary of C language learning problems (VS)
Summary of C language learning problems (VS)
2022-07-05 12:21:00 【A finger leaf next to the giant】
stay C In language , Summary of strange problems encountered , Development tools are :VS2019
1. Dynamic memory allocation failed :
In Africa main.cpp That is, the main module uses malloc() function , But it is not introduced in the module <stdlib.h> library
because malloc() function , It is <stdlib.h> The contents of the library , If it is not imported into the warehouse , You will not be able to dynamically allocate memory ,
It will cause dynamic memory allocation failure , Variable < Unable to read memory >
2. Use uninitialized variables :
When using structure as parameter , Be careful if you use it improperly , Calling init() The result of the initialization function does not necessarily affect the external variables , This leads to the fact that the external variables , Not initialized , And in the next use , Memory conflict exceptions may occur , At this time, you should pay attention to the variables in your position , Whether it has been initialized .
3. Structure variables as function parameters :
In general , We will extract the code that initializes the structure , Package to init() Function , The initialization is completed by taking the structure variable as the function parameter , But sometimes, for example , Structure variables as parameters , Use... In the initialization function malloc Dynamic memory allocation , Such behavior cannot complete the initialization of external variables . This may lead to 2. Problems in .
Then the application of structural variables as function parameters , You need to make a summary . Due to the length of different attempts , I will make a separate document .
C The language structure is initialized as a function parameter
4. The pointer variable gets the address of the structure member variable
In a stack that holds tree nodes , I want to get the address of a variable in its stack , And return the address through the pointer variable , Then modify the pointer variable data , According to reason , If the pointer variable gets the address of the variable in the stack , Then the data modification of the pointer variable will also affect the data modification of the variables in the stack .
When obtaining the address of variables in the stack , I don't think we need to modify its stack , And just use the secondary pointer , Just return its variable address , But I found that after returning , When modifying its pointer variable , Data that does not affect variables in the stack , This means that only the value is copied , Without actually returning its address .
Then I will stack variables , Change stack pointer as parameter , The same secondary pointer gets the address of its variable on the stack , After returning, continue to run the code to modify the data , Unexpectedly, I found that with the modification of pointer variable data , The variables in the stack have also changed .
Let's compare :
The way to succeed :
Failure mode :
Problem so , And the solution is In this document
5. Pointer to unallocated memory space
For such uninitialized pointers , How to judge whether it has not been initialized ?
Use directly through experiments NULL The way is not feasible , Because after definition , Its value is randomly assigned , therefore , You can define its value as NULL, Then it is possible to make such a judgment .
边栏推荐
- ACID事务理论
- Why do you always fail in automated tests?
- GPS data format conversion [easy to understand]
- Learn the memory management of JVM 02 - memory allocation of JVM
- Simply solve the problem that the node in the redis cluster cannot read data (error) moved
- How can beginners learn flutter efficiently?
- Understand redis persistence mechanism in one article
- Acid transaction theory
- Codeforces Round #804 (Div. 2)
- Redis highly available sentinel mechanism
猜你喜欢
Simple production of wechat applet cloud development authorization login
Why do you always fail in automated tests?
Multi table operation - sub query
Automated test lifecycle
ABAP table lookup program
Matlab boundarymask function (find the boundary of the divided area)
mmclassification 训练自定义数据
Learn memory management of JVM 01 - first memory
How to clear floating?
About cache exceptions: solutions for cache avalanche, breakdown, and penetration
随机推荐
ZABBIX agent2 installation
Seven ways to achieve vertical centering
Tabbar configuration at the bottom of wechat applet
Learn garbage collection 01 of JVM -- garbage collection for the first time and life and death judgment
Swift - enables textview to be highly adaptive
How to recover the information server and how to recover the server data [easy to understand]
PXE启动配置及原理
语义分割实验:Unet网络/MSRC2数据集
Automated test lifecycle
PXE startup configuration and principle
POJ-2499 Binary Tree
Want to ask, how to choose a securities firm? Is it safe to open an account online?
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
Uniapp + unicloud + Unipay realize wechat applet payment function
Semantic segmentation experiment: UNET network /msrc2 dataset
一类恒等式的应用(范德蒙德卷积与超几何函数)
想问问,如何选择券商?在线开户是很安全么?
Swift - add navigation bar
Wireless WiFi learning 8-channel transmitting remote control module
Flutter2 heavy release supports web and desktop applications