苦苦挣扎着图书馆 [英] Struggling with libraries

查看:51
本文介绍了苦苦挣扎着图书馆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习C作为另一种语言,而我正在尝试将一些

实用程序构建到库中。我有这个(粗略的我知道)功能:


void insert(char * insrt,char * source,int place){

char temp [strlen( INSRT)+ strlen的(源)+1]; strncpy(temp,source,place);

temp [place] =''\ 0'';

strcat(temp,insrt);

strcat(临时,来源+地方);

strcpy(来源,临时);

}


简单的文件,这一切都有效,但在静态库中,当我调用它时,我的b $ b得到了一个segnmentation故障。恶棍是最后的strcpy。


我尝试返回临时而且也不起作用,我没有得到

相当的字符串函数中存在(并且编译器警告

返回一个局部变量)。


我知道有解决方案,我只是不知道这是什么。任何帮助都会很棒。


Tony

I''m just learning C as another language, and I''m trying to build some
utilities into a library. I have this (crude I know) function:

void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1]; strncpy(temp, source, place);
temp[place]=''\0'';
strcat(temp, insrt);
strcat(temp, source+place);
strcpy(source, temp);
}

In a simple file, it all works, but in a static library, when I call it I
get a segnmentation fault. The villain is the final strcpy.

I tried returning temp instead and that doesn''t work either, I don''t get
quite the string that exists in the function (and the compiler warns about
returning a local variable).

I know there''s a solution, I just have no idea what it is. Any help would
be great.

Tony

推荐答案

Tony Burrows写道:
Tony Burrows wrote:
我只是在学习C作为另一种语言,我正在尝试将一些
实用程序构建到库中。我有这个(粗略的我知道)功能:

void insert(char * insrt,char * source,int place){
char temp [strlen(insrt)+ strlen(source)+ 1]; strncpy(temp,source,place);
temp [place] =''\ 0'';
strcat(temp,insrt);
strcat(temp,source + place);
strcpy(source,temp);


在一个简单的文件中,一切正常,但在静态库中,当我调用它时,我会得到一个分解故障。恶棍是最后的strcpy。

我尝试返回temp而不是也工作,我没有得到
相当函数中存在的字符串(和编译器警告
返回一个局部变量)。
返回指向自动对象的指针是违法的。


我知道有一个解决方案,我只是不知道它是什么。任何帮助都会很棒。
I''m just learning C as another language, and I''m trying to build some
utilities into a library. I have this (crude I know) function:

void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1]; strncpy(temp, source, place);
temp[place]=''\0'';
strcat(temp, insrt);
strcat(temp, source+place);
strcpy(source, temp);
}

In a simple file, it all works, but in a static library, when I call it I
get a segnmentation fault. The villain is the final strcpy.

I tried returning temp instead and that doesn''t work either, I don''t get quite the string that exists in the function (and the compiler warns about
returning a local variable). It''s illegal to return pointers to auto objects.

I know there''s a solution, I just have no idea what it is. Any help would
be great.




没有看到它如何被召唤,很难说。


非常确定你的''来源''有足够的空间来支付

''insrt''。



WIthout seeing how it is beeing called, hard to tell.

Be very very sure your ''source'' has enough room for
''insrt''.


Tony Burrows写道:
Tony Burrows wrote:
我只是在学习C作为另一种语言,而我正在尝试将一些
实用程序构建到库中。我有这个(粗略的我知道)功能:

void insert(char * insrt,char * source,int place){
char temp [strlen(insrt)+ strlen(source)+ 1]; strncpy(临时,来源,
地方); temp [place] =''\ 0'';
strcat(temp,insrt);
strcat(temp,source + place);
strcpy(source,temp);
}

在一个简单的文件中,一切正常,但在静态库中,当我调用
时,我得到了一个segnmentation错误。恶棍是最后的strcpy。

我尝试返回temp而不是也工作,我不会得到函数中存在的字符串(和警告关于返回局部变量的警告。

我知道有一个解决方案,我只是不知道它是什么。任何帮助都会很棒。
I''m just learning C as another language, and I''m trying to build some
utilities into a library. I have this (crude I know) function:

void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1]; strncpy(temp, source,
place); temp[place]=''\0'';
strcat(temp, insrt);
strcat(temp, source+place);
strcpy(source, temp);
}

In a simple file, it all works, but in a static library, when I call
it I get a segnmentation fault. The villain is the final strcpy.

I tried returning temp instead and that doesn''t work either, I don''t
get quite the string that exists in the function (and the compiler
warns about returning a local variable).

I know there''s a solution, I just have no idea what it is. Any help
would be great.




您需要帮助我们。发布一个COMPLETE最小程序,演示

问题。我很有可能,源代码中没有足够的空间来包含扩展的字符串,但我们怎么知道呢?



Brian



You need to help us. Post a COMPLETE minimal program that demonstrates
the problem. I all likelihood, there is not sufficient room in source
to contain the expanded string, but how can we tell?


Brian


2006年4月17日星期一18:46:44 +0200,Nils O. Sel?sdal写道:
On Mon, 17 Apr 2006 18:46:44 +0200, Nils O. Sel?sdal wrote:
Tony Burrows写道:
Tony Burrows wrote:
我只是在学习C作为另一种语言,我正在尝试将一些
实用程序构建到库中。我有这个(粗略的我知道)功能:

void insert(char * insrt,char * source,int place){
char temp [strlen(insrt)+ strlen(source)+ 1]; strncpy(temp,source,place);
temp [place] =''\ 0'';
strcat(temp,insrt);
strcat(temp,source + place);
strcpy(source,temp);


在一个简单的文件中,一切正常,但在静态库中,当我调用它时,我会得到一个分解故障。恶棍是最后的strcpy。

我尝试返回temp而不是也工作,我没有得到
I''m just learning C as another language, and I''m trying to build some
utilities into a library. I have this (crude I know) function:

void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1]; strncpy(temp, source, place);
temp[place]=''\0'';
strcat(temp, insrt);
strcat(temp, source+place);
strcpy(source, temp);
}

In a simple file, it all works, but in a static library, when I call it I
get a segnmentation fault. The villain is the final strcpy.

I tried returning temp instead and that doesn''t work either, I don''t get


相当函数中存在的字符串(并且编译器警告
返回一个局部变量)。
quite the string that exists in the function (and the compiler warns about
returning a local variable).


返回指向自动对象的指针是违法的。
< br>


It''s illegal to return pointers to auto objects.

我知道有一个解决方案,我只是不知道它是什么。任何帮助都会很棒。
I know there''s a solution, I just have no idea what it is. Any help would
be great.



没有看到它如何被召唤,很难说。

非常确定你的来源''有足够的空间来'
''insrt''。



WIthout seeing how it is beeing called, hard to tell.

Be very very sure your ''source'' has enough room for
''insrt''.



我从一个简单的小程序中调用它:


#include< stdio.h>

#include" insert.h"


int main(void){

char * str1 ="你是一个疯子!" ;;

char * str2 =" big";

puts(str1);

puts(str2);

insert(str2,str1,10);

返回0;

}


insert.h是

#include< string.h>

void insert(char * insrt,char * source,int place );

void insertn(char * insrt,char * source,int place,int size);


使用gcc -o testInsert编译testInsert.c insert .c


如果我把打印语句放入,那么分段错误发生在

插入乐趣中执行strcpy(source,temp)时的ction。


如果我在堆上创建了一个字符串(我认为)malloc并返回了一个

指针那,那会解决这个问题吗?如果是这样,它是正确的

方式? C编程书似乎无法触及这个。


Tony


I''ve called it from a simple little program:

#include <stdio.h>
#include "insert.h"

int main(void){
char *str1 = "You are a nutcase!";
char *str2 = "big ";
puts(str1);
puts(str2);
insert(str2, str1, 10);
return 0;
}

insert.h is
#include <string.h>
void insert(char* insrt, char* source, int place);
void insertn(char* insrt, char* source, int place, int size);

Compiled with gcc -o testInsert testInsert.c insert.c

If I put print statements in, then the segmentation fault happens in the
insert function when strcpy(source, temp) is executed.

If I created a string on the heap with (I think) malloc and returned a
pointer to that, would that deal with the problem? If so, is it the right
way? The C programming books don''t seem to touch on this.

Tony


这篇关于苦苦挣扎着图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