Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:52:02+00:00 2026-06-13T10:52:02+00:00

I’m having a newbie problem in C++. I have a struct Edge defined as

  • 0

I’m having a newbie problem in C++.
I have a struct Edge defined as following:

struct Edge {
    int position[4];
    int average;
};

Now I need to create a lot of these structs and I created a helper-method for that, that creates an array of these structs based on some parameters:

Edge* createEdges(int some_parameters){
    Edge *edges = new Edge[8];

    for(int i = 0 ; i < 8; i++){
        Edge edge;
        edge.position[0] = 1; //fill the rest of the edge array in the same manner
        edge.average = 10;

        edges[i] = edge;
    }

    return edges;
}

However when I now call: Edge *edges = createEdges(int some_parameters) there is no sensible data in the Edge array (out of scope?).

I think I’m mixing up some things here but I would prefer if I can make this work without resorting to the vector datastructures. Is this the normal way of handling this or should I declare the edge array myself and pass it to the helper method to fill it up?

Edit:

First of all, I want to thank everyone for there comments/hints/tips/advice/… they have helped me to find the problem that I overlooked so easily.
After I seen the replies that the code should work, I tested the simplified code aswell (something I should have done in the 1st place) and surprisingly, it worked! So then I checked to see why my real code didn’t work and the simplified version did.
My real code looked like this:

Edge* createEdges(int some_parameters){
     Edge* edges = new Edge[8];
     if(some_parameter != 0){
          //create the edges as in my 1st snippet
          return NULL; //doh, should return edges here !
     } else { 
          return NULL;
     }
}

The reason why I had not seen that I simply returned the wrong value (NULL) was because the debugger showed me some 0xf6f6f6 addressess with some negative values for edge.position (something I don’t quite understand, it should’ve just show me 0x000000, perhaps I was just imagining things).

All in all, this was an important lesson in why to never code after 3am, nothing good will come of it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:52:03+00:00Added an answer on June 13, 2026 at 10:52 am

    Here is a bit modified version that creates edges in the function, and returns a vector containing all created edges.

    #include <iostream>
    #include <vector>
    
    struct Edge {
        int position[4];
        int average;
    };
    
    std::vector< Edge > createEdges(int some_parameters){
        std::vector< Edge > edges( 8 );
    
        for(int i = 0 ; i < 8; i++){
            const Edge edge{ { 1, 0, 0, 0 }, 10 };
    
            edges[i] = edge;
        }
    
        return edges;
    }
    
    
    int main()
    {
        auto e( createEdges(5));
    
        std::cout<<e[0].average<<std::endl;
    }
    

    There are few modifications :

    1. Instead of allocating an array of edges, and returning pointer, I return vector
    2. the way edge objects are created, and initialized (I think this is what is asked)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.