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

anything wrong with this?

atv
Hi,
is there something wrong with this code? It seems that it crashes
at the else { clause, when allocating a second record. Excuse for
the indentation, the g_warnings are from gtk.

Also, i'm using 2 pointers in the struct, which consists of 3 ints,
and 2 pointers, which i give 256 bytes each. Much to much ofcourse,
but it's just testcode.

The code doesn't even reach the g_warning in the { code.
Any help most appreciated.
atv

if(tail==NULL) {
g_warning("First record in schedule allocated");
tail=(schedule *)malloc(256);
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;
head=tail;
} else {
g_warning("New record in schedule allocated");
/* First check if a day is already booked */
while(tmp!=NULL) {
g_warning("Checking for double appointment");
if(!strcmp(tmp->timestamp,asctime(tstruct))) {
gtk_widget_hide(window1);
return -1;}
tmp=tmp->next;
}
tail->next=(schedule *)malloc(256);
tail=tail->next;
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;

Nov 14 '05 #1
2 2148
atv <at********@xs4all.nl> wrote in news:3fdb7aee$0$207
$e*******@news.xs4all.nl:
Hi,
is there something wrong with this code? It seems that it crashes
at the else { clause, when allocating a second record. Excuse for
the indentation, the g_warnings are from gtk.
Well, you can't expect much if we can't compile your code.
if(tail==NULL) {
g_warning("First record in schedule allocated");
tail=(schedule *)malloc(256);
No need to cast malloc's return value. What is tail? Why are you allocating
256 chars when you need to allocate sizeof(*tail) chars? How do you know
the allocation succeeded?
tail->string=malloc(256);
tail->timestamp=malloc(256);
How do you know the allocation succeeded?
strcpy(tail->string,string);


Do you know if string points to something that is less than 256 chars long?

Please try to provide a short example which we can compile and still
exhibits the problem.
--
A. Sinan Unur
as**@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uc*@ftc.gov
Nov 14 '05 #2
atv wrote:
Hi,
is there something wrong with this code?
Yes.
It seems that it crashes
at the else { clause, when allocating a second record.
If there were nothing wrong with the code, it wouldn't crash, would it?
Excuse for
the indentation, the g_warnings are from gtk.

Also, i'm using 2 pointers in the struct, which consists of 3 ints,
and 2 pointers, which i give 256 bytes each. Much to much ofcourse,
but it's just testcode.

The code doesn't even reach the g_warning in the { code.
Any help most appreciated.
atv

Well, the first thing to do is see what the compiler has to say...
if(tail==NULL) {
g_warning("First record in schedule allocated");
tail=(schedule *)malloc(256);
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;
head=tail;
} else {
g_warning("New record in schedule allocated");
/* First check if a day is already booked */
while(tmp!=NULL) {
g_warning("Checking for double appointment");
if(!strcmp(tmp->timestamp,asctime(tstruct))) {
gtk_widget_hide(window1);
return -1;}
tmp=tmp->next;
}
tail->next=(schedule *)malloc(256);
tail=tail->next;
tail->string=malloc(256);
tail->timestamp=malloc(256);
strcpy(tail->string,string);
strcpy(tail->timestamp,asctime(tstruct));
tail->year=tstruct->tm_year;
tail->month=tstruct->tm_mon;
tail->day=tstruct->tm_mday;
tail->next=NULL;


foo.c:1: parse error before `if'
foo.c:3: warning: type defaults to `int' in declaration of `tail'
foo.c:3: `schedule' undeclared here (not in a function)
foo.c:3: parse error before `)'
foo.c:6: parse error before `->'
foo.c:6: warning: type defaults to `int' in declaration of `strcpy'
foo.c:6: ANSI C forbids data definition with no type or storage class
foo.c:7: parse error before `->'
foo.c:12: warning: type defaults to `int' in declaration of `head'
foo.c:12: initializer element is not constant
foo.c:12: ANSI C forbids data definition with no type or storage class
foo.c:13: parse error before `}'
foo.c:21: warning: type defaults to `int' in declaration of `tmp'
foo.c:21: invalid type argument of `->'
foo.c:21: ANSI C forbids data definition with no type or storage class
foo.c:22: parse error before `}'
foo.c:24: warning: type defaults to `int' in declaration of `tail'
foo.c:24: redefinition of `tail'
foo.c:3: `tail' previously defined here
foo.c:24: invalid type argument of `->'
foo.c:24: ANSI C forbids data definition with no type or storage class
foo.c:25: parse error before `->'
foo.c:27: parse error before `->'
foo.c:27: warning: type defaults to `int' in declaration of `strcpy'
foo.c:27: ANSI C forbids data definition with no type or storage class
foo.c:28: parse error before `->'
make: *** [foo] Error 1

Perhaps getting it to compile relatively cleanly would be a sound first step
towards a solution.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #3

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

Similar topics

4
by: Chris | last post by:
Could anyone please help me. IS there something wrong with it? char * function getString() { char myString; sprintf(myString, "hello world");
6
by: greenflame | last post by:
I have been working for some time on a script that will show a matrix with the elements aligned on the right for sometime and finally got it to work. Then I did some patching up and ran the script...
1
by: Adam Monsen | last post by:
Is there anything wrong with using something like super(type(self), self).f() to avoid having to hardcode a type? For example: class A(object): def f(self): print "in A.f()" class B(A): def...
3
by: Roubles | last post by:
Hi All, Here's my problem, I have a bunch of code that passes an allocated object (say obj) to a function, and then dereferences that allocated object when the function returns: foo(obj);...
4
by: QQ | last post by:
Hi Here is part of my code typedef struct{ int len; char code; }Code; typedef struct{ .... Code *a;
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.