leetcode链表题报错 runtime error: member access within null pointer of type ‘ListNode‘

今天在做leetcode203:移除链表元素时,反复遇到了报错:runtime error: member access within null pointer of type ‘ListNode’ (solution.cpp),报错提示的意思是试图访问’ListNode空指针类型的成员,就浅浅记录一下修复bug的过程吧。。。。

刚开始的代码是这样的,逻辑是先建立一个头结点放到链表头部,这样就可以统一链表结点删除的操作了,然后创建ListNode类型指针cur,初始化其指向头结点的下一个结点,利用while循环遍历链表,当cur指针指向Null时停止遍历。然后就报错了…

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyNode=new ListNode(0);
        dummyNode->next=head;
        ListNode* cur=dummyNode->next;
        ListNode* tmp=nullptr;
        while(cur!=nullptr){
            if(cur->val==val){
                tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
                cur=cur->next;
            }else   cur=cur->next;
        }
        head=dummyNode->next;
        delete dummyNode;
        return head;
    }
};

报错的代码行是:cur->next=cur->next->next;,报错提示的意思是试图访问’ListNode空指针类型的成员,然后才发现原因出在:当移除的元素位于链表最后一个时,此时cur指向最后一个结点,cur->next为空,而访问cur->nex->next就是访问空指针的成员变量了

那么怎么解决这个问题呢?我最开始想到的办法是是增加判断语句,当cur指向的是最后一个结点时,就不执行
cur->next=cur->next->next;操作,而是将前一个结点的next指针置为空,但是因为cur指向的是当前结点,所以没有办法操作上一个结点,这个办法好像行不通。

于是我参考了代码随想录中的参考答案如下:

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点
        dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作
        ListNode* cur = dummyHead;
        while (cur->next != NULL) {
            if(cur->next->val == val) {
                ListNode* tmp = cur->next;
                cur->next = cur->next->next;
                delete tmp;
            } else {
                cur = cur->next;
            }
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
    }
};

可以发现这个代码与我的代码不同有两点:第一是cur指针初始化时指向的是头结点,而我的代码cur指向是指向头结点的下一个结点,即原始链表的第一个结点,第二是while循环中的条件为cur->next != NULL,即当cur结点的下一个结点不为空时继续循环,而我的代码是cur!=nullptr,即当前结点不为空时继续循环。

区别感觉很小,但是代码随想录中的这份代码并不会报错,原因就在这份代码的循环条件是cur->next != NULL,这样保证了cur->next不为空,因此就不会出现访问空指针的成员变量的情况;而且判断某结点的值时利用的是cur->next ->val进行判断,而不是让cur指针直接指向该结点,即利用cur->val判断,=这样做的好处是判断某一个结点的值时同时也能保留对其上一个结点进行修改的权利,这样当需要删除的元素在链表中最后一个时,可以很方便的将其前一个结点的next指针置为nullptr

于是我参考代码随想录把原始代码改成了下面的代码,但是一时脑瘫的在if(cur->next->val==val)的作用域最后多加了一句cur=cur->next;,以为此时指针也要后移一位,结果又遇到了同样的报错,而且报错转移到了if(cur->next->val==val)这一行,最后才反应过来,如果待删除元素位于链表最后,当执行完cur->next=cur->next->next时,cur->next为空,如果这时执行cur=cur->next;,则cur为空,等到下一次循环判断时,又会访问空指针成员变量,因此这一句不能加,同一个坑踩两次了属于是…而且在需要删除的结点情况下,cur指针不用后移,因为下一次循环cur->next访问的就是被删除节点的下一个结点了,这个逻辑当时也没理清。

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyNode=new ListNode(0);
        dummyNode->next=head;
        ListNode* cur=dummyNode;
        ListNode* tmp=nullptr;
        while(cur->next!=nullptr){
            if(cur->next->val==val){
                tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
                cur=cur->next;
            }else   cur=cur->next;
        }
        head=dummyNode->next;
        delete dummyNode;
        return head;
    }
};

最后总结来说:
1.利用让cur指针的移动范围为:[头结点,倒数第二个结点],比起范围为[第二个结点,最后一个结点]更加合理,可以有效处理待删除元素在链表尾部的问题,也能够避免bug的发生
2.使用链表时要严格检查有没有访问空指针的成员,特别要考虑待删除元素在链表边界的特殊情况

