first step, first careful


第一次发表技术博客,谨以此几句,抒发自己的激动之情。


刚刚从博奥(国企专做MFC界面)辞职,想好好修炼下内功:算法与数据结构。遂从网络上找到一个无名小题磨磨枪,实现一个数组的倒序存储。如a,b,c,d到d,c,b,a; 开始以为很简单,但是手实在太生了,很多基础知识都忘了。现在从这个小程序,逐一开始扩展:

#include "stdafx.h"
#include 
   
   
    
    
using namespace std;

void reverse_order(char * p, int len)
{
    char tmp;
    if (len%2)
    {
        for(int i=0; i<(len-1)/2; i++)
        {
            tmp    =  p[i];
            p[i]   =  p[len-i-1];
            p[len-i-1] =  tmp;
        }
    }
    else
    {
        for(int i=0; i
    
    
   
   

问题1:在用VS2010写一段代码,首先不是算法问题,但是调试的时候发现报 Stack around the variable 'xxx' was corrupted 的错误,这不是什么高大上的错误,后来发现是  数组越界  造成的。问题发现是从一篇博客借鉴的:http://blog.csdn.net/delphiwcdj/article/details/6336808  仔细阅读他的博客,再去对应找自己的数组是否也存在数组越界,发现果然出现在13-14和22-23行代码(13-14和22-23行的代码是本来是一样的,但是为了表达清晰,13-14是正确的不越界的代码,而22-23是错误越界代码)。在22-23行代码中,当 i=0 就马上出现了p[0] = p[len] 的数组越界问题,解决之后,程序正常运行!

问题2:优化设计---将函数打包为函数模板,练习一下

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
    
    
     
     
using namespace std;

template 
     
     
      
                         //These placeholders, in this case, T, are used to specify the types of the function’s
                                        //parameters , to specify the function’s return type  and to declare variables
                                        //within the body of the function definition.
void reverse_order(T * p, int len)
{
	char tmp;
	if (len%2)
	{
		for(int i=0; i<(len-1)/2; i++)
        {
			tmp    =  p[i];
			p[i]   =  p[len-i-1];
			p[len-i-1] =  tmp;
		}
	}
	else
	{
		for(int i=0; i
      
      
     
     
    
    






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值