Re: Struggling with libraries



Tony Burrows wrote:
On Mon, 17 Apr 2006 18:46:44 +0200, Nils O. Selåsdal wrote:

Tony Burrows wrote:
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.
WIthout seeing how it is beeing called, hard to tell.

Be very very sure your 'source' has enough room for
'insrt'.
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.

I'm sure they do. The comp.lang.c FAQ certainly does with a much simpler example here http://c-faq.com/strangeprob/strlitnomod.html which tells you that string literals are not modifiable.

In addition, even if string literals were modifiable, you are trying to make the string longer, so this would not work either:
#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;
}

Since it would go off the end of str1.

If I was writing a function like your insert I would probably make is malloc enough space for a new string and return the resultant string in that rather than modifying one of the input strings.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.



Relevant Pages

  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Struggling with libraries
    ... void insert(char* insrt, char* source, int place){ ... I tried returning temp instead and that doesn't work either, ...
    (comp.lang.c)
  • Re: Struggling with libraries
    ... void insert(char* insrt, char* source, int place){ ... strcpy(source, temp); ... char * insert(char* insrt, char* source, int place){ ...
    (comp.lang.c)

Loading