问题标签 [malloc]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
8402 浏览

c - 为什么使用 realloc() 时会出现双重释放或损坏错误?

我尝试在 C 中编写一个字符串替换函数,该函数适用于char *已使用malloc(). 它有点不同,它会查找和替换字符串,而不是起始字符串中的字符。

如果搜索和替换字符串的长度相同(或者替换字符串比搜索字符串短),这很简单,因为我分配了足够的空间。如果我尝试realloc()使用realloc().

也许一些代码会有所帮助:

该程序有效,直到我尝试realloc()在替换字符串比初始字符串长的情况下。(它仍然有效,它只是吐出错误以及结果)。

如果有帮助,调用代码如下所示:

0 投票
8 回答
1508 浏览

performance - 在多核机器上扩展多线程应用程序

我正在做一个项目,我们需要更多的性能。随着时间的推移,我们继续改进设计以更多地并行工作(线程和分布式)。然后最新的步骤是将其中的一部分移到具有 16 个内核的新机器上。我发现我们需要重新考虑如何在共享内存模型中扩展至那么多内核。例如,标准内存分配器不够好。

人们会推荐什么资源?

到目前为止,我发现 Sutter 的专栏 Dr. Dobbs 是一个好的开始。我刚拿到多处理器编程的艺术和关于英特尔线程构建块的 O'Reilly 书

0 投票
2 回答
1725 浏览

performance - 有人用过谷歌性能工具吗?

寻找有关以下方面的反馈:

http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools

0 投票
5 回答
4986 浏览

c - malloc()/free() 的对齐限制

我读过的较早的 K&R(第 2 版)和其他 C 语言文本讨论了动态内存分配器的实现方式,malloc()并且free()通常还顺便提到了一些关于数据类型对齐限制的事情。显然,某些计算机硬件架构(CPU、寄存器和内存访问)限制了您如何存储和处理某些值类型。例如,可能要求 4 字节 ( long) 整数必须从四的倍数地址开始存储。

主要平台(英特尔和 AMD、SPARC、Alpha)对内存分配和内存访问施加了哪些限制(如果有),或者我可以安全地忽略在特定地址边界上对齐内存分配吗?

0 投票
11 回答
1458 浏览

memory - 如何记录 malloc

这有点假设并且非常简化,但是......

假设一个程序将调用第三方编写的函数。这些当事方可以被认为是非敌对的,但不能被认为是“有能力的”。每个函数都会接受一些参数,有副作用并返回一个值。它们在不运行时没有状态。

目标是通过记录所有 malloc(等)并在函数退出后释放所有内容来确保它们不会导致内存泄漏。

这可能吗?这实用吗?

ps 对我来说重要的部分是确保没有分配持续存在,因此在不这样做的情况下消除内存泄漏的方法对我没有用。

0 投票
5 回答
19022 浏览

c - 函数调用中的 Malloc 似乎在返回时被释放?

我想我已经把它归结为最基本的情况:

输出是这样的:

如果有人能阐明我的理解失败的地方,将不胜感激。

0 投票
6 回答
5932 浏览

c - What is causing a stack overflow?

You may think that this is a coincidence that the topic of my question is similar to the name of the forum but I actually got here by googling the term "stack overflow".

I use the OPNET network simulator in which I program using C. I think I am having a problem with big array sizes. It seems that I am hitting some sort of memory allocation limitation. It may have to do with OPNET, Windows, my laptop memory or most likely C language. The problem is caused when I try to use nested arrays with a total number of elements coming to several thousand integers. I think I am exceeding an overall memory allocation limit and I am wondering if there is a way to increase this cap. Here's the exact problem description:

I basically have a routing table. Let's call it routing_tbl[n], meaning I am supporting 30 nodes (routers). Now, for each node in this table, I keep info. about many (hundreds) available paths, in an array called paths[p]. Again, for each path in this array, I keep the list of nodes that belong to it in an array called hops[h]. So, I am using at least nph integers worth of memory but this table contains other information as well. In the same function, I am also using another nested array that consumes almost 40,000 integers as well. As soon as I run my simulation, it quits complaining about stack overflow. It works when I reduce the total size of the routing table. What do you think causes the problem and how can it be solved? Much appreciated Ali

0 投票
2 回答
1773 浏览

c - pthread_detach 会为我管理我的内存吗?

假设我有以下代码:

分离的线程会释放 malloc 分配的内存,还是我现在必须做的事情?

0 投票
8 回答
14025 浏览

c++ - C/C++ 的多线程内存分配器

我目前有大量的多线程服务器应用程序,我正在四处寻找一个好的多线程内存分配器。

到目前为止,我在:

  • 孙梅
  • 谷歌的 tcmalloc
  • 英特尔的线程构建块分配器
  • 埃默里·伯杰的宝藏

根据我的发现,hoard 可能是最快的,但我在今天之前没有听说过它,所以我怀疑它是否真的像看起来那么好。任何人都有尝试这些分配器的个人经验吗?

0 投票
8 回答
7330 浏览

c - 什么是嵌入式系统的好 C 内存分配器?

我有一个单线程的嵌入式应用程序,它分配和释放大量的小块(32-64b)。基于缓存的分配器的完美场景。虽然我可以尝试编写一个,但它可能会浪费时间,并且没有像已经在前线的一些解决方案那样经过测试和调整。

那么我可以在这种情况下使用的最佳分配器是什么?

注意:我在系统中使用 Lua 虚拟机(这是 80% 以上分配的罪魁祸首),所以我不能轻易地重构我的代码以使用堆栈分配来提高分配性能。