分段故障(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?
原文:
最满意答案
这就是全部
.
。 它无法在函数之间传递多态参数,因此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, hencef . g
doesn't work iff
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 constraintc
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 getuseConnection 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.
相关问答
更多-
GHC为什么说这两种类型是不同的?(Why does GHC say that these two types are different? How can I get them to be the same?)[2023-04-17]
您对类型类的定义不会限制b可以是什么,这意味着应该允许用户选择他想要的任何类型。 当然你的实例定义不允许这样做,这就是它被拒绝的原因。 你想说的是“a是通过类型b进行参数化的,并且这是我想要使用的类型b”。 这就是你如何说的: class Query a where filter :: a b -> (b -> Bool) -> [b] 然后你编写instance Query Segment而不是instance Query (Segment t)因为类型类定义中的a现在只代表Segment ,而b ... -
在存在GADT和类型家族时,GHC无法推断出类型(GHC could not infer types in presence of GADTs and Type Families)[2022-10-19]
我没有阅读过比我的头脑更多的文章,但我相信这个问题几乎可以肯定是由类型家庭造成的。 你有一个类型的功能 Vec a n1 -> Vec a n2 -> Vec a (Plus n1 n2) 但原则上,类型推断无法识别这一点。 我可以添加到你的代码第二类家庭, type family Plus' (a :: *) (b :: *) :: * type instance Plus' Zero b = b type instance Plus' (Succ a) b = Succ (Plus' a b) 看起 ... -
minus n不会减少,因为minus是在第一个参数上使用模式匹配定义的 : total minus : Nat -> Nat -> Nat minus Z right = Z minus left Z = left minus (S left) (S right) = minus left right 因此,您还需要拆分Z和S n案例: minusReduces : (n : Nat) -> n `minus` Z = n minusReduces Z = ...
-
你使用GADT与隐含的forall会让你感到悲伤。 现在是提及“类型化存在量化” 反模式的好时机 由于你在RealFrac a的定义中包含了RealFrac a约束,你隐式使用forall如下所示: data PointSet a where Halfplane :: forall a. RealFrac a => a -> a -> a -> (a -> a -> Bool) -> a -> PointSet HalfPlane Ellipsoid :: forall a. RealFra ...
-
为什么在伊德里斯没有涉及“mod”的平等不是类型检查?(Why doesn't equality involving “mod” not typecheck in Idris?)[2023-10-25]
这是因为mod函数的偏倚(感谢@AntonTrunov澄清)。 它是多态的,默认数字常量是Integer s。 Idris> :t mod mod : Integral ty => ty -> ty -> ty Idris> :t 3 3 : Integer Idris> :t mod 3 2 mod 3 2 : Integer 对于Integer类型, mod函数不是总计。 而是使用modNatNZ函数,所以所有类型检查完美和工作。 v1 : modNatNZ 3 2 SIsNotZ = 1 v1 = ... -
C ++与类型的多态性(C++ Polymorphism with types)[2021-10-10]
您可以向NodeBase添加模板化函数: class NodeBase { public: templateT* getData(); }; 将对象转换为Node 并返回getData() 。 这是一个工作示例 - 仅包括因为它实际上花了我一段时间来修复循环定义的问题: class NodeBase { public: template T* getData(); }; template 我想你要找的是模式匹配和/或守卫 。 例如Erlang允许这样做: foo(X) when X > 0 -> bar(X); foo(X) when X == 0 -> baz(X); foo(X) -> X. foo("bar", X) -> bar(X); foo(42, X) -> baz(X); foo(_, X) -> X. 前者演示了守卫的使用,后者是一种简单的模式匹配,其中第一个参数是"bar" , 42或其他任何东西。 这两种技术都可以在许多函数语 ...存在类型(existential types)[2024-03-09]
如果你不依赖于使用存在类型,这应该工作: case class Sub[T, B[X] <: Seq[X]](b: B[T]) { def foo[B2[X] <: Seq[X]](other: Sub[T,B2]) = other.b ++ b } If you're not tied to using an existential type, this should work: case class Sub[T, B[X] <: Seq[X]](b: B[T]) { def foo[ ...这就是全部. 。 它无法在函数之间传递多态参数,因此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 = useCo ...粗略地说,是的。 当你使用有界的存在量化时,也就是当量化的类型变量受到一些约束C a => ...限制时,GHC将在构造函数内部存储一些指针,记住C a的方法,以便它们可以稍后访问,当您在该构造函数上进行模式匹配时。 通常使用单个指针,就像许多OOP实现中的vtable到vtable一样。 编译器也可以直接存储指向方法的指针,避免间接。 我认为这是由GHC在类型类中只有一个方法时完成的。 它也可以用在方法很少的情况下,比如说两种方法 - 这会使每个值的内存占用更大,但访问速度更快。 因此,编译代码是完美的, ...相关文章
更多- SOAP Fault 元素详解
- Hadoop Datanode支持磁盘故障代码hack
- Windows7系统蓝屏故障分析:蓝屏代码详解
- Windows XP/Vista/Windows 7常见蓝屏故障分析
- Twitter Storm: distributed and fault-tolerant realtime computation
- 如何按日期分段查询?
- Spark - A Fault-Tolerant Abstraction for In-Memory Cluster Computing
- Facebook的Hadoop应用与故障转移方案
- SecondaryNamenode配置与NameNode故障恢复
- Discretized Streams, 离散化的流数据处理
最新问答
更多- 您如何使用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)