"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by <p.j.morti.. » Thu, 02 Nov 2000 05:54:49



The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
in an integer(a1-a4). when i compile it i get no error messages, but when i
try to run it, it returnes: Segmentation fault( core dumped ).
Could anyone tell me what im doing wrong.

M. Mortimer.
-----------------------------------------------------------
#include <string.h>

int main(void)
{
  char *ipaddr, *del;
  int a1, a2, a3, a4;

  ipaddr="10.0.0.1";
  del=strtok(NULL, ".");
  a1=atoi(del);
  del=strtok(NULL, ".");
  a2=atoi(del);
  del=strtok(NULL, ".");
  a3=atoi(del);
  del=strtok(NULL, ".");
  a4=atoi(del);

  return 0;

Quote:}

-----------------------------------------------------------
 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Barry Margoli » Thu, 02 Nov 2000 06:06:40



>The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
>in an integer(a1-a4). when i compile it i get no error messages, but when i
>try to run it, it returnes: Segmentation fault( core dumped ).
>Could anyone tell me what im doing wrong.

You're modifying a literal string; some compilers put literals in read-only
sections of memory, precisely to catch mistakes like this.  Use:

char ipaddr[] = "10.0.0.1";

instead of (what is equivalent to):

char *ipaddr = "10.0.0.1";

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by John Gord » Thu, 02 Nov 2000 06:06:12



> The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
> in an integer(a1-a4). when i compile it i get no error messages, but when i
> try to run it, it returnes: Segmentation fault( core dumped ).
> Could anyone tell me what i'm doing wrong.
> M. Mortimer.
> -----------------------------------------------------------
> #include <string.h>
> int main(void)
> {
>   char *ipaddr, *del;
>   int a1, a2, a3, a4;
>   ipaddr="10.0.0.1";
>   del=strtok(NULL, ".");

the first call to strtok() must specify the string you're working on,
not NULL.

Quote:>   a1=atoi(del);
>   del=strtok(NULL, ".");
>   a2=atoi(del);
>   del=strtok(NULL, ".");
>   a3=atoi(del);
>   del=strtok(NULL, ".");
>   a4=atoi(del);
>   return 0;
> }
> -----------------------------------------------------------

---
"... What with you being his parents and all, I think that you could
be trusted not to shaft him."  -- Robert Chang, rec.games.board


 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Arthur H. Gol » Thu, 02 Nov 2000 06:00:18



> The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
> in an integer(a1-a4). when i compile it i get no error messages, but when i
> try to run it, it returnes: Segmentation fault( core dumped ).
> Could anyone tell me what im doing wrong.

> M. Mortimer.
> -----------------------------------------------------------
> #include <string.h>

> int main(void)
> {
>   char *ipaddr, *del;
>   int a1, a2, a3, a4;

>   ipaddr="10.0.0.1";
>   del=strtok(NULL, ".");

You're passing strtok a NULL pointer...what do you expect
this to mean?
More important though (quoting from the man page):

BUGS
       Never use this function.  This function modifies its
first
       argument.   The  identity  of  the delimiting
character is
       lost.  This function cannot be used on constant
strings.

Quote:>   a1=atoi(del);
>   del=strtok(NULL, ".");
>   a2=atoi(del);
>   del=strtok(NULL, ".");
>   a3=atoi(del);
>   del=strtok(NULL, ".");
>   a4=atoi(del);

>   return 0;
> }
> -----------------------------------------------------------

Please rethink (and repost if you still have problems).

HTH,
--ag

--
Artie Gold, Austin, TX  (finger the cs.utexas.edu account
for more info)

--
"I'd sooner fly another combat mission than ride the Cyclone
again" -- Joseph Heller

 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Marc Tardi » Thu, 02 Nov 2000 06:10:59


Two problems:
1. no allocated memory to ipaddr
2. use ipaddr in the first call to strtok

So the first few lines should look like:
  char ipaddr[16], *del; /* expct an IP to be (3*4)+3+'\0' *
                          * characters at most             */
  int a1, a2, a3, a4;

   strcpy(ipaddr, "10.0.0.1");
   del=strtok(ipaddr, ".");
   ...


Quote:> The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
> in an integer(a1-a4). when i compile it i get no error messages, but when i
> try to run it, it returnes: Segmentation fault( core dumped ).
> Could anyone tell me what im doing wrong.

> M. Mortimer.
> -----------------------------------------------------------
> #include <string.h>

> int main(void)
> {
>   char *ipaddr, *del;
>   int a1, a2, a3, a4;

>   ipaddr="10.0.0.1";
>   del=strtok(NULL, ".");
>   a1=atoi(del);
>   del=strtok(NULL, ".");
>   a2=atoi(del);
>   del=strtok(NULL, ".");
>   a3=atoi(del);
>   del=strtok(NULL, ".");
>   a4=atoi(del);

>   return 0;
> }
> -----------------------------------------------------------

 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Barry Margoli » Thu, 02 Nov 2000 06:46:38




