LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2006, 11:39 AM   #1
kickzha
LQ Newbie
 
Registered: Sep 2004
Location: augsburg homeboy
Distribution: debian sarge/sid
Posts: 17

Rep: Reputation: 0
Objects in C??


Hello,

stupid question from beginner - maybe.

I improve my beginner know how, and will call a function that displays source through libgtksourceview.

That function and it's callbacks is embedded in his own source-file, and has one header-file.

I defined a structure, which is declared in the function like this
Code:
	static codeview_data	data = {0};
and push it to the callpacks in the gpointer data argument of the g_signal_connect call.

The structure needs to be allocated statically, because the function builds up the gui, thread leaves ( and would free codeview_data if it isn't static ), and the main function calls gtk_main().

How to opereate to call the function several times using a new version of the structure without deleting the previous one ??

(i think similar to <object>.new() in c++, but dont know what to talk like that because not even familiar with)

I tried to allocate memory
Code:
codeview_data *data = malloc ( sizeof ( codeview_data ) );
but that doesnt work...tips anyhow??


greets chab

Last edited by kickzha; 06-17-2006 at 11:46 AM.
 
Old 06-17-2006, 01:32 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
malloc() returns a pointer to void; you have to cast the result so that it's compatible with the type you're assigning it to. In your example:
Code:
codeview_data* data = (codeview_data*)malloc(sizeof(codeview_data));
 
Old 06-17-2006, 04:12 PM   #3
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 48
Quote:
Originally Posted by kickzha
but that doesnt work...tips anyhow??
Can you tell us what "doesn't work"? Show us your code and the error.

Quote:
Originally Posted by taylor_venable
malloc() returns a pointer to void; you have to cast the result so that it's compatible with the type you're assigning it to.
AFAIK this is unnecessary in C. void * should be compatible with any other pointer type.
 
Old 06-17-2006, 04:18 PM   #4
kickzha
LQ Newbie
 
Registered: Sep 2004
Location: augsburg homeboy
Distribution: debian sarge/sid
Posts: 17

Original Poster
Rep: Reputation: 0
...oh, im sorry.

To my surprise (never would have expected out of that trivial (by that complexcity) perspectivity) taylor_venable seems to be right - the error doesn't occur by now.

But at the moment i can't tell if it is really the fact, because i hang on another part.

I would have answered, and said: 'Thank you very much !!'

...but wouldn't have said that until i am not sure for that it works right.

But i think it does.


@taylor_venable: Thank you very much !!

I will tell more if im sure.


thx,
greets

chabayo
 
Old 06-17-2006, 04:48 PM   #5
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,133

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Quote:
Originally Posted by taylor_venable
malloc() returns a pointer to void; you have to cast the result so that it's compatible with the type you're assigning it to. In your example:
Code:
codeview_data* data = (codeview_data*)malloc(sizeof(codeview_data));
This isn't necessary in C if you #include <stdlib.h> and in fact might be dangerous in very rare circumstances. it is necessary in C++ due to changes in the type system.
 
Old 06-17-2006, 06:56 PM   #6
kickzha
LQ Newbie
 
Registered: Sep 2004
Location: augsburg homeboy
Distribution: debian sarge/sid
Posts: 17

Original Poster
Rep: Reputation: 0
...somehow it does the work now like that:
Code:
codeview_data *data = malloc ( sizeof ( codeview_data ) );
& i don't know why.

The truth is i had included the stdlib.h, and unistd.h a.s.o.

Now i just took the needet ( that includes stdlib, but not unistd ) .

Makes that sense??

LOL (rofl, he said LOL)

cu
chab
 
Old 06-17-2006, 08:38 PM   #7
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
To spooon & btmiller:

A professor once told me you always have to cast a void* to the type of whatever variable you assign it to... however, on my system with GCC 3.4.4, it works with or without the cast, regardless of whether or not I #include <stdlib.h>. So you may very well be right, and I think I've also heard what you said before, but I for one can't back it up empirically. (Wouldn't be the first time a professor told me something wrong, though. )

Apologies if I was wrong, kickzha, but at least it seems to be working now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cache all objects xnet_online Linux - Software 1 04-28-2006 01:09 PM
How to cast objects lucky6969b Programming 1 12-08-2005 06:41 PM
java objects zaicheke Programming 3 03-23-2005 06:21 AM
lost objects gurra59 Linux - Newbie 4 09-07-2003 04:29 PM
Objects in Java? mikeshn Programming 0 06-11-2002 10:21 AM


All times are GMT -5. The time now is 10:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration