- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试根据从命令行收到的数字集来计算总和,并且我使用一个名为 worker 的配套程序来为我计算。如果收到的数字数量是奇数,它会在数字数量上加一个零,使集合成为偶数。
这是一个易于理解的程序流程(归功于 Alok):
举个例子会更清楚:
假设您要添加 7 个数字:1 2 3 4 5 6 7
./协调员 1 2 3 4 5 6 7
虽然在这组特定的数字上我得到:
Process ID: 15195
Sum of 1 and 2 is 3
Process ID: 15196
Sum of 3 and 4 is 7
Process ID: 15197
Sum of 5 and 6 is 11
Process ID: 15198
Sum of 7 and 0 is 7
*** glibc detected *** ./coordinator: free(): invalid next size (fast): 0x080ec048 ***
后跟堆错误列表。
我相信我没有正确地重新分配指针的大小,我在第一次调用 next_step() 后尝试将旧输出重定向到新输入。所以它试图将数据放入没有空间的内存部分。
更新:
@诺曼
这是我收到的输出:
==3585== Memcheck, a memory error detector
==3585== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3585== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3585== Command: ./coordinator 1 2 3 4
==3585==
calc: 2:
input[0]: 1
input[1]: 2
input[2]: 3
input[3]: 4
==3585== Use of uninitialised value of size 4
==3585== at 0x4076186: ??? (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4079A81: vfprintf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4080F7F: printf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x8048833: main (in /home/bryan/cpp/coordinator)
==3585==
==3585== Conditional jump or move depends on uninitialised value(s)
==3585== at 0x407618E: ??? (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4079A81: vfprintf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4080F7F: printf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x8048833: main (in /home/bryan/cpp/coordinator)
==3585==
==3585== Conditional jump or move depends on uninitialised value(s)
==3585== at 0x4077877: vfprintf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4080F7F: printf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x8048833: main (in /home/bryan/cpp/coordinator)
==3585==
==3585== Conditional jump or move depends on uninitialised value(s)
==3585== at 0x407789B: vfprintf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x4080F7F: printf (in /lib/tls/i686/cmov/libc-2.10.1.so)
==3585== by 0x8048833: main (in /home/bryan/cpp/coordinator)
==3585==
input[4]: 0
==3586== Memcheck, a memory error detector
==3586== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3586== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3586== Command: ./worker 1 2
==3586==
Process ID: 3586
Sum of 1 and 2 is 3
==3586==
==3586== HEAP SUMMARY:
==3586== in use at exit: 0 bytes in 0 blocks
==3586== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3586==
==3586== All heap blocks were freed -- no leaks are possible
==3586==
==3586== For counts of detected and suppressed errors, rerun with: -v
==3586== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)
==3587== Memcheck, a memory error detector
==3587== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3587== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3587== Command: ./worker 3 4
==3587==
Process ID: 3587
Sum of 3 and 4 is 7
==3587==
==3587== HEAP SUMMARY:
==3587== in use at exit: 0 bytes in 0 blocks
==3587== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3587==
==3587== All heap blocks were freed -- no leaks are possible
==3587==
==3587== For counts of detected and suppressed errors, rerun with: -v
==3587== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)
==3585== Invalid write of size 4
==3585== at 0x8048A3A: main (in /home/bryan/cpp/coordinator)
==3585== Address 0x417f0b4 is 8 bytes after a block of size 4 alloc'd
==3585== at 0x4024C6C: malloc (vg_replace_malloc.c:195)
==3585== by 0x4024CF6: realloc (vg_replace_malloc.c:476)
==3585== by 0x8048A25: main (in /home/bryan/cpp/coordinator)
==3585==
==3588== Memcheck, a memory error detector
==3588== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==3588== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==3588== Command: ./worker 3 7
==3588==
Process ID: 3588
Sum of 3 and 7 is 10
==3588==
==3588== HEAP SUMMARY:
==3588== in use at exit: 0 bytes in 0 blocks
==3588== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==3588==
==3588== All heap blocks were freed -- no leaks are possible
==3588==
==3588== For counts of detected and suppressed errors, rerun with: -v
==3588== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)
==3585== Invalid read of size 4
==3585== at 0x8048AB5: main (in /home/bryan/cpp/coordinator)
==3585== Address 0x417f0e0 is 0 bytes after a block of size 0 alloc'd
==3585== at 0x4024C6C: malloc (vg_replace_malloc.c:195)
==3585== by 0x4024CF6: realloc (vg_replace_malloc.c:476)
==3585== by 0x8048A77: main (in /home/bryan/cpp/coordinator)
==3585==
The final sum is: 0==3585==
==3585== HEAP SUMMARY:
==3585== in use at exit: 28 bytes in 2 blocks
==3585== total heap usage: 4 allocs, 2 frees, 32 bytes allocated
==3585==
==3585== LEAK SUMMARY:
==3585== definitely lost: 8 bytes in 1 blocks
==3585== indirectly lost: 0 bytes in 0 blocks
==3585== possibly lost: 20 bytes in 1 blocks
==3585== still reachable: 0 bytes in 0 blocks
==3585== suppressed: 0 bytes in 0 blocks
==3585== Rerun with --leak-check=full to see details of leaked memory
==3585==
==3585== For counts of detected and suppressed errors, rerun with: -v
==3585== Use --track-origins=yes to see where uninitialised values come from
==3585== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 11 from 6)
最佳答案
你应该考虑编写一个函数,我们称它为step_once()
,它将采用input
和n
个数字,并写入到相应的 output
与 m = n/2
元素。上面的n
是输入数字的个数+1,input
的最后一个元素等于0。
在您的驱动程序函数中,假设main()
:如果output
包含1 个数字,您就完成了。否则,您重新分配 input
以包含 n_new = m+1
元素,重新分配 output
以包含 m_new = n_new/2
元素,并再次调用函数 step_once()
。你一直这样做,直到你得到一个数字:
function next_step(input, output, n, m):
n := number of input numbers # this is 1 greater than
# the number of numbers being summed
m := n / 2 # C division
n_children := m
i := 0
while i < m:
fork worker with input[2*i] and input[2*i+1]
get result in output[i]
i := i + 1
function main:
set n := length(input) + 1
set m := n/2
allocate memory for input # n+1 elements, last = 0
allocate memory for output # m elements
set values in input
while True:
next_step(input, output, n, m)
if length or output == 1:
done, return
else:
set n := length(output) + 1
set m := n/2
allocate space for new_input # n elements
set new_input := output + [0]
free input and output
set input := new_input
allocate memory for output # m elements
优点是您可以测试您的 next_step()
函数以确保其正常工作,从而使调试更容易。
举个例子会更清楚:
假设您要添加 7 个数字:1 2 3 4 5 6 7
输入
= [1 2 3 4 5 6 7 0]n
= len(input
) = 8m
= n/2
= 4输出
= [0 0 0 0]output
。output
有 4 个元素,因此我们为新的 input
分配 4+1 = 5 个元素的空间。输入
= [3 7 11 7 0]n
= len(input
) = 5m
= n/2
= 2输出
= [0 0]output
。output
有 2 个元素,因此我们为新的 input
分配 2+1 = 3 个元素的空间。输入
= [10 18 0]n
= len(input
) = 3m
= n/2
= 1输出
= [0]output
。关于c - 使用 fork() 计算接收到的命令行参数的总和时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2214604/
我基本上有三个表: hunt_c_usershunt_c_collected_eggshunt_c_achievements 我目前只使用 hunt_c_users 和 hunt_c_collecte
我已经计算了不同表中计数的总和。这会执行两次,每个 performanceID 一次。现在我想得到两个总和的总和。 下面是我目前做的两个总和的代码: SELECT SUM((COUNT (Bo
我有一个对 2 个值求和的脚本。我计划添加更多值(value),但首先我需要让它发挥作用。人们告诉我给他们 NUMBER 值,所以我这样做了,但现在它甚至没有给出输出。 base = 0; $("#F
我正在尝试计算在我们的数据库中跟踪的花费总额。每个订单文档包含一个字段“total_price” 我正在尝试使用以下代码: db.orders.aggregate({ $group: {
给定 Excel 2013(或更高版本)中的 2 个命名表: tbl发票 ID InvRef Total 1 I/123 45 2 I/234
希望你们一切都好。我来这里是因为我从今天早上开始就试图解决一个问题,我再也受不了了。 这就是上下文:我有一个 excel 工作簿,其中有不同的工作表,其中包含不同国家/地区的不同商业计划。我的目标是制
我有一份报告显示客户订购的产品及其价格: CompanyA Product 7 14.99 CompanyA Product 3 45.95 CompanyA Prod
我使用此python客户端: https://github.com/ryananguiano/python-redis-timeseries 如何汇总所有匹配? ts = TimeSeries(cli
希望创建一个总和和计数公式,该公式将自动调整以适应范围内插入的新行。 例如,如果我在单元格 D55 中有公式 =SUM(D17:D54)。每次我在该范围内插入新行时,我都需要更改公式的顶部范围来解释它
所以,我需要聚合日期相同的行。 到目前为止,我的代码返回以下内容: date value source 0 2018-04-08 15:52:26.1
我有数字输入 数量约为 30 我需要将它们全部汇总到一个字段 我拥有的在下面 查看:
您好,我正在尝试根据以下数据计算过去三个月中出现不止一次的不同帐户 ID 的数量;我想要 2 作为查询结果,因为 test1@gmail.com 和 test2@gmail.com 出现超过 1 次。
我有两个带有以下字段的表: ... orders.orderID orders.orderValue 和 payments.orderID payments.payVal 在 payments.pay
我想按 image_gallery 和 video_gallery 两列的 DESC 进行排序。 SELECT b.*, c.title as category, (S
实际上我的原始数据库为 SELECT sum(data1,data2) as database_value,sum(data3,data4) as database_not_value from t
我试图获取三个分数中每一个的值并将它们相加并显示在“总计:”中。我的问题是,我不知道如何做到这一点,以便每次其中一个分数值发生变化时,相应的总分值也会随之变化。 我可以在某处调用“onchange”来
如何获得按第一个值分组的元组列表中第二个和第三个值的总和? 即: list_of_tuples = [(1, 3, 1), (1, 2, 4), (2, 1, 0), (2, 2, 0)] expec
我正在尝试将我的列表中的整数转换为列表的总和和平均值,并说明任何低于冰点 F<32 的温度。每当我尝试获取总和或平均值时,我都会收到错误提示“+: 'int' 和 'str' 不支持的操作数类型”。我
在我的 ios 项目中,我使用了两个实体 (CoreData):具有一对多关系的 Person 和 Gifts 我知道如何计算给一个人的礼物总和: NSDecimalNumber *orderSum=
我有两个表(输入和类别): CREATE TABLE categories ( iId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sNam
我是一名优秀的程序员,十分优秀!