我正在使用Visual Studio 2013.我收到Access violation reading location 0xCDCDCDD1错误.来自维基百科的参考文献:Magic Numbers,这里,这个问题告诉我,我正在使用未初始化的指针.我认为问题在于malloc我正在使用的功能,尽管在使用之前我确实参考过CPlusPlus文档.
我的代码:
#include "SingleNode.h"
#include "SingleList.h"
#include <iostream>
SingleList::SingleList() {}
SingleList::~SingleList() {}
...
void SingleList::addHead(int value){
ISingleNode *temp1 = (ISingleNode *)malloc(sizeof(SingleList));
head = NULL;
temp1->setValue(value);
temp1->setNext(head->getNext());
if (!head->getNext())
{
head = temp1;
}
else
{
temp1->setNext(head->getNext());
head = temp1;
}
}
...
Run Code Online (Sandbox Code Playgroud)
错误在行中 temp1->setValue(value);
该setValue(int value)功能包括在这里:
#include "SingleNode.h"
SingleNode::SingleNode() {}
SingleNode::~SingleNode() {}
void SingleNode::setValue(int value){
this->value = …Run Code Online (Sandbox Code Playgroud) 据记载,在堆上分配的变量存储在低地址区域中并向堆栈增长,反之亦然.我决定测试一下:
#include <stdio.h>
#include <stdlib.h>
const char my_const_global_var = '0';
char my_global_var = '0';
int main(void) {
char my_stack_var = '0';
char* my_heap_var = (char*) malloc(1);
*my_heap_var = '0';
}
Run Code Online (Sandbox Code Playgroud)
看起来my_const_global_var并且my_global_var在低地址区域(000XXXXX在堆之前和之后不久)解决了,但令我惊讶的是,my_stack_var它正好在75%左右(大约是bffbdaXX).我猜我的全局/我会得到一个段错误)堆/堆栈变量超过3 GB的内存,所以我做了一个搜索,发现提到了一个3 GB的屏障,但没有提到剩下的1 GB可寻址空间会发生什么.
剩下的25%的内存地址空间会发生什么?
这是我得到的:
class MyClass {
int holder;
public:
MyClass() {
holder = 5;
}
};
Run Code Online (Sandbox Code Playgroud)
template<class T>
class First {
std::vector<T> items;
public:
First() {
T* tmp;
for (int i = 0; i < 20; i++) {
tmp = new T();
items.push_back(*tmp);
}
};
~First() {
for (int i = 0; i < 20; i++) {
delete items.at(i);
}
};
};
Run Code Online (Sandbox Code Playgroud)
class Second {
std::vector<std::deque<First<MyClass>>> items;
public:
Second() {
std::deque<First<MyClass>>* tmp;
for (int i = 0; i < 10; i++) { …Run Code Online (Sandbox Code Playgroud) 基本上,如果我有类A,A类持有类的实例B,而无需使用new,我创建一个类的实例A使用new.类中B的类实例是否A存储在堆栈或堆中?
任何人都可以向我解释如何在应用程序动力学的帮助下查找内存泄漏。我试图进行搜索,但找不到任何合适的答案。
我想知道是否有办法构建一个支持常量get-min,delete-min和merge操作的优先级队列结构.我不关心插入的时间复杂性,也不必支持reduce-key操作.我的用例在(坏)伪代码中:
func periodical(current_state) {
// always_executed_jobs is a priority queue
queue = always_executed_jobs;
// other_jobs is an array of priority queues;
// current_state is an index to the array, so
// sometimes_executed_jobs is another priority queue
sometimes_executed_jobs = other_jobs[current_state];
queue.merge(sometimes_executed_jobs);
while (!queue.empty()) {
job = get_min(queue);
execute(job);
delete_min(queue);
}
}
Run Code Online (Sandbox Code Playgroud)
我考虑过splay树(特别是https://cs.stackexchange.com/questions/524/does-there-exist-a-priority-queue-with-o1-extracts)和Fibonacci堆,但他们不喜欢似乎满足这些要求.
问题是在字典中找到三个最常见的单词.我已经提出了下面的代码,但由于某些原因它不起作用(我的意思是当我尝试在eclipse中运行它时,它直接导致我调试页面,虽然我没有在编译器屏幕上得到任何错误),我调试后找不到原因.你能帮我找到问题吗?
java.util.PriorityQueue.add
(未知来源)中的线程"main"java.lang.NullPointerException中的异常,
位于
generalquestions.MostCommonWords.mostCommonStringFinder(MostCommonWords.java:41)的java.util.PriorityQueue.add (未知来源).MostCommonWords.main
(MostCommonWords.java:61)
public static Queue<Integer> mostCommonStringFinder (String document, int k){
if (document == null){
throw new IllegalArgumentException();
}
if (document.isEmpty()){
throw new IllegalArgumentException("Document is empty");
}
String [] wordHolder = document.split(" ");
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (String s : wordHolder){
if (!map.containsKey(s)){
map.put(s, 1);
}
else{
int value = map.get(s);
value++;
map.put(s, value);
}
}
Queue<Integer> minHeap = new PriorityQueue<>();
for ( int i = 0 ; i < k ; …Run Code Online (Sandbox Code Playgroud) 我想知道为什么我不能将一个抽象对象数组初始化到堆栈而不是堆.
这里有一些类似于我的C++代码在最后一行失败了.我主要只是对处理堆与堆栈的问题背后的原因感到好奇.提前致谢!
#define ARRAY_SIZE 10
class Obj {
public:
virtual void fn() =0;
};
class Sub : public Obj {
public:
void fn() {
// ...
}
};
Obj * o1_array[ARRAY_SIZE];
Obj * o2_array = new Obj[ARRAY_SIZE]; // Compiler Error
Run Code Online (Sandbox Code Playgroud) 考虑以下程序(请参阅此处的实时演示)
import std.stdio;
class myclass
{
public:
int get_a()
{
return a;
}
private:
int a=3;
}
int main()
{
myclass m; // It should be myclass m=new myclass();
writefln("%d",m.get_a());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++支持自动(堆栈分配)和动态(堆分配)对象.但为什么每个类对象必须在D中动态分配?为什么D不支持堆栈分配的对象?
另一个令人惊讶的事情是,ideone编译器错误为:
prog.d(14): Error: null dereference in function _Dmain
Run Code Online (Sandbox Code Playgroud)
但是当我在dmd2编译器上的本地机器上尝试它时,它给了我运行时错误而不是编译时错误.为什么?为什么这个程序的行为有所不同?以下是我在dmd2给出的本地机器上的错误.
object.Error@(0): Access Violation
----------------
0x00402056
0x00405F9B
0x00405EB1
0x00403D93
0x7651EE6C in BaseThreadInitThunk
0x7758377B in RtlInitializeExceptionChain
0x7758374E in RtlInitializeExceptionChain
Run Code Online (Sandbox Code Playgroud) 当我用C malloc系列函数动态分配内存时,是否有一些规则(来自C标准或操作系统的内部工作)关于该内存的初始值是什么?
int* ptr = malloc(sizeof (*ptr));
bool b = *ptr == 0; // always true?
Run Code Online (Sandbox Code Playgroud) heap ×10
c++ ×4
stack ×4
algorithm ×2
c ×2
heap-memory ×2
memory ×2
appdynamics ×1
class ×1
d ×1
destructor ×1
dictionary ×1
exception ×1
java ×1
jvm ×1
object ×1
splay-tree ×1
templates ×1