首页 \ 问答 \ 分段故障(Segmentation Fault)

分段故障(Segmentation Fault)

所以我需要制作一个符合特定算法的特定哈希码函数。 该算法在这个问题的背景下并不重要。 我遇到了一个段错误,我不确定如何修复它。 我在gdb中调试它,发现它来自访问无效的内存地址。

这是我的代码:

int hash_code(const char* str){
   int len = strlen(str);
   char* dst;
   if(len == 0 )
   return 0;
 else{
   strncpy(dst, str, (len - 1));
   return (hash_code(dst) * 65599) + str[len-1];
  }
}

我非常有信心它来自dst,但我不确定如何解决它,不能解决seg故障。 我将使用或初始化dst以避免这种情况?


So I need to make a specific hashcode function that meets a specific algorithm. The algorithm isn't really important in the context of this question. I'm getting a seg fault and am not sure how to fix it. I debugged it in gdb and found out it's from accessing an invalid memory address.

here's my code:

int hash_code(const char* str){
   int len = strlen(str);
   char* dst;
   if(len == 0 )
   return 0;
 else{
   strncpy(dst, str, (len - 1));
   return (hash_code(dst) * 65599) + str[len-1];
  }
}

I'm pretty confident that it's from the dst, but I'm not sure how to work around it, to not get the seg fault. What would I use or initialize dst with to avoid this?


原文:
更新时间:2022-01-22 18:01

最满意答案

这就是全部. 。 它无法在函数之间传递多态参数,因此f . g 如果f是rank-2多态的话, f . g不起作用。 请注意以下工作:

(~.) :: ((∀ m. Data m => m String) -> z) -> (x -> (∀ m. Data m => m String))
          -> x -> z
(~.) f g x = f (g x)

getKeyValWith :: SomeConn -> Key -> IO String
getKeyValWith c = useConnection c ~. getKeyVal

理想情况下. 会有类似的类型

(.) :: ∀ c . ((∀ y . c y => y) -> z) -> x -> ((∀ y . c y => y) -> x)
               -> x -> z

因此涵盖所有特殊情况,如~. 以上。 但这是不可能的 - 它需要推断出在任何给定情况下最弱的约束条件 - 在传统情况下, cy = y~y₀ - 我很确定这一般是不可计算的。

(一个有趣的问题是,如果编译器在类型检查之前尽可能多地内联,我们可以得到多少,因为它现在已经用$ 。如果它进行了自动eta扩展,它肯定可以得到useConnection c . getKeyVal工作,但自动eta扩展通常不是一个好主意......)

通过将多态参数包装在GADT中来隐藏Rank-2多态性,就像使用SomeConn ,这是通常的解决方法。


This is all about .. It's unable to pass a polymorphic argument between the functions, hence f . g doesn't work if f is rank-2 polymorphic. Notice the following works:

(~.) :: ((∀ m. Data m => m String) -> z) -> (x -> (∀ m. Data m => m String))
          -> x -> z
(~.) f g x = f (g x)

getKeyValWith :: SomeConn -> Key -> IO String
getKeyValWith c = useConnection c ~. getKeyVal

Ideally, . would have a type like

(.) :: ∀ c . ((∀ y . c y => y) -> z) -> x -> ((∀ y . c y => y) -> x)
               -> x -> z

and thus cover all special cases like ~. above. But this is not possible – it would require inferring the weakest possible constraint c to pick in any given situation – in the traditional case, c y = y~y₀ – and I'm pretty sure that is uncomputable in general.

(An interesting question is how far we could get if the compiler just inlined . as much as possible before type-checking, as it right now already does with $. If it did automatic eta-expansion, it could certainly get useConnection c . getKeyVal to work, but automatic eta-expansion is in general not a good idea...)

Hiding the Rank-2 polymorphism by wrapping the polymorphic argument in a GADT, as you did with SomeConn, is the usual workaround.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)