473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Odd SEGFAULT Problem

I'm having a SEGFAULT on the following program. Weird thing is that it happens randomly with different "str"s (ie. user input; check main() for more info) and my debugger shows me that the getToken function is working and returning the correct answer.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. char* getToken(char *src, unsigned int pos=0)
  6. {
  7.     char *token=new char[strlen(src)];
  8.     unsigned int i=0;
  9.     while(!isblank(src[pos]))
  10.     {
  11.         token[i]=src[pos];
  12.         pos++;
  13.         i++;
  14.     }
  15.     token[pos]='\0';
  16.     return token;
  17. }
  18.  
  19. int main(int argc,char* argv[])
  20. {
  21.     puts("Enter command:");
  22.     char *str;
  23.     str=gets(str);
  24.     char *nstr=getToken(str);
  25.     puts(nstr);
  26.     return 0;
  27. }
Note: The getToken(...) function is supposed to function like strtok , returning tokens.
eg.
Call: getToken("This a sample for testing this function"); (Currently works)
Return: "This"

Call: getToken("Another sample",8); (Currently works)
Return: "sample"

Call: getToken("aviraldg aviraldg"); (SEGFAULTs)
Returns: n/a
Expected return: "aviraldg"

Also, please note that this is not homework , and I'm trying to build a simple "some" script(ing language) parser here...
Oct 19 '08 #1
2 1267
JosAH
11,448 Expert 8TB
Your 'str' variable doesn't point to a valid char buffer in your main() function.

kind regards,

Jos
Oct 19 '08 #2
newb16
687 512MB
And ( just in case ) in token function you should allocate strlen(str)+1 bytes to have place for trailing \0 if the string consists of one token.
Oct 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
4
by: Ovid | last post by:
Hi all, I'm having a problem trying to create a 2D array whose dimensions are determined at runtime. Below my signoff is a minimal test case that hopefully demonstrates what I'm trying to do. ...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
6
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While free()ing the 28th node (of total 40), I hit a...
7
by: Parahat Melayev | last post by:
I am programming simple TCP server. Some packets causes server to get segmentation fault. Is it possible to handle segmentation fault and log that packet and make server to continue running? If...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.