473,396 Members | 1,879 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,396 software developers and data experts.

crazy problem with strtok puttings ptrs in **argv

Hi,

I have the following code which is driving me crazy. I compile it on
MacOSX and it keeps crashing upon entering a command in the program
(ran trough gdb)

[command]pwd

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x000105bc in parse_commands (command=0xbffe70e4 "pwd") at
/Users/alefveld/Projects/project1/client.c:198
198 while((argv[i++]=strtok(command,""))) {
(gdb) quit
this is the conflicting piece of code. i suspect it's in the ptr to ptr
because when working with a normal ptr no problem occurs. i just don't
want to make a dozen strtok calls. i would like all the arguments of a
single command to be split up in tokens and put nicely in my **argv. do
i need to initialize anything? is the char *ptr strtok giving me not
correct? do i need to cast it ?

ps *command is a '\0' ended string.

int parse_commands(char *command)
{
int i=0, argc=0;
char **argv={0}; // initialize everything to NULL
connection_index *tmp=head; // not relevant here

/* Split up command in tokens */
while((argv[i++]=strtok(command,""))) {
command=NULL;
argc++;}

/* A command always exists of 2 or more arguments. */
if(argc<=1) return -1;

thanks a lot,
Rgds,
Alef

Nov 15 '05 #1
3 2595


al**@xs4all.nl wrote:
Hi,

I have the following code which is driving me crazy. I compile it on
MacOSX and it keeps crashing upon entering a command in the program
(ran trough gdb)

[command]pwd

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000000
0x000105bc in parse_commands (command=0xbffe70e4 "pwd") at
/Users/alefveld/Projects/project1/client.c:198
198 while((argv[i++]=strtok(command,""))) {
(gdb) quit
this is the conflicting piece of code. i suspect it's in the ptr to ptr
because when working with a normal ptr no problem occurs. i just don't
want to make a dozen strtok calls. i would like all the arguments of a
single command to be split up in tokens and put nicely in my **argv. do
i need to initialize anything? is the char *ptr strtok giving me not
correct? do i need to cast it ?

ps *command is a '\0' ended string.

int parse_commands(char *command)
{
int i=0, argc=0;
char **argv={0}; // initialize everything to NULL
connection_index *tmp=head; // not relevant here

/* Split up command in tokens */
while((argv[i++]=strtok(command,""))) {


There are at least two problems here.

First, argv is NULL. Since argv[0] is equivalent to
*(argv + 0) which is the same as *argv which is the same
as *NULL, you can't expect anything good to happen. You
are trying to store the value returned by strtok(), but
you have not provided any memory to store it in.

Second, using "" as the second argument to strtok()
is legal, but silly: it will "tokenize" the string using
an empty set of delimiter characters -- and since the
string therefore contains no delimiters, strtok() will
just return the entire thing in one lump.

--
Er*********@sun.com

Nov 15 '05 #2
al**@xs4all.nl wrote:
.... snip ...
this is the conflicting piece of code. i suspect it's in the ptr
to ptr because when working with a normal ptr no problem occurs.
i just don't want to make a dozen strtok calls. i would like all
the arguments of a single command to be split up in tokens and put
nicely in my **argv. do i need to initialize anything? is the char
*ptr strtok giving me not correct? do i need to cast it ?
You are allowed to capitalize the first letters of sentences and
the personal pronoun 'I'. This adds to the legibility of your
article.

ps *command is a '\0' ended string.

int parse_commands(char *command)
{
int i=0, argc=0;
char **argv={0}; // initialize everything to NULL
so you declared a single pointer and set it to NULL. It is name
argv for some reason, and points (after suitable initialization
only) to storage that holds a pointer to char.
connection_index *tmp=head; // not relevant here

/* Split up command in tokens */
while((argv[i++]=strtok(command,""))) {
argv is still a NULL, pointing nowhere. This is undefined
behavior. By the way, you are also allowed to embed real blanks in
your code. There are no penalties for legibility.
command=NULL;
argc++;}

/* A command always exists of 2 or more arguments. */
if(argc<=1) return -1;

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 15 '05 #3
al**@xs4all.nl wrote:
char **argv={0}; // initialize everything to NULL
The braces, and the comment, suggest a confusion between pointers and
arrays... you've made argv a pointer, not an array, and this line is
equivalent to

char **argv = NULL;

Set argv to point to valid memory before dereferencing it.
/* Split up command in tokens */
while ((argv[i++] = strtok(command, ""))) {
command = NULL;
argc++;
}


You don't want an empty delimiter string. That makes the entire
command one token. In other words, strtok(s, ""), when s is not NULL,
simply returns s.
Nov 15 '05 #4

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

Similar topics

1
by: Bulba! | last post by:
OK. I have reworked this program (below) to use just the data manipulation capabilities of Python (training was largely the motivation). I've tried to manipulate the data just in Python and not in...
3
by: Leonardo Javier Bel?n | last post by:
I cannot make the following program call the MYSQL C API when called from the function process_token and I dont know why. I checked the WEB and I did found nothing related to this bug. I am using...
6
by: Marco Chiarandini | last post by:
Dear all, I am experiencing a problem in the deallocation of STL data structures. In paritcular I create a vector of sets and insert integers in each set. I do not define any deallocator since...
32
by: Chumbo | last post by:
If I have this(please bear with me - this is a case of a former java guy going back to C ;-) int main () { char *tokconvert(char*); char str = "dot.delimited.str"; char *result; result =...
3
by: magix | last post by:
Dear Guru, I have been thinking hard on how to token based on demiliter after certain position. Example, I have list of possible string below, and the the delimiter is "1" with the rules below...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
8
by: Stu Cazzo | last post by:
Hi all, I have a question on why strtok is doing what it's doing for my splitString( string2 ); call. Below is the output for the entire program: token was: word1 token was: word2 token...
14
by: stevenruiz | last post by:
Hello All My question mainly is how to use/reference Double Pointers? I am currently trying to understand what the meaning of a 'vector of pointers' means also? What I am trying to do is take...
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.