LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 08-07-2005, 10:42 AM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
sizeof and malloc in C


Hey eveyone im trying to learn better by messing around with malloc and sizeof but im getting strange resluts.



Code:
 

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


int main () 

{ 


    char *test; 



    test = (char *) malloc (200); 



    printf("The size of the memory is %d\n",sizeof (test)); 

    free(test); 

    printf("The size of the memory is %d\n",sizeof (test)); 


    return 0; 

}

is there some reason I should be getting only 4 on output ? no mater what
I change the malloc number to?

OUTPUT
------------------
The size of the memory is 4
The size of the memory is 4
 
Old 08-07-2005, 11:04 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Yes. When you do sizeof(test) the result is 4 because a pointer to char is 4 bytes on your machine. Sizeof tells you the size of a particular data type (such as int or pointer to char). Unfortunately, there's no portable way to tell the size of a chunk of alloced memory other than to keep track of it when you alloc it.
 
Old 08-07-2005, 11:18 AM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
damn


i even tryed using sizeof(&test) but i got 1's instead
 
Old 08-07-2005, 11:35 AM   #4
cetialphav
Member
 
Registered: Sep 2003
Location: Raleigh, NC, USA
Distribution: Fedora
Posts: 88

Rep: Reputation: 16
You have to understand that sizeof() is not a function call. It purely deals with types and that is something the compiler can determine at compile time. It looks like a function, but it really is a macro. So it looks at sizeof(temp) and converts that to sizeof(char *) and knows that that is 4 bytes. For sizeof(&temp), it sees sizeof(char), which is 1 byte. All of this computation happens at compile time, not runtime.
 
Old 08-07-2005, 12:06 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
LOL

I tried creating a program that would try and guess what the size of a malloc return was but no go i would have to alredy know what it was before i could figure it out.
 
  


Reply



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
sizeof problem ltordsen Programming 8 12-05-2005 09:13 AM
about malloc eshwar_ind Programming 11 02-18-2004 03:41 PM
sizeof( ) in c++ question amoor757 Programming 6 01-26-2004 02:42 PM
sizeof my typedef... jhorvath Programming 7 10-11-2002 05:41 PM
sizeof() and C/C++ boku Programming 2 09-01-2002 07:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration