Thread: Downcasting to access a variable?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Downcasting to access a variable?

    Let's say you have a base class that is going to be the value for a node in your linked list
    Code:
    struct Entity {
           int x, int y;
    };
    However, when you need to do a specific operation in the loop of the linked list you need a variable that's in the derived class, say a bullet.
    Code:
    ((Bullet*) (&tmp->e))->lifeTime
    I can write and read to this variable from downcasting, but immediately after the program crashes. If you need the full source here it is: [C Intermediate Language] #include <SDL.h> #include <SDL_image.h> typedef enum Type { PLAYER1, PL - Pastebin.com. If you have any other criticisms of the code feel free to share: I'm very new to C. Thanks

  2. #2
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    Here's a slimmed down version of the code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct Entity {
        int x, y;
    } Entity;
    
    typedef struct Player {
        Entity e;
        int health;
    } Player;
    
    typedef struct Bullet {
        Entity e;
        int lifeTime;
    } Bullet;
    
    int main() {
    
        Entity *e = malloc(sizeof *e);
    
        ((Bullet *)e)->lifeTime = 5;
    
        e->x = 32;
        e->y = 55;
    
        printf("%d , %d , %d", e->x, e->y, ((Bullet *)e)->lifeTime);
    
        free(e);
    
        return 0;
    }
    Last edited by MrData; 07-20-2016 at 09:33 PM. Reason: More data

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    On this line, you allocate space for an Entity object, not a Bullet object:
    Code:
    Entity *e = malloc(sizeof *e);
    Therefore, this line effectively attempts to access memory out of bounds:
    Code:
    ((Bullet *)e)->lifeTime = 5;
    You could try this instead:
    Code:
    Entity *e = malloc(sizeof(Bullet));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why can't I access the variable?
    By Ramcin Oudishu in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2014, 07:58 PM
  2. Loosing inherted data members. Trying Downcasting.
    By Swerve in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2009, 01:10 PM
  3. Access restriction for external variable..
    By bhupesh.kec in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 04:01 AM
  4. global variable access, from c to c++
    By manav in forum C++ Programming
    Replies: 17
    Last Post: 04-28-2008, 03:48 AM
  5. Problem with variable access
    By slippy in forum Tech Board
    Replies: 4
    Last Post: 09-19-2007, 02:54 PM

Tags for this Thread