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

array of structures

Hello, All!

I suppose it's C-specific issue, so I post here.

The following test code is intended to parse CGI query string, but it makes
'segmentation fault' at 'XXX' label:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* query format */
typedef struct cgi_query_s {
char arg[10];
char value[20];
} cgi_query_t;

int main(void)
{
const char seps[] = "&="; /* separators used in query */
char *token; /* splitted piece of string */

int idx = 0;
char *str, *s;

cgi_query_t *cq = calloc(10, sizeof(cgi_query_t));
strcpy(str, "arg1=qwe&arg2=asd&arg3=zxc&arg4=bnm");

token = strtok(str, seps);
puts("after token");

while( token != NULL ) {
strcpy(cq[idx].arg, token);
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX
token = strtok(NULL, seps);
}

return 0;
}

Where is possible bug?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #1
4 1197
Roman Mashak wrote:
Hello, All!

I suppose it's C-specific issue, so I post here.

The following test code is intended to parse CGI query string, but it
makes 'segmentation fault' at 'XXX' label:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* query format */
typedef struct cgi_query_s {
char arg[10];
char value[20];
} cgi_query_t;

int main(void)
{
const char seps[] = "&="; /* separators used in query */
char *token; /* splitted piece of string */

int idx = 0;
char *str, *s;

cgi_query_t *cq = calloc(10, sizeof(cgi_query_t));
strcpy(str, "arg1=qwe&arg2=asd&arg3=zxc&arg4=bnm");


str doesn't point to sufficient space for this copy to work. In fact, str
doesn't point anywhere. Did you think char * means string? It doesn't.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #2


Roman Mashak wrote:
Hello, All!

I suppose it's C-specific issue, so I post here.

The following test code is intended to parse CGI query string, but it makes
'segmentation fault' at 'XXX' label:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* query format */
typedef struct cgi_query_s {
char arg[10];
char value[20];
} cgi_query_t;

int main(void)
{
const char seps[] = "&="; /* separators used in query */
char *token; /* splitted piece of string */

int idx = 0;
char *str, *s;

cgi_query_t *cq = calloc(10, sizeof(cgi_query_t));
strcpy(str, "arg1=qwe&arg2=asd&arg3=zxc&arg4=bnm"); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^

This is most likely the cause of your problem. You've declared the
pointer (str), but you haven't given it anything to point *to*, so it's
pointing off into some random memory location.

You either need to declare str as a static array:

char str[] = "arg1=...";

or you need to use malloc() to create a buffer and copy the string to
it:

str = malloc(sizeof *str * strlen("arg1=...") + 1;
if (str)
strcpy(str, "arg1=...");
else
/* memory allocation error */

token = strtok(str, seps);
puts("after token");

while( token != NULL ) {
strcpy(cq[idx].arg, token);
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX
token = strtok(NULL, seps);
}

return 0;
}

Where is possible bug?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


Nov 15 '05 #3
Roman Mashak wrote:
Hello, All!

I suppose it's C-specific issue, so I post here.

The following test code is intended to parse CGI query string, but it makes
'segmentation fault' at 'XXX' label:
<snipped>

In addition to the other responses:
token = strtok(NULL, seps);
strcpy(cq[idx++].value, token); // XXX


strtok may return NULL. You must test token
for non-NULL-ness before using it.
Nov 15 '05 #4
Thanks to everyone for replies and hints.
To my mind, strtok() is pretty archaism :) don't you think so?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 15 '05 #5

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

Similar topics

8
by: michi | last post by:
Hello everybody, I have following problem: I have an array of pointers to structures: table* tab = new table; and structure table is like this: struct table{ CSLL::node* chain;
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.