There is a bug in the code. You are fortunate that the UNIX platform at least shows you this; in Windows it is just "lucky" to be running.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
How is that strange? On Windows you didn't touch memory that would cause a critical error, on Unix you did. It means your code is broken. Look for uninitialized pointers and out of bound array accesses. Those are the prime culprits for a seg fault.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>if there is a bug in the code, why doesn't it show when I compile?
*stunned silence*
>how can i rectify the problem?
Take my suggestions and look for off by one errors when you index an array or places you might have an uninitialized pointer.
>I thought segmentation fault has got to do with memory errors...
Yes. If you access memory outside of your address space, you get a segmentation fault.
>or is this a portability issue?
No, it's a programmer issue. Fix your broken code.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
It depends on what you mean by "run out of memory". If malloc fails then it returns a null pointer, which would cause a seg fault if you don't test for failure. However, malloc failure is extremely unlikely these days, so the problem is still probably something you did wrong. If running out of "stack" space for automatic variables caused a segmentation fault, I would still be suspicious of your code because Unix is very good about growing the "stack" if needed. So if you're actually running out of memory (meaning fast memory and virtual memory), there's a lot more wrong with your code than a little error.
Unfortunately, memory errors are very difficult to find, and without actual code to look at, we're just making educated guesses.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Hello,
Please post your code. Let's take a look at it. Please use CODE tags when you do it, so that it is nice and clean to read.
Christian
kc0arf
Posting Virtuoso
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
Because you declare acName as acharacter pointer. That is, you cannot change individual letters. All you can do is have it point to another string. So you could write
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *acName="hello daniweb";
*acName="H";
printf("%s\n",acName);
}
and the output would be H .
In order to capitalize the first letter, you have to declare acName as acharacter array. You do this by declaring it as an array of characters, but a null character must be included in the characters count. That is, "hello daniweb" has 13 characters. You add the null character and declare an array of 14 characters. The version below does what you want.
#include<stdio.h>
#include<stdlib.h>
void main()
{
char acName[14] = "hello daniweb";
*acName='H';
printf("%s\n",acName);
}
creeps
Junior Poster in Training
82 posts since Jul 2010
Reputation Points: 85
Solved Threads: 8
One more thing , this is a very old thread. Please put any more questions you have in a new thread.
I actually think this is an awesome example of using the search feature :P
creeps
Junior Poster in Training
82 posts since Jul 2010
Reputation Points: 85
Solved Threads: 8