LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-29-2006, 09:10 AM   #1
phugoid
LQ Newbie
 
Registered: Mar 2006
Location: Dubai, United Arab Emirates
Distribution: Suse 10.0
Posts: 14

Rep: Reputation: 0
Usage of struct in C++ caused unexpected behavior


I could use some help understanding the output from the program below, which I compiled using gcc 4.0.2:
Code:
#include <iostream>
using namespace std;

struct charptr {
    char* m;
};

int main() {

    charptr foo;

    *(foo.m) = 'c';
    cout << (*(foo.m)) << '\n';

}
Output:
Code:
c
     23621:
     23621:     calling fini: /usr/lib/libstdc++.so.6 [0]
     23621:
     23621:
     23621:     calling fini: /lib/tls/libm.so.6 [0]
     23621:
     23621:
     23621:     calling fini: /lib/libgcc_s.so.1 [0]
     23621:
     23621:
     23621:     calling fini: /lib/tls/libc.so.6 [0]
     23621:
If I replace the line:
cout << (*(foo.m)) << '\n';
with
cout << "Stuff here" << (*(foo.m)) << '\n';
I get a segmentation fault at runtime intead.

I thought *(foo.m) in this case represents "the char value pointed to by foo.m", and I should set and read it this way.
 
Old 04-29-2006, 09:40 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
You can compile that with gcc? How do you do that?! Actually this is not C, but C++, so you should compile with g++. Hmmm..., however you have to allocate memory for 'foo.m' using the 'new' keyword. Otherwise you'll run into a segfault.
Code:
foo.m = new char;
 
Old 04-29-2006, 11:59 AM   #3
phugoid
LQ Newbie
 
Registered: Mar 2006
Location: Dubai, United Arab Emirates
Distribution: Suse 10.0
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks Flesym. I realize now that foo.m was pointing to an undefined location before I started using it.

I do compile with g++, I was just quoting the result of doing "c++ -v"... Sorry for confusion.
 
Old 04-29-2006, 12:00 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Code:
 
#include <iostream>
using namespace std;

struct charptr {
    char* m;
};

int main() {

    charptr foo;  //maybe this should be struct charptr foo; 

    *(foo.m) = 'c';
    cout << (*(foo.m)) << '\n';

}
Not sure if C++ uses struct the same way C does but if it does im suprized this even compiles.

in C it would look something like this
Code:
 

#include<stdio.h> 
  struct charptr { 
                  char *m; 
                 } 
int main () 
{ 
   struct charptr foo; // creates the pointer but it points to nothing
   char letter;  //Creates a place in memory to store the data 
   letter = 'c'; //assiens the data to the address space 

   foo.m = &letter;  // points the pointer to the address in         
                     //   memory that contains the data. 
   

   printf("%c\n",*foo.m); //derefrence the pointer to get the data 
   return 0; 
}

Last edited by exvor; 04-29-2006 at 12:57 PM.
 
Old 04-29-2006, 03:48 PM   #5
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Yes it compiles and it is perfectly correct C++ syntax. C++ is very different from C.
 
Old 04-29-2006, 04:46 PM   #6
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
Just follow Fleysm's advice. You didn't allocate any memory there for it. You'll get unpredictable results without allocating memory properly. As far as "gcc" or "g++," its the same thing. The real issue is the extension of the filename, compiling a .c with g++, invokes the C compiler, compiling a .cpp with gcc invokes the C++ compiler.
 
  


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
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
Unexpected disk Usage varunbihani Linux - Software 1 05-06-2004 08:42 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
Unexpected Behavior (passwd command) Penguin666 Linux - General 3 11-19-2003 11:34 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

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

All times are GMT -5. The time now is 02:56 AM.

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