# 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.

## 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 on other sites
Weird; that code runs fine for me. (It's missing newline characters though)

##### Share on other sites

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 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 on other sites

Thinking it could be from other sections of your program.

##### Share on other sites

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 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 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 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 on other sites

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

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 14
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631754
• Total Posts
3002104
×