Segmentation error for array operation.

L

Logan

/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
 
J

jacob navia

Logan said:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;

This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
 
L

Logan

Fri, 14 Dec 2007 12:23:45 +0100¿¡, jacob navia ½è½À´Ï´Ù:
This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
Thank you, but since I've changed that line to

char smaller;

Now I don't get any output when I am expecting

abc

int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
 
M

Mark Bluemel

Logan said:
Fri, 14 Dec 2007 12:23:45 +0100¿¡, jacob navia ½è½À´Ï´Ù:
This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
Thank you, but since I've changed that line to

char smaller;


Variable Length Arrays are specific to C99 (just in case you didn't
know that).
Now I don't get any output when I am expecting

abc

"smaller" has automatic storage duration, so it's not guaranteed
that it will be available to the calling routine...

The normal approach to this sort of thing is either :-

a) have getSubString() use malloc() to allocate space for the data
it returns. In this case callers have to ensure they free()
the returned pointer at some point.
b) have callers to getSubString() pass it a location into which it
can store its result.

I also note that your code doesn't terminate the substring...
 
J

jacob navia

Logan said:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;


The variable smaller disappears when the function finishes
You should allocate the space for the characters, then fill
it, then return it
 
L

Logan

Fri, 14 Dec 2007 11:40:51 +0000¿¡, Mark Bluemel ½è½À´Ï´Ù:
Logan said:
Fri, 14 Dec 2007 12:23:45 +0100¿¡, jacob navia ½è½À´Ï´Ù:
Logan wrote:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
Thank you, but since I've changed that line to

char smaller;


Variable Length Arrays are specific to C99 (just in case you didn't
know that).
Now I don't get any output when I am expecting

abc

"smaller" has automatic storage duration, so it's not guaranteed
that it will be available to the calling routine...

The normal approach to this sort of thing is either :-

a) have getSubString() use malloc() to allocate space for the data
it returns. In this case callers have to ensure they free()
the returned pointer at some point.

Yeah, you guys are absolutely correct. When I do

char smaller[1000];

then the code works. Thank you.
 
L

Logan

Fri, 14 Dec 2007 22:18:03 +1100¿¡, Logan ½è½À´Ï´Ù:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


/* Return B from ABC
Don't need to check since this works for me ;)
*/
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
 
L

Logan

Fri, 14 Dec 2007 22:55:28 +1100¿¡, Logan ½è½À´Ï´Ù:
Fri, 14 Dec 2007 22:18:03 +1100¿¡, Logan ½è½À´Ï´Ù:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


/* Return B from ABC
Don't need to check since this works for me ;)
*/
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


I didn't free(smaller); it but who cares...
 
M

Mark Bluemel

Logan said:
Yeah, you guys are absolutely correct. When I do

char smaller[1000];

then the code works. Thank you.


If you mean you are using code like :-

char* getSubstring(char* larger, int a, int b) {
char smaller[1000];
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3))
}

You are still doing Something Bad(tm). Actually at least 2
Somethings.

1) You still don't terminate the substring in getSubstring(), so
if the "smaller" array doesn't get initialised to all '\0'
(and there's no guarantee it will), you'll get a bad string.

2) The "smaller" array has "auto storage duration" which means the
space it occupies is not guaranteed to hold its value (or even
be accessible) once getSubstring() returns...

See what happens when you run this program, for example :-

#include <stdio.h>
char* getSubstring(char* larger, int a, int b) {
char smaller[1000];
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

void muckItUp() {
int i;
char garbage[1000];
for (i=0;i<100;i++) {
garbage = 'z';
}
}

int main() {
char* keepme;
char* l="abcabcabc";
printf("%s\n", getSubstring(l, 3, 3));
printf("%s\n", getSubstring(l, 2, 2));

keepme = getSubstring(l,3,3);
muckItUp();
printf("%s\n",keepme);
}
 
M

Mark Bluemel

Logan said:
Fri, 14 Dec 2007 22:55:28 +1100¿¡, Logan ½è½À´Ï´Ù:
Fri, 14 Dec 2007 22:18:03 +1100¿¡, Logan ½è½À´Ï´Ù:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}

