473,395 Members | 1,584 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.

vc.user_comments[i] = string; /*crashing*/

Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

'read_vorbis_string' returns a pointer to a calloced string. This function
works for sure.
But the problem is, that 'vc.user_comments[i] = string;' is not the right way
to assing the address of the 'string' to the pointer array 'vc.user_comments'.
What is?
Nov 14 '05 #1
11 1878
"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:xV**************@read3.inet.fi...
Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

'read_vorbis_string' returns a pointer to a calloced string. This function
works for sure.
But the problem is, that 'vc.user_comments[i] = string;' is not the right way to assing the address of the 'string' to the pointer array 'vc.user_comments'. What is?


You've left out the INSTANTIATION of vorbis_comment. Please post this
section, where you declare and initialize vc.

karl m
Nov 14 '05 #2
karl malbrain wrote:
"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:xV**************@read3.inet.fi...
Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

'read_vorbis_string' returns a pointer to a calloced string. This function
works for sure.
But the problem is, that 'vc.user_comments[i] = string;' is not the right


way
to assing the address of the 'string' to the pointer array


'vc.user_comments'.
What is?

You've left out the INSTANTIATION of vorbis_comment. Please post this
section, where you declare and initialize vc.

karl m


Here is your "INSTANTIATION":

vorbis_comment read_comments
( const char *scname)
{
/* After second 'vorbis' string comes the width (int) of VENDOR string.
* Then comes the VENDOR string itself.
* After VENDOR string comes the number (int) of comments.
* After the number of comments comes the width (int) of first user comment.
* After the first user comment comes the width (int) of the second user comment.
*/
register int i;

vorbis_comment vc;
FILE *sc;
char *ident = "vorbis";

sc = fopen (scname, "rb");
assert (sc != NULL);

assert (!wind_till (ident, sc));
assert (!wind_till (ident, sc));

vc.vendor = read_vorbis_string (sc);
assert (vc.vendor != NULL);

assert (fread (&vc.comments, sizeof (int), 1, sc) == 1);

printf ("%d\n", vc.comments);

vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
assert (vc.comment_wds != NULL);

for (i = 0 ; i < vc.comments - 1 ; i++) {
vc.user_comments[i] = read_vorbis_string (sc);

assert (vc.user_comments[i] != NULL);
printf ("__%s__", vc.user_comments[i]);

vc.comment_wds[i] = strlen (vc.user_comments[i]);
}

vc.comment_wds[vc.comments + 1] = 0;
return vc;
}
/* End Of File */
Nov 14 '05 #3
Problem solved.

I hadn't calloced memory for the array of pointers. I was only callocing memory
for the strings which the array of pointers should be pointing to.

/* ... */

assert (fread (&vc.comments, sizeof (int), 1, sc) == 1);

printf ("%d\n", vc.comments);

/* ADDED */
vc.user_comments = (char *) calloc (vc.comments + 1, sizeof (char *));
assert (vc.user_comments != NULL);
/* End of ADDED */

vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
assert (vc.comment_wds != NULL);
/* ... */
Nov 14 '05 #4
Tatu Portin wrote:

Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

'read_vorbis_string' returns a pointer to a calloced string. This
function works for sure.

But the problem is, that 'vc.user_comments[i] = string;' is not
the right way to assing the address of the 'string' to the pointer
array 'vc.user_comments'. What is?


You haven't shown a complete compilable program, which forces
anyone replying to make assumptions (or to ignore you). There is
no declaration of vc. Assuming one exists in scope, of the form:

vorbis_comment vc;

vc.user_comments is of type char **, i.e a pointer to a pointer to
a char. It is uninitialized, and any use of it will cause
undefined behaviour. Don't ask me what you should initialize it
to, since you didn't bother making your program and specifications
complete. I suspect you want a linked list rather than an array.

--
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 14 '05 #5
CBFalconer wrote:
Tatu Portin wrote:
Have this kind of struct:

typedef struct {
char **user_comments;
/* ... */
} vorbis_comment;

/* prototype */
char * read_vorbis_string
( FILE *sc);

/* problem */
vc.user_comments[i] = read_vorbis_string (sc);

'read_vorbis_string' returns a pointer to a calloced string. This
function works for sure.

But the problem is, that 'vc.user_comments[i] = string;' is not
the right way to assing the address of the 'string' to the pointer
array 'vc.user_comments'. What is?

You haven't shown a complete compilable program, which forces
anyone replying to make assumptions (or to ignore you). There is
no declaration of vc. Assuming one exists in scope, of the form:

vorbis_comment vc;

vc.user_comments is of type char **, i.e a pointer to a pointer to
a char. It is uninitialized, and any use of it will cause
undefined behaviour. Don't ask me what you should initialize it
to, since you didn't bother making your program and specifications
complete. I suspect you want a linked list rather than an array.


Here is the complete working program. (compiled -Wall -pedantic)

/* Start Of File */

/* Public Domain */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

/* CDmaker
* Purpose is to correctly rip tags from Ogg Vorbis files for
* reproduction in printable form for CD distribution.
*/

typedef struct {
char **user_comments;
int *comment_wds;
int comments;
char *vendor;
} vorbis_comment;

/* 'read_comments' is a hack.
* It does not work by the specification.*/
char * read_vorbis_string
( FILE *sc);

int wind_till
( const char *ident
, FILE *sc);

vorbis_comment read_comments
( const char *scname);

int print_comments
( const vorbis_comment vc);

int main
( int argc
, char *argv[])
{
vorbis_comment vc;

vc = read_comments (argv[1]);

print_comments (vc);

return 0;
}

char * read_vorbis_string
( FILE *sc)
{
register int i;
int wd;
char *str;
char ch;

if (fread (&wd, sizeof (int), 1, sc) != 1)
return NULL;

str = (char *) calloc (wd + 1, sizeof (int));
if (str == NULL)
return NULL;

for (i = 0 ; i < wd ; i++) {
ch = fgetc (sc);
if (ch == EOF)
break;
str[i] = ch;
}
str[wd + 1] = '\0';

return str;
}
int wind_till
( const char *ident
, FILE *sc)
{
register int i;
int ch = '\0';

if (ident[0] == '\0')
return 0;

while (1) {
for (i = 0 ; ident[i] == ch ; i++) {
if (ident[i + 1] == '\0')
return 0;

ch = fgetc (sc);
if (ch == EOF)
return 2;

}

ch = fgetc (sc);
if (ch == EOF)
return 1;
}

return 3;
}

vorbis_comment read_comments
( const char *scname)
{
/* After second 'vorbis' string comes the width (int) of VENDOR string.
* Then comes the VENDOR string itself.
* After VENDOR string comes the number (int) of comments.
* After the number of comments comes the width (int) of first user comment.
* After the first user comment comes the width (int) of the second user comment.
*/
register int i;

vorbis_comment vc;
FILE *sc;
char *ident = "vorbis";

sc = fopen (scname, "rb");
assert (sc != NULL);

assert (!wind_till (ident, sc));
assert (!wind_till (ident, sc));

vc.vendor = read_vorbis_string (sc);
assert (vc.vendor != NULL);

assert (fread (&vc.comments, sizeof (int), 1, sc) == 1);

/* Allocate memory for pointers */
vc.user_comments = (char **) calloc (vc.comments + 1, sizeof (char *));
assert (vc.user_comments != NULL);

/* Allocate memory for comment lenghts */
vc.comment_wds = (int *) calloc (vc.comments + 1, sizeof (int));
assert (vc.comment_wds != NULL);

for (i = 0 ; i < vc.comments ; i++) {
vc.user_comments[i] = read_vorbis_string (sc);
assert (vc.user_comments[i] != NULL);

vc.comment_wds[i] = strlen (vc.user_comments[i]);
}

vc.comment_wds[vc.comments + 1] = 0;
return vc;
}

int print_comments
( vorbis_comment vc)
{
register int i;

printf ("%s, %d comments\n", vc.vendor, vc.comments);

for (i = 0 ; i < vc.comments ; i++) {
printf ("%d: \t%s\n"
, vc.comment_wds[i]
, vc.user_comments[i]
);
}

return i;
}
/* End Of File */
Nov 14 '05 #6
We have to use arrays, plus we haven't learned pointer yet.. but thanx!

Nov 14 '05 #7
Wow guys...... this is CMSC 100... so lost.... :-\ no pointers just
arrays and i'm only 19 i dont get all that!!

Nov 14 '05 #8
what's vorbis? Here's my entire program:
/*
*/

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

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

int main ( void )
{
system ("clear");
int i;
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May",
"June", "July", "August", "September",
"October",
"November", "December"};

for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
}
storeSales (months, sales);
printResults (months, sales);
return 0;

}
void storeSales ( char months[][15], double sales[] )
{
int i = 0;
for ( i = 0; i < SIZE; i++ )
{

do
{
printf("Enter the store's sales for the month of %s:",
months[ i ]);
scanf( "9.2%lf", &sales [ i ]);
printf("\n");
}
while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
}
}
void printResults ( char months[][15], double sales[] )
{
int i;
double lowest = 0.0, highest = 0.0, average = 0.0;

for (i = 0 ; i < SIZE ; i++)
average = average + (sales[i] / SIZE);
printf ("Average of the store's sales: %lf\n", average);
lowest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (lowest > sales[i]) {
lowest = sales[i];
}
}
printf ("Lowest sales month: %lf\n", lowest);
highest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (highest < sales[i]) {
highest = sales[i];
}
}
printf ("Highest sales month: %lf\n", highest);
return;
}

Nov 14 '05 #9
/*
*/

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

#define SIZE 12

void printResults (char months[][15], double sales[]);
void storeSales (char months[][15], double sales[]);

int main ( void )
{
system ("clear");
double sales[ SIZE ];
char months [SIZE][15] = {"January", "February", "March", "April",
"May",
"June", "July", "August", "September", "October",
"November", "December"};
storeSales (months, sales);
printResults (months, sales);
return 0;

}
void storeSales ( char months[][15], double sales[] )
{
int i = 0;
for ( i = 0; i < SIZE; i++ )
{

do
{
printf("Enter the store's sales for the month of %s:", months[ i ]);
scanf( "9.2%lf", &sales [ i ]);
printf("\n");
}
while ((sales[ i ] < 0.00 ) || ( sales[ i ] > 100000.00));
}
}
void printResults ( char months[][15], double sales[] )
{
int i;
double lowest = 0.0, highest = 0.0, average = 0.0;

for (i = 0; i < SIZE; i++)
{
printf("%-9s: %9.2f\n", months[ i ], sales[ i ]);
}

for (i = 0 ; i < SIZE ; i++)
average = average + (sales[i] / SIZE);
printf ("Average of the store's sales: %lf\n", average);
lowest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (lowest > sales[i]) {
lowest = sales[i];
}
}
printf ("Lowest sales month: %lf\n", lowest);
highest = sales[0];
for (i = 0 ; i < SIZE ; i++) {
if (highest < sales[i]) {
highest = sales[i];
}
}
printf ("Highest sales month: %lf\n", highest);
return;
}

I see what you mean, I did it.. but after compilation and entering
sales for January it's stuck in the loop??? it says "Enter sales for
January" until I press ctrl C!

Nov 14 '05 #10
On 6 Dec 2004 14:57:21 -0800, in comp.lang.c , "honeygrl33"
<gi*************@aol.com> wrote:
/*
*/


<snip code>

When posting code, you need to post some sort of question too. Why did you
post this? Whats your problem? What do you want people to help with?

Do NOT rely on people having access to posts you made several days ago.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #11
Mark McIntyre wrote:
"honeygrl33" <gi*************@aol.com> wrote:
/*
*/


<snip code>

When posting code, you need to post some sort of question too. Why
did you post this? Whats your problem? What do you want people to
help with?

Do NOT rely on people having access to posts you made several days
ago.


In fact don't rely on access to ANY other posts. Many get lost, or
delayed for days and weeks. Usenet is a 'no guarantees'
mechanism. Therefore snip quotations down to the portion germane
to your answer, and answer after the quoted portion.

--
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 14 '05 #12

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

Similar topics

4
by: Yannick Majoros | last post by:
Hello, PHP is crashing (segfault) due to some bug. See http://bugs.php.net/bug.php?id=32929 for details, but here is the code in which it occurs: while...
12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
0
by: Henry Hank | last post by:
Environment: I'm setting up a database server on a Dell Poweredge 2650, dual 1.8GHZ pentium with 1GB of memory and RAID5 drives. I've installed RedHat 9, and updated the kernel to 2.4.20-19.9smp....
5
by: Eddie | last post by:
I have a MySQL-server running Innodb. We have installed ~ 2GB of memory in the server. In spite of this MySQL keeps crashing due to out-of-memory errors. The server is a dual xeon i686 running...
3
by: Shawn August | last post by:
Hello: I am converting a working VB6 program to C#. During testing of the C# version, I noticed the ReadFile API is crashing. The parameters going into the this function are identical to the...
5
by: news.cyberlink.ch | last post by:
Hi, We have a DB2 version 7 fixpack 14 installed on Windows 200 at a customer site that is crashing at regular intervals.Everything was working fine for over many years, now since about one...
3
by: Brad | last post by:
In a Vista/IIS7 asp.net app, a coded crystal report export is crashing IIS7....but it works just fine in visual studio's cassini web server. And if I create a web form and use the crystal...
7
by: David | last post by:
Hi, using C# asp.net 1.1 I am having a problem. If I use the code below, but have null passed as the PostingGuid, then by the time I get to fill the dataset, I have "an object is not set to...
2
by: =?Utf-8?B?QW5uZXh4eHh4eHg=?= | last post by:
My computer keeps crashing. The files created when it does this are: WERdc15.dir00\Mini090708-03.dmp WERdc15.dir00\sysdata.xml has anybody any idea of what these files are and the solution to...
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: 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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.