首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

指针解引用得到异常的值

2014-01-06 
【求助】指针解引用得到错误的值各位朋友好,本人目前在一家金融企业做linux管理,半路自学C/C++已有半年(打算

【求助】指针解引用得到错误的值
各位朋友好,本人目前在一家金融企业做linux管理,半路自学C/C++已有半年(打算后续转行到开发),遇到一个难以理解的问题,代码风格不好,辛苦各位朋友指导。
本人编译环境X86CPU 32bitOS vs2010
问题描述:下面代码我使用get_min_max(L, min, max)函数之后把min,max指针指向了正确的地址(最小值和最大值,暂时未考虑数据重复),但是*min,*max确得到了异常小的负数...实在想不通哪里出问题了,求助各位前辈。
下面代码中,Basic_seq_list是我自己写的一个简单的顺序表,该类中无任何数据合法性检验机制。
非常感谢!
下面代码是main.cpp部分

#include <iostream>
#include "Basic_seq_list.h"

template <typename data_type>
void get_min_max(Basic_seq_list<data_type> L, data_type *&, data_type *&);

template <typename data_type>
void show(Basic_seq_list<data_type> &);

int main()
{
Basic_seq_list<int> L(5);
for (Basic_seq_list<int>::size_t ix = 0; ix != L.get_length(); ++ ix)
{
std::cout << "L[" << ix << "]:" << std::flush;
std::cin >> L[ix];
}

int *min = &L[0];
int *max = &L[0];
get_min_max(L, min, max);
std::cout << sizeof(*min) << std::endl;//静态类型占4byte且用int解释内存块
std::cout << *min << std::endl;//这里为什么不是得到正常值?
system( "PAUSE" );
return 0;
}

template <typename data_type>
void get_min_max(Basic_seq_list<data_type> L, data_type *&min, data_type *&max)
{
min = &L[0];
max = &L[0];
for (size_t ix = 1; ix != L.get_length(); ++ ix)
{
if (L[ix] < *min)
{
min = &L[ix];
}
else if (!(L[ix] < *max))
{
max = &L[ix];
}
}
std::cout << sizeof(*min) << std::endl;//静态类型占4byte且用data_type解释内存块,data_type为int
std::cout << *min << std::endl;//正常得到最小值
}

template <typename data_type>
void show(Basic_seq_list<data_type> &L)
{
for (Basic_seq_list<data_type>::size_t ix = 0; ix != L.get_length(); ++ ix)
{
std::cout << L[ix] << " ";
}
std::cout << std::endl;
}

下面是Basic_seq_list.h
#ifndef _BASIC_SEQ_LIST_H
#define _BASIC_SEQ_LIST_H
#define _RESERVE_SIZE 8
template <typename data_type>
class Basic_seq_list
{
public:
typedef unsigned int size_t;

public:
Basic_seq_list(size_t = 0, size_t = _RESERVE_SIZE);
Basic_seq_list(const Basic_seq_list<data_type> &);
~Basic_seq_list();

public:
void init();

size_t get_length() const;
size_t get_reserve() const;
size_t get_capacity() const;

data_type & get_elem(size_t);
size_t locate_node(data_type) const;

void insert_elem(size_t, const data_type &);
void delete_elem(const data_type &);
void push_back(const data_type &);
void push_front(const data_type &);
void pop_back();
void pop_front();

data_type & operator[](size_t);
Basic_seq_list<data_type> & operator=(const Basic_seq_list<data_type> &);
private:
//顺序表数据部分大小
size_t length;
//顺序表预留部分大小(预留部分为了避免插入元素时过于频繁的新申请堆内存)
size_t reserve;
//顺序表总大小(包含数据部分和预留部分)
size_t capacity;
//data指针指向存储data_type数据类型的顺序表
data_type *data;

void copy_operate(const Basic_seq_list<data_type> &);
};

#include "Basic_seq_list.cpp"
#undef _RESERVE_SIZE
#endif

下面是Basic_seq_list.cpp
#ifndef _BASIC_SEQ_LIST_CPP
#define _BASIC_SEQ_LIST_CPP
#include "Basic_seq_list.h"

template <typename data_type>
Basic_seq_list<data_type>::Basic_seq_list(size_t _length, size_t _reserve)
{
length = _length;
reserve = _reserve;
capacity = length + reserve;
data = new data_type[capacity];
}
template <typename data_type>
Basic_seq_list<data_type>::Basic_seq_list(const Basic_seq_list<data_type> &rhs)
{
copy_operate(rhs);
}
template <typename data_type>
Basic_seq_list<data_type>::~Basic_seq_list()
{
delete [] data;
}

template <typename data_type>
void Basic_seq_list<data_type>::init()
{
if (length != 0)
{
delete [] data;
Basic_seq_list<data_type>::Basic_seq_list();
}
}

template <typename data_type>
size_t Basic_seq_list<data_type>::get_length() const
{
return length;
}

template <typename data_type>
size_t Basic_seq_list<data_type>::get_reserve() const
{
return reserve;
}

template <typename data_type>


size_t Basic_seq_list<data_type>::get_capacity() const
{
return capacity;
}

template <typename data_type>
data_type & Basic_seq_list<data_type>::get_elem(size_t ix)
{
return data[ix];
}

template <typename data_type>
size_t Basic_seq_list<data_type>::locate_node(data_type elem) const
{
size_t ix = 0;
while (true)
{
if (elem == data[ix])
{
return ix;
}
}
return 1;
}

template <typename data_type>
void Basic_seq_list<data_type>::insert_elem(size_t ix, const data_type &elem)
{
if (length == 0)
{
data = new data_type[1 + reserve];
data[0] = elem;
return;
}
else if (length == capacity)
{
reserve = _RESERVE_SIZE;
data_type *_data = new data_type[length + reserve];
for (size_t ix = 0; ix != length; ++ ix)
{
_data[ix] = data[ix];
}
delete [] data;
data = _data;
}
for (size_t n = 0; length - n != ix; ++ n)
{
data[length - n] = data[length - n - 1];
}
data[ix] = elem;
++ length;
-- reserve;
capacity = length + reserve;
}

template <typename data_type>
void Basic_seq_list<data_type>::delete_elem(const data_type &elem)
{
size_t ix = locate_node(elem);
size_t n = 0;
while (true)
{
data[ix + n] = data[ix + n + 1];
if (ix + n + 1 == length - 1)
{
break;
}
++ n;
}
data[ix + n] = 0;
-- length;
++ reserve;
capacity = length + reserve;
}

template <typename data_type>
void Basic_seq_list<data_type>::push_back(const data_type &elem)
{
insert_elem(length, elem);
}

template <typename data_type>
void Basic_seq_list<data_type>::push_front(const data_type &elem)
{
const size_t ix = 0;
insert_elem(ix, elem);
}

template <typename data_type>
void Basic_seq_list<data_type>::pop_back()
{
const size_t ix = length - 1;
delete_elem(get_elem(ix));
}

template <typename data_type>
void Basic_seq_list<data_type>::pop_front()
{
const size_t ix = 0;
delete_elem(get_elem(ix));
}

template <typename data_type>
data_type & Basic_seq_list<data_type>::operator[](size_t ix)
{
return data[ix];
}

template <typename data_type>
Basic_seq_list<data_type> & Basic_seq_list<data_type>::operator=(const Basic_seq_list<data_type> &rhs)
{
if (this != &rhs)
{
delete [] data;
copy_operate(rhs);
}
}

template <typename data_type>
void Basic_seq_list<data_type>::copy_operate(const Basic_seq_list<data_type> &rhs)
{
length = rhs.length;
reserve = rhs.reserve;
capacity = rhs.capacity;
data = new data_type[capacity];
size_t ix = 0;
while (true)
{
data[ix] = rhs.data[ix];
++ ix;
if (ix == length)
{
break;
}
}
}

#endif


[解决办法]
代码太长,懒得看了,
void get_min_max(Basic_seq_list<data_type> &L, data_type *&, data_type *&);


这里第一个参数改成引用传值试试。

热点排行