Quote:>Two problems:
>1. no allocated memory to ipaddr

Actually, that's not his problem.  The ipaddr="10.0.0.1" initializes ipaddr
to point to allocated memory.  However, it's a string constant, so it can't
be used in a call to strtok().  So your suggested fix is appropriate (but I
like the fix in my post better, because it avoids hard-coding the string
size).

Quote:>2. use ipaddr in the first call to strtok

>So the first few lines should look like:
>  char ipaddr[16], *del; /* expct an IP to be (3*4)+3+'\0' *
>                          * characters at most             */
>  int a1, a2, a3, a4;

>   strcpy(ipaddr, "10.0.0.1");
>   del=strtok(ipaddr, ".");
>   ...

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by <p.j.morti.. » Thu, 02 Nov 2000 07:21:06


I tried to do a:

char ipaddr[]="10.0.0.1";

but then it gave this error message at compile time:

array size missing in ipaddr

Barry Margolin heeft geschreven in bericht ...



>>The following program (should) cut(s) ipaddr in 4 pieces and puts the
digits
>>in an integer(a1-a4). when i compile it i get no error messages, but when
i
>>try to run it, it returnes: Segmentation fault( core dumped ).
>>Could anyone tell me what im doing wrong.

>You're modifying a literal string; some compilers put literals in read-only
>sections of memory, precisely to catch mistakes like this.  Use:

>char ipaddr[] = "10.0.0.1";

>instead of (what is equivalent to):

>char *ipaddr = "10.0.0.1";

>--

>Genuity, Burlington, MA
>*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
>Please DON'T copy followups to me -- I'll assume it wasn't posted to the

group.
 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Barry Margoli » Thu, 02 Nov 2000 07:30:28



>I tried to do a:

>char ipaddr[]="10.0.0.1";

>but then it gave this error message at compile time:

>array size missing in ipaddr

Then I think you have a buggy compiler.  It's supposed to determine the
array length automatically based on the size of the initializer.  This has
long been allowed in both C and C++.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by mike burrel » Thu, 02 Nov 2000 08:32:52



> The following program (should) cut(s) ipaddr in 4 pieces and puts the digits
> in an integer(a1-a4). when i compile it i get no error messages, but when i
> try to run it, it returnes: Segmentation fault( core dumped ).
> Could anyone tell me what im doing wrong.
>   char *ipaddr, *del;
>   int a1, a2, a3, a4;
>   ipaddr="10.0.0.1";
>   del=strtok(NULL, ".");

the first invocation of strtok() has to have a pointer to modifiable char as
its first argument, not the null pointer.  *after* the first invocation, you
should pass it the null pointer (and it will continuen to use the same
string).

thus, this should read:
        del = strtok(ipaddr, ".");
(and then further invocations should use NULL).        

IN ADDITION, strtok()'s string argument must be *modifiable*.  you have
assigned a string literal to ipaddr; a string literal is not modifiable.
since you're using Unix, you should have the strdup() function at your
disposal.  thus, your code should look like:

        ipaddr=strdup("10.0.0.1");
        if (! ipaddr) { /* error */ }
        del=strtok(ipaddr, ".");
        a1=atoi(del);   /* note: atoi() has been obsolesced by strtol() */
        del=strtok(NULL, ".");
        /* ... etc ... */

assuming that you want to use strtok() (be sure you know the consequences of
using strtok() in your code).

--
 /"\                                                 m i k e   b u r r e l l

  X        AGAINST HTML MAIL,

 
 
 

"Segmentation fault( core dumped ) "<--- sentence is driving me mad!!!!!

Post by Blane Brambl » Sat, 04 Nov 2000 01:01:28



> I tried to do a:

> char ipaddr[]="10.0.0.1";

if you know the format of ipaddr is going to be correct, you can use:

sscanf(ipaddr, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);

to get the numbers from the string. Check the man page for sscanf for
other details. Don't forget to check the return value (which should be 4
in this case).

Blane.

 
 
 

1. Get "Segmentation fault (core dumped)" but no core file found

On our Solaris, when a program has a segmentation fault, it crashes and
prints out the message "Segmentation fault (core dumped)" but the core file
is somehow not written to the disk and cannot be found afterwards. Does
anybody has similar experience? How can I fix this problem?

--
-------------------------------------------------------------------------------

2. measuring time

3. Help on "Segmentation fault (core dumped)"

4. sound for video in linux

5. HELP! RPM: "Segmentation fault (core dumped)"

6. Q: Setup of screen-virtual terminal program

7. Several new programs, when run, crash with "Segmentation fault (core dumped)" ???

8. e450 Disk Performance

9. "Segmentation fault: core dumped" message, redirection

10. ps command failed and produced "Bus error" or "Segmentation fault" messages

11. "Bus error" or "Segmentation Fault" By ps command

12. No "core" on segmentation fault

13. difference between "core dump" and "panic"