ps:如果细心的话可以发现代码随想录中判断空指针用的是Null,而我的代码里用的是nullptr,当时觉得有些疑惑,这里再简单总结一下在C++中Null和nullptr的区别:用Null表示空指针是C语言中遗留下来的传统,但在C++中可能会引起问题,因此在C++11中引入了nullptr表示空指针,如果要在C++中表示空指针,那么使用nullptr而不是Null.。

  • 32
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Beginning: Microsoft Visual C# 2008 0 About the Authors 9 Credits 10 Contents 12 Introduction 30 Who This Book Is For 30 How This Book Is Structured 31 What You Need to Use This Book 34 Conventions 34 Source Code 35 Errata 35 p2p.wrox.com 36 Part I: The C# Language 38 Chapter 1: Introducing C# 40 What Is the .NET Framework? 40 What Is C#? 45 Visual Studio 2008 46 Summary 48 Chapter 2: Writing a C# Program 50 The Development Environments 51 Console Applications 55 Windows Forms Applications 61 Summary 66 Chapter 3: Variables and Expressions 68 Basic C# Syntax 69 Basic C# Console Application Structure 71 Variables 73 Expressions 83 Summary 93 Exercises 94 Chapter 4: Flow Control 96 Boolean Logic 96 The goto Statement 106 Branching 107 Looping 115 Summary 127 Exercises 127 Chapter 5: More About Variables 130 Type Conversion 130 Complex Variable Types 140 String Manipulation 155 Summary 160 Exercises 161 Chapter 6: Functions 162 Defining and Using Functions 163 Variable Scope 175 The Main() Function 181 Struct Functions 184 Overloading Functions 185 Delegates 187 Summary 190 Exercises 191 Chapter 7: Debugging and Error Handling 192 Debugging in VS and VCE 193 Error Handling 213 Summary 221 Exercises 221 Chapter 8: Introduction to Object-Oriented Programming 222 What Is Object-Oriented Programming? 223 OOP Techniques 229 OOP in Windows Applications 240 Summary 243 Exercises 243 Chapter 9: Defining Classes 246 Class Definitions in C# 246 System.Object 253 Constructors and Destructors 255 OOP Tools in VS and VCE 260 Class Library Projects 266 Interfaces Versus Abstract Classes 270 Struct Types 272 Summary 275 Exercises 276 Chapter 10: Defining Class Members 278 Member Definitions 278 Additional Class Member Topics 291 Interface Implementation 294 Partial Class Definitions 299 Partial Method Definitions 301 Example Application 302 Summary 313 Exercises 313 Chapter 11: Collections, Comparisons, and Conversions 314 Collections 314 Comparisons 340 Conversions 363 Summary 365 Exercises 366 Chapter 12: Generics 368 What Is a Generic? 368 Using Generics 370 Defining Generics 388 Summary 405 Exercises 405 Chapter 13: Additional OOP Techniques 408 The :: Operator and the Global Namespace Qualifier 408 Custom Exceptions 410 Events 412 Expanding and Using CardLib 426 Summary 434 Exercises 435 Chapter 14: C# 3.0 Language Enhancements 436 Initializers 437 Type Inference 443 Anonymous Types 445 Extension Methods 449 Lambda Expressions 455 Summary 466 Exercises 467 Part II: Windows Programming 468 Chapter 15: Basic Windows Programming 470 Controls 471 The Button Control 477 The Label and LinkLabel Controls 480 The TextBox Control 481 The RadioButton and CheckBox Controls 491 The RichTextBox Control 498 The ListBox and CheckedListBox Controls 506 The ListView Control 512 The TabControl Control 523 Summary 528 Exercises 529 Chapter 16: Advanced Windows Forms Features 530 Menus and Toolbars 531 Toolbars 538 SDI and MDI Applications 549 Creating Controls 561 Summary 572 Exercises 572 Chapter 17: Using Common Dialogs 574 Common Dialogs 574 How to Use Dialogs 576 File Dialogs 577 Printing 596 Print Preview 612 FontDialog and ColorDialog 614 Summary 618 Exercises 619 Chapter 18: Deploying Windows Applications 620 Deployment Overview 620 ClickOnce Deployment 621 Visual Studio Setup and Deployment Project Types 634 Microsoft Windows Installer Architecture 635 Creating an Installation Package for the SimpleEditor 639 Building the Project 654 Installation 654 Summary 659 Exercises 660 Part III: Web Programming 662 Chapter 19: Basic Web Programming 664 Overview 665 ASP.NET Runtime 665 Creating a Simple Page 666 Server Controls 673 Event Handlers 674 Input Validation 679 State Management 683 Authentication and Authorization 689 Reading and Writing to an SQL Server Database 697 Summary 706 Exercises 707 Chapter 20: Advanced Web Programming 708 Master Pages 708 Site Navigation 716 User Controls 717 Profiles 720 Web Parts 725 JavaScript 738 Summary 744 Exercises 745 Chapter 21: Web Services 746 Before Web Services 747 Where to Use Web Services 748 Web Services Architecture 751 Web Services and the .NET Framework 754 Creating a Simple ASP.NET Web Service 757 Testing the Web Service 759 Implementing a Windows Client 761 Calling the Service Asynchronously 765 Implementing an ASP.NET Client 768 Passing Data 769 Summary 773 Exercises 774 Chapter 22: Ajax Programming 776 Ajax Overview 776 Update Panel 778 Timer Control 783 Update Progress 784 Web Services 786 Extender Controls 792 Summary 794 Exercises 794 Chapter 23: Deploying Web Applications 796 Internet Information Services 796 IIS Configuration 797 Copying a Web Site 800 Publishing a Web Site 802 Windows Installer 804 Summary 809 Exercises 810 Part IV: Data Access 812 Chapter 24: File System Data 814 Streams 815 The Classes for Input and Output 815 Serialized Objects 842 Monitoring the File Structure 847 Summary 855 Exercises 855 Chapter 25: XML 856 XML Documents 856 Using XML in Your Application 865 Summary 884 Exercises 885 Chapter 26: Introduction to LINQ 886 LINQ Varieties 887 First LINQ Query 887 Using the LINQ Method Syntax and Lambda Expressions 892 Ordering Query Results 895 orderby Clause 897 Ordering Using Method Syntax 897 Querying a Large Data Set 899 Aggregate Operators 902 Querying Complex Objects 905 Projection: Creating New Objects in Queries 909 Projection: Method Syntax 912 Select Distinct Query 912 Any and All 914 Ordering By Multiple Levels 916 Multi-Level Ordering Method Syntax: ThenBy 918 Group Queries 918 Take and Skip 920 First and FirstOrDefault 923 Set Operators 924 Joins 928 Resources and Further Reading 929 Summary 930 Exercises 930 Chapter 27: LINQ to SQL 932 Object -Relational Mapping (ORM) 933 Installing SQL Server and the Northwind Sample Data 933 First LINQ to SQL Query 935 Navigating LINQ to SQL Relationships 944 Drilling Down Further with LINQ to SQL 948 Grouping, Ordering, and Other Advanced Queries in LINQ to SQL 951 Displaying Generated SQL 954 Data Binding with LINQ to SQL 958 Updating Bound Data with LINQ to SQL 965 Summary 966 Exercises 967 Chapter 28: ADO.NET and LINQ over DataSet 970 What Is ADO.NET? 971 Overview of ADO.NET Classes and Objects 974 Reading Data with the DataReader 977 Reading Data with the DataSet 985 Updating the Database 989 Accessing Multiple Tables in a DataSet 1002 XML and ADO.NET 1010 SQL Support in ADO.NET 1014 Using LINQ over DataSet with ADO.NET 1022 Summary 1027 Exercises 1027 Chapter 29: LINQ to XML 1030 LINQ to XML Functional Constructors 1031 Saving and Loading an XML Document 1035 Working with XML Fragments 1039 Generating XML from LINQ to SQL 1041 How to Query an XML Document 1046 Summary 1053 Exercises 1053 Part V: Additional Techniques 1054 Chapter 30: Attributes 1056 What Is an Attribute? 1056 Reflection 1060 Built-in Attributes 1063 Custom Attributes 1075 Summary 1083 Chapter 31: XML Documentation 1086 Adding XML Documentation 1087 Making Use of XML Documentation 1105 Summary 1110 Exercises 1110 Chapter 32: Networking 1112 Networking Overview 1112 Networking Programming Options 1120 WebClient 1121 WebRequest and WebResponse 1123 TcpListener and TcpClient 1133 Summary 1141 Exercises 1142 Chapter 33: Introduction to GDI+ 1144 Overview of Graphical Drawing 1145 Drawing Lines Using the Pen Class 1156 Drawing Shapes Using the Brush Class 1159 Drawing Text Using the Font Class 1161 Drawing Using Images 1165 Advanced Capabilities of GDI+ 1175 Summary 1177 Exercises 1178 Chapter 34: Windows Presentation Foundation 1180 What Is WPF? 1181 Anatomy of a Basic WPF Application 1185 WPF Fundamentals 1197 Programming with WPF 1237 Summary 1249 Exercises 1250 Chapter 35: Windows Communication Foundation 1252 What Is WCF ? 1253 WCF Concepts 1254 WCF Programming 1258 Summary 1282 Exercises 1283 Chapter 36: Windows Workflow Foundation 1284 Activities 1288 The Workflow Runtime 1303 Data Binding 1310 Summary 1315 Index 1316
Welcome to Turbo C++ Version 3.0 -------------------------------- This README file contains important information about Turbo C++. For the latest information about Turbo C++ and its accompanying programs and manuals, read this file in its entirety. TABLE OF CONTENTS ----------------- 1. How to Get Help 2. Installation 3. Features 4. Important Information 5. Testing Your Expanded Memory 6. Corrections to the On-line Help 1. HOW TO GET HELP ------------------- If you have any problems, please read this file, the HELPME!.DOC and other files in your DOC subdirectory, and the Turbo C++ manuals first. If you still have a question and need assistance, help is available from the following sources: 1. Type GO BPROGB on the CompuServe bulletin board system for instant access to the Borland forums with their libraries of technical information and answers to common questions. If you are not a member of CompuServe, see the enclosed special offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on-line charges. 2. Check with your local software dealer or users' group. 3. Borland's TECHFAX service. Call (800) 822-4269 for a FAX catalog of entries. 4. If you have an urgent problem that cannot wait and you have sent in the license agreement that came with the package, you may call the Borland Technical Support Department at (408) 438-5300. Please have the following information ready before calling: a. Product name and serial number on your original distribution disk. Please have your serial number ready or we will be unable to process your call. b. Product version number. The version number for Turbo C++ can be displayed by pressing Alt-H/A. c. Computer brand, model, and the brands and model numbers of any additional hardware. d. Operating system and version number. (The version number can be determined by typing VER at the DOS prompt.) e. Contents of your AUTOEXEC.BAT file. f. Contents of your CONFIG.SYS file. 2. INSTALLATION ---------------- You MUST use the INSTALL program to install Turbo C++. The files on the distribution disks are all archived and have to be properly assembled. You cannot do this by hand! IMPORTANT! If you want to create backup copies of your disks, make sure that you put the backup on the same type of disk as the source. If you're backing up the 5 1/4 inch 1.2 Mb disk set, use only blank 5 1/4 inch 1.2 Mb disks for backup, etc. The installation will not work correctly if you do not use the same media type for the backup disks. To start the installation, change your current drive to the one that has the install program on it and type INSTALL. You will be given instructions in a box at the bottom of the screen for each prompt. For example, if you will be installing from drive A:, type: A: INSTALL - This INSTALL handles the installation of both the compiler and tools in one operation, and allows several new configuration options. - After installation, make sure you insert \TC\BIN - or whatever you selected as your BIN directory - into your DOS path so the executable files can be found. - Note: The list of files is contained in a separate file called FILELIST.DOC, which will appear in the target directory you specify during installation. - After your initial installation, you can run INSTALL again to add elements you omitted the first time. Just select the items you want to add in the INSTALL options screen. Because some things you may want to save could be overwritten, review the following items to make sure you don't lose important information: 1. Selecting CMD (the Command-line compiler) causes an overwrite of any existing turboc.cfg & tlink.cfg file with path information provided in that INSTALL session. Any switches other than -L (library path) and -I (include path) will not be preserved. 2. Selecting IDE will reset the include and library paths to those provided in that INSTALL session. 3. By selecting any one of the following, the help file paths and choices for THELP.CFG will reflect the current session's installation choices: a. CMD - command-line compiler b. IDE - integrated environment 4. Alterations to headers or startup files will be overwritten if any library models are selected. In general, any selection you make of something installed earlier will cause an overwrite of the earlier version without prompting. You should read the rest of this README file to get further information about this release before you do the installation. 3. FEATURES ------------ Turbo C++ 3.0 includes big speed and capacity gains. Here are some important features found in this version: - DPMI services for increased capacity - C++ 2.1 support, including the new nested class specifications, and support of C++ 3.0 templates. - Support for pre-compiled headers for substantial time savings during subsequent recompiles. - Color syntax highlighting - Unlimited Undo/Redo replacing previous 'restore line' capability - Added library functions for compatibility with other runtime libraries, and addition of support for long double parameters in math functions. (Please refer to On-line Help for details.) - New MAKE features. (Please see the MAKE chapter in the User's Guide for details.) - Added BGI (Borland Graphics Interface) fonts and support. (See "New BGI fonts" below.) - A resident DPMI kernel program, DPMIRES.EXE. (See "DPMI" below.) - THELP now allows you to switch between help files without unloading and reloading. (Please see UTIL.DOC for details.) NEW BGI FONTS ------------- Several new fonts have been added to the Borland Graphics Interface: Name Value Description ------------------------------------------- SCRIPT_FONT 5 Stroked script font SIMPLEX_FONT 6 Stroked simplex font TRIP_SCR_FONT 7 Stroked triplex script font COMPLEX_FONT 8 Stroked complex font EURO_FONT 9 Stroked European font BOLD_FONT 10 Stroked bold font The fonts in the BGI now support the full ASCII character set. DPMI ---- TC.EXE, TCC.EXE, and TLINK.EXE are now hosted under DPMI. These files support protected-mode compilation and replace the files of the same name in Turbo C++ Second Edition. Turbo C++ Second Edition should continue to be used in instances where real-mode compilation is desired. If you encounter a "machine not in database" message while attempting to run the compiler, run the DPMIINST program to add your machine configuration to the DPMI server database. This version includes a resident DPMI host program, DPMIRES.EXE, that allows you to preload the server before invoking TC, TCC, or any other DPMI-hosted executables. If you want to run such hosted EXEs in a Windows Standard Mode DOS window, you should run DPMIRES.EXE before loading Windows. To do this, enter the following commands at DOS: set DPMIMEM=MAXMEM 2000 dpmires win /s If you want to limit the amount of extended memory used by the DPMI-hosted executables, an environment variable called DPMIMEM can be set to do so. For instance, the command set DPMIMEM=MAXMEM 2000 reserves about 2 Mb of memory for DPMIRES. The number after MAXMEM can be adjusted, but cannot be lower than 1000. The hosted executables cannot spawn each other when SHARE is loaded. For instance, if you run MAKE on a file which in turn calls MAKE again, you will get a sharing violation. In this specific case, you can call the real mode version, MAKER, within the given makefile, and a sharing violation won't occur. 4. IMPORTANT INFORMATION ------------------------- - When using Brief with THELP, make sure to use Brief's -p switch to ensure that the thelp window will be visible. - We recommend that you use the following mouse drivers with this product: Microsoft Mouse version 7.04 or later; Logitech Mouse version 5.01 or later; Genius Mouse version 9.06 or later. - If you get a "floating point formats not linked" message at runtime, put the following somewhere in your source files: extern void _floatconvert(); #pragma extref _floatconvert This will force inclusion of floating point formats, which may not be linked to reduce executable size. COMPILER - The default extension for source files to the command-line compiler is .CPP; that is, if you enter TCC -c test the compiler will search for test.cpp, and give an error if a file of that name cannot be found. If you want to have the command-line compiler assume a .c extension and C language source, use the command-line option -P-c. For more information, see "The command-line compiler" in the User's Guide. - Note that the Generate COMDEFs choice under Options|Compiler|Advanced Code Generation and the -Fc command- line option are only supported in the C language. Linker errors will result if you attempt to use a communal variable in C++. - The macros min() and max() are not defined when stdlib.h is compiled as C++ (to allow their use in 3rd party libraries, etc.). - Note that SYMDEB creates .SYM files for use in debugging; Turbo C++ creates .SYM files for pre-compiled headers. They are not compatible and collisions should be avoided by setting the name of the pre-compiled header file (using - H=filename). - There is now full support of distance modifiers (near and far) used for class member pointers. Here are two sample declarations and their meanings: void (A::* far var) (); this is a far variable 'var' of type 'void (A::*)()'; void (far A::* var) (); this is a 'default distance' variable 'var' of type 'void (far A::*)()' - If you use C++ templates, and use a separate TLINK command line rather than letting TCC invoke TLINK, you should make sure that you turn on case-sensitive links with the /c switch. - Incorrect code will be generated if you have a statement of the type "A op B" where either A or B is an enum and the other operand is a long, and "op" is one of the following operators: += -= *= /= | ^ The same problem applies when the operands are a non-integer enum and an int. Cast the enum to long or int respectively to solve the problem. IDE - When debugging a mouse application the Options|Debugger|Display Swapping option should be set to "Always" for best results. - In the IDE, the mouse cursor is turned off during compilation for performance improvements. - To run or debug an overlaid application in the IDE when DOS SHARE is loaded, the .EXE file must first be marked as read-only. Otherwise, unload SHARE. - Pressing Control-Break twice while running or stepping a program from the IDE may cause unexpected results. In particular, avoid pressing Control-Break twice in response to any function requiring input (scanf, getch, etc.). To break out of a program during such interaction, press Control-Break and enter a valid input string. Control will be returned to the IDE. EXAMPLE PROGRAMS - When you are running any example programs that come with .PRJ files, if you didn't use the standard directories when you installed Turbo C++ you will have to change the .PRJ file to reflect your actual directory setup. Do this from inside Turbo C++ with Alt-O/D. LINKING C++ WITH C - Linking C++ modules with C modules requires the use of a linkage specification. Prototypes for C functions within C++ modules must be in one of the following forms: extern "C" declaration extern "C" { declarations } For example, if a C module contains these functions: char *SCopy(char*, char*); void ClearScreen(void) they must be declared in a C++ module in one of the following ways: extern "C" char *SCopy(char*, char*); extern "C" void ClearScreen(void); or extern "C" { char *SCopy(char*, char*); void ClearScreen(void); } Failure to do so will result in "Undefined symbol" errors during link. For further examples, see the standard header files. CLASS LIBRARY - Two versions of the class libraries are provided; one that includes debug information and one that does not. Small versions of each are provided, and project files are provided to build other models. Note that the non-debug versions are used by default. If you would like to use the debug version, copy it to the non-debug file. For instance, in the CLASSLIB\LIB directory, copy TCLASDBS.LIB to TCLASSS.LIB for the small model version. - In some places the User's Guide incorrectly refers to the online documentation for the Container Class Libraries as CONTAIN.DOC. The correct file name is CLASSLIB.DOC, located in the ..\DOC directory. 5. TESTING YOUR EXPANDED MEMORY: EMSTEST.COM --------------------------------------------- Included with Turbo C++ is a program to test your Expanded Memory hardware and software. If you have problems using Turbo C++ with your EMS, type EMSTEST at the DOS prompt and follow the instructions. 6. CORRECTIONS TO THE ON-LINE HELP ----------------------------------- The information for alloca is not available in on-line help. The correct help screen should read as follows: ------------------------------------------------------------------ Function: alloca Allocates temporary stack space Syntax: #include <malloc.h> void *alloca(size_t size); Remarks: alloca allocates bytes on the stack. The allocated space is automatically freed up when the calling function exits. Return value: o On success (if enough stack space is available), returns a pointer to the allocated stack area. o On error, returns null. Argument size is the number of bytes allocated on the stack. Because alloca modifies the stack pointer, do no place calls to alloca in an expression that is an argument to a function. NOTE: If the calling function does not contain any references to local variables in the stack, the stack won't be resotored correctly when the function exits and your program will crash. To ensure that the stack is restored correctly, use this code in your calling function: char *p; char dummy[1]; dummy[0] := 0;; ... p = alloca(nbytes); Because alloca is not defined in ANSI C, you should use malloc instead. See also: malloc ------------------------------------------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值