LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   memory allocation for struct in c++ (http://www.linuxquestions.org/questions/programming-9/memory-allocation-for-struct-in-c-798795/)

amused 03-30-2010 12:14 AM

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..

amused 03-30-2010 12:20 AM

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..

paulsm4 03-30-2010 12:26 AM

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";


amused 03-30-2010 12:31 AM

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 (Post 3917528)
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";



paulsm4 03-30-2010 12:33 AM

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";


pixellany 03-30-2010 12:40 AM

I have merged your 2 duplicate threads---next time, only one thread per topic---thanks

amused 03-30-2010 12:52 AM

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.

graemef 03-30-2010 03:11 AM

Quote:

Originally Posted by amused (Post 3917552)
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?

paulsm4 03-30-2010 12:00 PM

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

smeezekitty 03-30-2010 12:04 PM

You cannot declare there.
Quote:

Originally Posted by amused (Post 3917521)
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..



All times are GMT -5. The time now is 09:30 PM.