Unless I missed something, all of the following code can be replaced with two snprintf (or sprintf) calls. (It probably has nothing to do with your segfault, but needless complexity never helps.)
Quote:
Originally Posted by shoaibjameel123
Regards,
Alister
---------- Post updated at 12:26 AM ---------- Previous update was at 12:10 AM ----------
I haven't tried to compile your code, just looking it over, but the following is wrong:
str doesn't even have enough room for ".dat", which requires 5 bytes.
This is what I did. Put this piece of code
Like this:
And BINGO!!! it worked
Also, thanks Alister, yes that was my mistake. I have increased the size of str. But there now I have encountered another problem.
When the following section comes:
The program now faults again.
This happens when I go to the next line after
in gdb
---------- Post updated at 12:42 PM ---------- Previous update was at 12:39 PM ----------
Now the problem does not occur with
but with
---------- Post updated at 02:43 PM ---------- Previous update was at 12:42 PM ----------
Finally the problem has been solved and the program is executing perfectly. I have made one change in the program.
Take out all malloc() calls inside this for loop and initialize them outside the loop.
After placing the following statements outside, the code runs well.
But now I have to pre-define the value of
. SO, I used #define preprocessor declaration and hard coded this value.
I am not sure why allocation of memory inside the for loop did not work.
That loop is leaking memory badly. Each time store_values and words_from_webpage are assigned a pointer, the only references to the un-free()'d memory allocated during the previous iteration are lost.
Perhaps there are other problems, but I just focused on what you highlighted in your most recent post.
Why are you using C for this? A learning exercise? If so, great. If not, consider using awk, perl, python, or something else that operates at a higher level. You're not gaining much by using C for this and you are wasting a lot of time with the memory management bugs. If it must be C, you'll probably benefit from using static analysis tools (valgrind, for example).
Also, consider that there's no reason (other than educational) to bother with itoa(), reverse(), and the string generating functions that they are supporting. snprintf() will do everything that they are doing.
Yes, in the loop things were not perfect and memory leak was excessive. I've fixed some of the other problems too, but those are minor.
There are some reasons why I use C for everything. One is that I work on extremely large sets of text files and do mathematical computations using those files. Hence, efficiency is one reason. But indeed I do have to toil extra hard to get my code perfect.
snprintf() is a valid suggestion and can get my code a lot more compact and readable. Thanks for that. I'll use it in my future codes.
One big reason why I stick to C is "passion". Just like some people love climbing Mount Everest but others think that they are wasting their precious time and life. (The last sentence is just a joke ).
Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1...
I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one.
Below are two very condensed snippets of which I have added the results inside the each code section.
IMPORTANT!... (11 Replies)
Hi everyone
Need to get version of npm application that have several output like this:
root: nmp -version
10
root: nmp -version
10
root: nmp-new -version
3.1
root: nmp-old -version
Segmentation fault
count them , after that write to the file like this:
10 2
3.1 1 (1 Reply)
Hi Guys,
I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error:
"Segmentation fault"
Can someone please help me resolve this?
Thanks alot (2 Replies)
I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this :
i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek:
#include </usr/include/mysql++/mysql++.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int outputToImport(const char*... (1 Reply)
Hi,
I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Hi ,
During execution a backup binary i get following error
"Program error 11 (Segmentation fault), saving core file in '/usr/datatools"
Riyaz (2 Replies)
I have written a program which takes a directory as command line arguments and displays all the dir and files in it.
I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
sometimes for this code i get a segmentation fault for codes llike this
:
int main{
int * a= 0;
int b;
a = (int*)malloc(sizeof(int));
///some code using these variable but no freeing of a
if(a){
free(a);
a = 0;
}
return... (3 Replies)