/* Return B from ABC
Don't need to check since this works for me ;)
*/
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


I didn't free(smaller); it but who cares...


You still didn't terminate your substring. And you should care
about that.
 
M

Mark Bluemel

Logan said:
Fri, 14 Dec 2007 22:18:03 +1100¿¡, Logan ½è½À´Ï´Ù:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


/* Return B from ABC
Don't need to check since this works for me ;)
*/
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);


And if malloc() fails?
 
J

JimS

Fri, 14 Dec 2007 12:23:45 +0100¿¡, jacob navia ½è½À´Ï´Ù:
This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
Thank you, but since I've changed that line to

char smaller;

Now I don't get any output when I am expecting

abc

int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


smaller goes out of scope when getSubstring returns. You could make
it static:
static char smaller;
Then you face the problem that smaller isn't nul terminated.
So maybe it should be
char* getSubstring(char* larger, int a, int b) {
static char smaller[b+1];
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
smaller='\0'; /* nul terminate string */
return smaller;
}

Jim
 
C

CBFalconer

Logan said:
.... snip ...

/* Return B from ABC
Don't need to check since this works for me ;)
*/
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


Since it has several faults, it shouldn't necessarily work.
Primarily, you never #include <stdio.h>, so the call to printf has
undetermined results. Then you never 0 terminate the string in
smaller, so even with the #include the results are indeteminate.
Third, you cast the output of malloc, thus hiding the fatal error
of not #including <stdlib.h>.
 
J

Joachim Schmitz

Logan said:
Fri, 14 Dec 2007 22:18:03 +1100¿¡, Logan ½è½À´Ï´Ù:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


/* Return B from ABC
Don't need to check since this works for me ;)
*/

Why do you pots it then?
char* getSubstring(char* larger, int a, int b) {
char* smaller=(char *)malloc(sizeof(char) * b);
drop the cast and use sizeof smaller instead od sizeod(char), it will remind
you to #include <stdlib.h>
char* smaller=malloc(b * sizeof *smaller);
Test whether the malloc succeeded before accessing that memory
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));

don't forget the free() here.May not be important here, but get into the
habit.

Bye, Jojo
 
K

Keith Thompson

Logan said:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
int i;
for(i=0; i<b; i++) {
smaller=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}


You would benefit greatly from reading the comp.lang.c FAQ,
<http://www.c-faq.com/>, particularly section 6.
 
B

Barry Schwarz

Fri, 14 Dec 2007 11:40:51 +0000¿¡, Mark Bluemel ½è½À´Ï´Ù:
Logan said:
Fri, 14 Dec 2007 12:23:45 +0100¿¡, jacob navia ½è½À´Ï´Ù:

Logan wrote:
/* Return B from ABC
I get segementation error when I execute this. Please help.
*/

char* getSubstring(char* larger, int a, int b) {
char* smaller;
This pointer (smaller) points to NOWHERE, i.e.
you did not allocate space for the data this pointer will point to
Thank you, but since I've changed that line to

char smaller;


Variable Length Arrays are specific to C99 (just in case you didn't
know that).
Now I don't get any output when I am expecting

abc

"smaller" has automatic storage duration, so it's not guaranteed
that it will be available to the calling routine...

The normal approach to this sort of thing is either :-

a) have getSubString() use malloc() to allocate space for the data
it returns. In this case callers have to ensure they free()
the returned pointer at some point.

Yeah, you guys are absolutely correct. When I do

char smaller[1000];

then the code works. Thank you.


No it doesn't. It only appears to work. It actually invokes
undefined behavior. smaller ceases to exist when getSubstring
returns. The address that is returned by definition becomes
indeterminate. It no longer points to smaller because smaller no
longer exists. And you still did not terminate the "string" in
smaller so it is not really a string. Even if still existed (such as
if you declared it static), the call to printf would still invoke
undefined behavior. And you are missing an include for printf.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top