LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-30-2010, 01:14 AM   #1
amused
LQ Newbie
 
Registered: Jan 2010
Posts: 6
Blog Entries: 1

Rep: Reputation: 0
memory allocation for struct in c++


How do we allocate memory of struct?

what i did was


Code:
int main()
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"and it is located at"<<struct amp*s = malloc(sizeof(struct amp))<<endl;
it gives me an error---

In funtion 'int main()':
error: expected primary-expression before 'struct'
error: expected ';' before 'struct'

Please help me with this.. i tried looking it on net but could not find it..
 
Old 03-30-2010, 01:20 AM   #2
amused
LQ Newbie
 
Registered: Jan 2010
Posts: 6
Blog Entries: 1

Original Poster
Rep: Reputation: 0
memory allocation for struct in c++

How do we allocate memory of struct?

what i did was

Code:
int main()
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"and it is located at"<<struct amp*s = malloc(sizeof(struct amp))<<endl;
it gives me an error---

In funtion 'int main()':
error: expected primary-expression before 'struct'
error: expected ';' before 'struct'

Please help me with this.. i tried looking it on net but could not find it..
 
Old 03-30-2010, 01:26 AM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Why would you do something perverse like mix cout (C++) with "malloc()" ?

Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.

You have several alternatives:
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
} s;

int
main(int argc, char *argv[])
{
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from stack ("automatic" variable)
  struct amp s;
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from heap 
  // "new" is a C++ operator, "malloc()" is a C/C++ standard library function
  struct amp *s = new struct amp;
  cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";

Last edited by paulsm4; 03-30-2010 at 01:27 AM.
 
Old 03-30-2010, 01:31 AM   #4
amused
LQ Newbie
 
Registered: Jan 2010
Posts: 6
Blog Entries: 1

Original Poster
Rep: Reputation: 0
memory location

Thanks for the code.

but my problem was on memory location, that is my second part struct amp*s = malloc(sizeof(struct amp)). please help me with this.

Thanks again.
Quote:
Originally Posted by paulsm4 View Post
Why would you do something perverse like mix cout (C++) with "malloc()" ?

Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.

You have several alternatives:
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
} s;

int
main(int argc, char *argv[])
{
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from stack ("automatic" variable)
  struct amp s;
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from heap 
  // "new" is a C++ operator, "malloc()" is a C/C++ standard library function
  struct amp *s = new struct amp;
  cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";
 
Old 03-30-2010, 01:33 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Just like deja vu all over again

Anyway, the problem is mixing a declaration ("struct amp*s) in with an expression.

You have several alternatives:
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
} s;

int
main(int argc, char *argv[])
{
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from stack ("automatic" variable)
  struct amp s;
  cout <<"The size of 'struct' is"<< sizeof(s)<<"\n";
Code:
struct amp
{
    string z;
    char y;
    float x;
    int w;
};

int
main(int argc, char *argv[])
{
  // Allocate from heap 
  // "new" is a C++ operator, "malloc()" is a C/C++ standard library function
  struct amp *s = new struct amp;
  cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"\n";
 
Old 03-30-2010, 01:40 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I have merged your 2 duplicate threads---next time, only one thread per topic---thanks
 
Old 03-30-2010, 01:52 AM   #7
amused
LQ Newbie
 
Registered: Jan 2010
Posts: 6
Blog Entries: 1

Original Poster
Rep: Reputation: 0
i am not getting it.. i just wanna know where is struct located.. please help me with finding how to find the memory location OF struct.

Last edited by amused; 03-30-2010 at 02:14 AM.
 
Old 03-30-2010, 04:11 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by amused View Post
i am not getting it.. i just wanna know where is struct located.. please help me with finding how to find the memory location OF struct.
If your variable is a pointer then it's value is the address location.
Otherwise you can find the address by using the & operator.

Why do you want to find the memory location of struct?
 
Old 03-30-2010, 01:00 PM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Graemef is absolutely correct:
Quote:
If your variable is a pointer then it's value is the address location.
Otherwise you can find the address by using the & operator.
For example:
Code:
#include <stdio.h>

#define MAX_STRING 80

struct amp
{
    char z[MAX_STRING+1];
    char y;
    float x;
    int w;
};

int 
main(int argc, char *argv[])
{
  struct amp my_struct, *my_struct_p;

  my_struct_p= &my_struct;
  printf ("addr(my_struct)= 0x%x == 0x%x...\n", 
    &my_struct, my_struct_p);
  return 0;
}
'Hope that helps .. PSM
 
Old 03-30-2010, 01:04 PM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,234

Rep: Reputation: 189Reputation: 189
You cannot declare there.
Quote:
Originally Posted by amused View Post
How do we allocate memory of struct?

what i did was


Code:
int main()
struct amp //<-- WTF?
{
    string z;
    char y;
    float x;
    int w;
};

cout <<"The size of 'struct' is"<< sizeof(struct amp)<<"and it is located at"<<struct amp*s = malloc(sizeof(struct amp))<<endl;
it gives me an error---

In funtion 'int main()':
error: expected primary-expression before 'struct'
error: expected ';' before 'struct'

Please help me with this.. i tried looking it on net but could not find it..
 
  


Reply

Tags
allocation, c++, memory, struct


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
Memory allocation morfeus80 Linux - Newbie 5 01-28-2008 03:51 PM
memory allocation esael Linux - General 7 01-12-2008 01:12 PM
Help - memory allocation in C zaichik Programming 3 09-04-2005 11:16 AM
memory allocation docGonzo2000 Linux - General 1 05-16-2003 10:22 PM
memory allocation. raven Programming 5 09-08-2002 02:50 PM


All times are GMT -5. The time now is 10:20 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration