我在接受采访时问了这个问题.
假设char*p = malloc(n)分配多于n,比如分配N个字节的内存,free(p)用于释放分配给p的内存.
堆管理器可以执行这样的错误分配吗?现在发生了什么,将释放n个字节或释放N个字节?
有什么方法可以找到释放多少内存?
编辑
有什么方法可以找到释放多少内存?
有总比没有好,
mallinfo()可以像"Fred Larson"所指出的那样轻松一些
我是silverlight的新手,正在研究mvc中托管的silverlight应用程序.用户将登录aspx页面/ LogOn,并将被重定向到silverlight应用程序或其他视图.要在silverlight中访问已登录用户,请在mvc中添加身份验证服务.
app.xaml.cs基于RIA服务中的启用身份验证进行了修改
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
WebContext webcontext = new WebContext
{
Authentication = new FormsAuthentication()
};
this.ApplicationLifetimeObjects.Add(webcontext);
WebContext.Current.Authentication.LoadUser().Completed +=
(s, e) => MessageBox.Show(WebContext.Current.User.Name);
}
Run Code Online (Sandbox Code Playgroud)
这种方法不起作用,因为消息框显示为空
我试图使用重定向Console.Out到两个文本文件Console.SetOut.
Console.SetOut(File.CreateText("c:\\del1.txt"));
Console.WriteLine(string1);
...
Console.SetOut(File.CreateText("c:\\del2.txt"));
Console.WriteLine(string2);
Run Code Online (Sandbox Code Playgroud)
通过此重定向,可以创建两个文本文件,而不包含任何数据.如果我注释掉第二个重定向,这工作正常.如何使用输出将输出重定向到不同的文件Console.SetOut.
Edit1:程序终止没有任何错误,这是否可以保证所有文件流都被关闭和刷新?
编辑2:感谢每一位回复了我的问题的人,我能够找到解决方案,不用更改代码并添加两行来关闭文件流.
Console.Out.Close();
任何人都可以解释为什么程序终止后文件流没有关闭和刷新?
我是F#的新手。我编写了一个函数,该函数返回目标中子字符串匹配索引的数组,其类似于我在C#中的编写方式。
有没有更实用的方法来解决此问题,并且可以在不使用任何可变变量的情况下解决该问题?
let SubStringIndices (haystack:string) (needle:string) =
let mutable indices = System.Collections.Generic.List<int>()
let mutable index = haystack.IndexOf(needle)
while index >= 0 do
indices.Add(index)
index <- haystack.IndexOf(needle, index+1)
indices.ToArray()
printfn "%A" (SubStringIndices "abaabababaaab" "ab")
// prints [|0; 3; 5; 7; 11|]
Run Code Online (Sandbox Code Playgroud)
我不是在寻找一种在每个索引处检查子字符串是否匹配的解决方案。
试着理解我的问题的答案
当试图释放由堆管理器分配的内存时会发生什么,它分配的内容超过了要求?
我写了这个函数,并对其输出感到困惑
int main(int argc,char **argv){
char *p,*q;
p=malloc(1);
strcpy(p,"01234556789abcdefghijklmnopqrstuvwxyz"); //since malloc allocates atleast 1 byte
q=malloc(2);
// free(q);
printf("q=%s\n",q);
printf("p=%s\n",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
产量
q=vwxyz
p=01234556789abcdefghijklm!
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释这种行为吗?或者这个实现是否具体?
如果free(q)被取消注释,我也会收到SIGABRT.