Jump to content
  • Advertisement
Sign in to follow this  
BiiXteR

Why can't I print out my std::string vector?

This topic is 673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For some reason my application crashes when I try to print out the values of my std::vector, which is filled with std::strings.

 

Here's how I declare the vector : 

	std::vector <std::string> map
	{
		"###############################################################",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"###############################################################",
	};

And here's how I try to print it out : 

	for (int i = 0; i < map.size(); i++)
	{
		printf(map[i].c_str());
	}

The crash message I get is : 

 

Exception thrown at 0x01157216 in ConsoleGame.exe: 0xC0000005: Access violation reading location 0x00000018.

Edited by BiiXteR

Share this post


Link to post
Share on other sites
Advertisement

Weird; that code runs fine for me. (It's missing newline characters though)

 

Forgot to mention that the error leads me to line 512 in the file xstring.

Share this post


Link to post
Share on other sites
Try to put together the smallest program that shows the problem. It is likely that you will figure out what the problem is in the process of producing that program. If not, post it here, so we can reproduce the problem on our own.

Share this post


Link to post
Share on other sites

Access violation reading location 0x00000018.

 
 
Looks like you are calling a member function on a null object.
 
Such as:
 
myThing = null;
myThing->DoSomething();
 
Look on your stack trace, go back out on the stack to the outermost function that you wrote, and look for a null pointer on any object.

For example, maybe a null pointer value for the string you are trying to print.
 
 
 

Forgot to mention that the error leads me to line 512 in the file xstring.

 
You're looking in the wrong spot.  While it is remotely possible that you found a bug in the standard library, consider that the libraries are used by millions of programmers around the globe every day.  The odds that it works correctly for everybody else but is broken for you is astonishingly small.  
 
Always assume the error is in your code, not in the standard library. 
 

  for (int i = 0; i < map.size(); i++)
    {
        printf(map.c_str());
    }


Following a hunch, it could be that map doesn't exist, or that it doesn't contain a string (it is empty). Edited by frob

Share this post


Link to post
Share on other sites

Are you by any chance using XCode?

 

Btw one more thing: It's a security risk to do printf( myCStringVariable ); Perform instead printf( "%s", myCStringVariable );

 

Cheers

Share this post


Link to post
Share on other sites

Are you by any chance using XCode?
 
Btw one more thing: It's a security risk to do printf( myCStringVariable ); Perform instead printf( "%s", myCStringVariable );
 
Cheers


Could you elaborate on why that's a security risk?

Chances are what he really wanted was `std::puts(myCStringVariable)' anyway.

Share this post


Link to post
Share on other sites

Are you by any chance using XCode?
 
Btw one more thing: It's a security risk to do printf( myCStringVariable ); Perform instead printf( "%s", myCStringVariable );
 
Cheers


Could you elaborate on why that's a security risk?


Suppose myCStringVariable contains "%s".
Then we have a statement here that can be rewritten as printf("%s").
This will try to print out whatever is on the stack where printf would look for its second argument - only there isn't one.

This could be anything - a valid pointer, a null pointer, arbitrary data - even executable machine code. An attacker could use this to crash the program, or look at the stack frame and work out where the current function's return address is, or do all manner of nasty things one can do with printf specifiers that point at garbage.

The %n specifier in the wrong hands could be particularly nasty because it allows printf to modify an argument passed to it... Edited by Oberon_Command

Share this post


Link to post
Share on other sites

 

Access violation reading location 0x00000018.

 
 
Looks like you are calling a member function on a null object.
 
Such as:
 
myThing = null;
myThing->DoSomething();
 
Look on your stack trace, go back out on the stack to the outermost function that you wrote, and look for a null pointer on any object.

For example, maybe a null pointer value for the string you are trying to print.
 
 
 

Forgot to mention that the error leads me to line 512 in the file xstring.

 
You're looking in the wrong spot.  While it is remotely possible that you found a bug in the standard library, consider that the libraries are used by millions of programmers around the globe every day.  The odds that it works correctly for everybody else but is broken for you is astonishingly small.  
 
Always assume the error is in your code, not in the standard library. 
 

  for (int i = 0; i < map.size(); i++)
    {
        printf(map.c_str());
    }

Following a hunch, it could be that map doesn't exist, or that it doesn't contain a string (it is empty).

 

 

I checked the size of the vector, which for some reason is 0.

I don't understand why though, I've tried giving it a value in the constructor, and in the header file and it still has a value of 0.

 

Here's some more code : 

 

MapManager.h

#pragma once

#include <vector>
#include <iostream>

class MapManager
{

public:

	std::vector <std::string> map =
	{
		"###############################################################",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"###############################################################"
	};

	void DrawMap();

	static MapManager &GetInstance();

private:

	MapManager();
	~MapManager();

};

MapManager.cpp

#include "MapManager.h"



MapManager::MapManager()
{

}

void MapManager::DrawMap()
{

	std::cout << map[0].c_str() << std::endl;

	for (int i = 0; i < map.size(); i++)
	{
		printf(map[i].c_str());
	}
}

MapManager &MapManager::GetInstance()
{
	MapManager temp;
	return temp;
}

MapManager::~MapManager()
{
}

Edited by BiiXteR

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!