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

  • SEARCH
  • Home
  • 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 8287993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:11:08+00:00 2026-06-08T12:11:08+00:00

In an std::vector of a non POD data type, is there a difference between

  • 0

In an std::vector of a non POD data type, is there a difference between a vector of objects and a vector of (smart) pointers to objects? I mean a difference in the implementation of these data structures by the compiler.

E.g.:

class Test {
   std::string s;
   Test *other;
};

std::vector<Test> vt;
std::vector<Test*> vpt;

Could be there no performance difference between vt and vpt?

In other words: when I define a vector<Test>, internally will the compiler create a vector<Test*> anyway?

  • 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-08T12:11:09+00:00Added an answer on June 8, 2026 at 12:11 pm

    In other words: when I define a vector, internally will the compiler create a vector anyway?

    No, this is not allowed by the C++ standard. The following code is legal C++:

    vector<Test> vt;
    Test t1; t1.s = "1"; t1.other = NULL;
    Test t2; t2.s = "1"; t2.other = NULL;
    vt.push_back(t1);
    vt.push_back(t2);
    Test* pt = &vt[0];
    pt++;
    Test q = *pt; // q now equal to Test(2)
    

    In other words, a vector “decays” to an array (accessing it like a C array is legal), so the compiler effectively has to store the elements internally as an array, and may not just store pointers.

    But beware that the array pointer is valid only as long as the vector is not reallocated (which normally only happens when the size grows beyond capacity).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an std::vector of handle objects. I have to wait on these handle
Is there a way to construct a new std::vector with uninitialized (non-zero) values or
I get the following error: error: conversion from 'std::vector<shape*>::const_iterator to non-scalar type 'std::vector<shape*>::iterator Can
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
If std::vector and friends are self resizing, does that mean if I declare a
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
I have an std::vector that holds all the objects and is passed to them
Is there a way to make a non-resizeable vector/array of non-reassignable but mutable members?
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
There is the class A containing two overloaded methods getItems(); typedef std::vector <int> TItems;

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.