473,503 Members | 10,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation error for array operation.

/* 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[i]=larger[a+i];
}
return smaller;
}

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

Dec 14 '07 #1
15 1395
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
int i;
for(i=0; i<b; i++) {
smaller[i]=larger[a+i];
}
return smaller;
}

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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Dec 14 '07 #2
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[b];

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

abc

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

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
Dec 14 '07 #3
Logan wrote:
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[b];
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...
Dec 14 '07 #4
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;
int i;
for(i=0; i<b; i++) {
smaller[i]=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

}

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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Dec 14 '07 #5
Fri, 14 Dec 2007 11:40:51 +0000¿¡, Mark Bluemel ½è½À´Ï´Ù:
Logan wrote:
>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[b];

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.
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...
Dec 14 '07 #6
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[i]=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[i]=larger[a+i];
}
return smaller;
}

int main() {
char* l="abcabcabc";
printf("%s", getSubstring(l, 3, 3));
}
Dec 14 '07 #7
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[i]=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[i]=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...
Dec 14 '07 #8
Logan wrote:
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[i]=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[i]=larger[a+i];
}
return smaller;
}

void muckItUp() {
int i;
char garbage[1000];
for (i=0;i<100;i++) {
garbage[i] = '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);
}
Dec 14 '07 #9
Logan wrote:
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[i]=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[i]=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.
Dec 14 '07 #10
Logan wrote:
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[i]=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?
Dec 14 '07 #11
On Fri, 14 Dec 2007 22:33:46 +1100, Logan
<Lo*********@student.uts.edu.auwrote:
>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[b];

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

abc

>>
>> int i;
for(i=0; i<b; i++) {
smaller[i]=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[b];
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[i]=larger[a+i];
}
smaller[i]='\0'; /* nul terminate string */
return smaller;
}

Jim
Dec 14 '07 #12
"Logan" <Lo*********@student.uts.edu.auschrieb im Newsbeitrag
news:pa****************************@student.uts.ed u.au...
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[i]=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[i]=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
Dec 14 '07 #13
Logan wrote:
>
.... 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[i]=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>.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '07 #14
Logan <Lo*********@student.uts.edu.auwrites:
/* 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[i]=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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 15 '07 #15
On Fri, 14 Dec 2007 22:51:54 +1100, Logan
<Lo*********@student.uts.edu.auwrote:
>Fri, 14 Dec 2007 11:40:51 +0000¿¡, Mark Bluemel ½è½À´Ï´Ù:
>Logan wrote:
>>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[b];

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
Dec 18 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4453
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
18
26058
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
19
2460
by: Sameer | last post by:
Hi friends, I am using Mandriva Linux 9.2 and gcc. My source code is, int chunkin ; //no error int i ; for (i=0;i<7225;i++) { chunkin = somedata ;
0
23441
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub)...
2
2146
by: tikcireviva | last post by:
Hi Guys, I am not sure what's wrong with my program. When I ran it under the GDB, it shows the following messages , which said that the issue is from stl_list.h. Does anyone know what's...
4
1933
by: John Doe | last post by:
segmentation error !!!! hi guys , i wrote this program to multiply two matrices (just the basic code without checkin 4 the condition n==p ) " #include<stdio.h> main() { int a,b,c; int...
157
5087
by: Nomad.C | last post by:
Hi I'm coding this simple program, but I get a segmentation fault, I'm pretty sure that the arrays are big enough and there isn't really any buffer overflows so why is this happening? Program...
8
14623
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
10
12462
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.