Strcmp generate a core dump












2















So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.



{
public:
bool CheckFoo(const char* word);

protected:
typedef std::unordered_map<std::string, bool> word_map;
word_map words_map;
};




bool CheckFoo(const char* word)
{
if (words_map.empty())
{
return false;
}

auto it = words_map.begin();

while (it != words_map.end())
{
const std::string &r = it->first;
const char* tmp = word;

if (strcmp(tmp, r.c_str() ) == 0)
{
return true;
}
}

return false;
}




if (    CheckFoo("wordFoo") )
{
// bla bla
}


The problem is that those codes generate a .core dump file..
Do you see any mistakes in my codes?



The crash core analyze point me to strcmp line










share|improve this question




















  • 1





    is this C or C++? Tag appropriately if its not C please.

    – Bwebb
    Nov 22 '18 at 0:14











  • you should check if r == null before r.c_str(), and notice that tmp not initialized

    – Dennis Vash
    Nov 22 '18 at 0:16


















2















So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.



{
public:
bool CheckFoo(const char* word);

protected:
typedef std::unordered_map<std::string, bool> word_map;
word_map words_map;
};




bool CheckFoo(const char* word)
{
if (words_map.empty())
{
return false;
}

auto it = words_map.begin();

while (it != words_map.end())
{
const std::string &r = it->first;
const char* tmp = word;

if (strcmp(tmp, r.c_str() ) == 0)
{
return true;
}
}

return false;
}




if (    CheckFoo("wordFoo") )
{
// bla bla
}


The problem is that those codes generate a .core dump file..
Do you see any mistakes in my codes?



The crash core analyze point me to strcmp line










share|improve this question




















  • 1





    is this C or C++? Tag appropriately if its not C please.

    – Bwebb
    Nov 22 '18 at 0:14











  • you should check if r == null before r.c_str(), and notice that tmp not initialized

    – Dennis Vash
    Nov 22 '18 at 0:16
















2












2








2








So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.



{
public:
bool CheckFoo(const char* word);

protected:
typedef std::unordered_map<std::string, bool> word_map;
word_map words_map;
};




bool CheckFoo(const char* word)
{
if (words_map.empty())
{
return false;
}

auto it = words_map.begin();

while (it != words_map.end())
{
const std::string &r = it->first;
const char* tmp = word;

if (strcmp(tmp, r.c_str() ) == 0)
{
return true;
}
}

return false;
}




if (    CheckFoo("wordFoo") )
{
// bla bla
}


The problem is that those codes generate a .core dump file..
Do you see any mistakes in my codes?



The crash core analyze point me to strcmp line










share|improve this question
















So i have a std::unordered_map, i want to acces to strings stored intro this map. I want to search intro intro all words inside the map and compare with a given word. If the strings are same then continue execution of the if statement.



{
public:
bool CheckFoo(const char* word);

protected:
typedef std::unordered_map<std::string, bool> word_map;
word_map words_map;
};




bool CheckFoo(const char* word)
{
if (words_map.empty())
{
return false;
}

auto it = words_map.begin();

while (it != words_map.end())
{
const std::string &r = it->first;
const char* tmp = word;

if (strcmp(tmp, r.c_str() ) == 0)
{
return true;
}
}

return false;
}




if (    CheckFoo("wordFoo") )
{
// bla bla
}


The problem is that those codes generate a .core dump file..
Do you see any mistakes in my codes?



The crash core analyze point me to strcmp line







c strncmp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 0:16







Armin

















asked Nov 22 '18 at 0:12









ArminArmin

183




183








  • 1





    is this C or C++? Tag appropriately if its not C please.

    – Bwebb
    Nov 22 '18 at 0:14











  • you should check if r == null before r.c_str(), and notice that tmp not initialized

    – Dennis Vash
    Nov 22 '18 at 0:16
















  • 1





    is this C or C++? Tag appropriately if its not C please.

    – Bwebb
    Nov 22 '18 at 0:14











  • you should check if r == null before r.c_str(), and notice that tmp not initialized

    – Dennis Vash
    Nov 22 '18 at 0:16










1




1





is this C or C++? Tag appropriately if its not C please.

– Bwebb
Nov 22 '18 at 0:14





is this C or C++? Tag appropriately if its not C please.

– Bwebb
Nov 22 '18 at 0:14













you should check if r == null before r.c_str(), and notice that tmp not initialized

– Dennis Vash
Nov 22 '18 at 0:16







you should check if r == null before r.c_str(), and notice that tmp not initialized

– Dennis Vash
Nov 22 '18 at 0:16














2 Answers
2






active

oldest

votes


















3














Can't write comments yet but,



Like Nunchy wrote, tmp is not defined in that context.
I also noticed that your code never increments the map iterator, which would result in a never ending loop.



I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.



Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.






share|improve this answer
























  • never increments the map iterator

    – Armin
    Nov 22 '18 at 0:34











  • this was the issue thanks

    – Armin
    Nov 22 '18 at 0:35



















0














You are declaring temp then referencing tmp which doesn't exist:



    const char* temp = word;

if (strcmp(tmp, r.c_str() ) == 0)


Does this compile? Surely it should be:



    const char* temp = word;

if (strcmp(temp, r.c_str() ) == 0)


?






share|improve this answer


























  • Sorry sorry, my mistake. I wrote bad, i edited

    – Armin
    Nov 22 '18 at 0:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422192%2fstrcmp-generate-a-core-dump%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Can't write comments yet but,



Like Nunchy wrote, tmp is not defined in that context.
I also noticed that your code never increments the map iterator, which would result in a never ending loop.



I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.



Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.






share|improve this answer
























  • never increments the map iterator

    – Armin
    Nov 22 '18 at 0:34











  • this was the issue thanks

    – Armin
    Nov 22 '18 at 0:35
















3














Can't write comments yet but,



Like Nunchy wrote, tmp is not defined in that context.
I also noticed that your code never increments the map iterator, which would result in a never ending loop.



I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.



Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.






share|improve this answer
























  • never increments the map iterator

    – Armin
    Nov 22 '18 at 0:34











  • this was the issue thanks

    – Armin
    Nov 22 '18 at 0:35














3












3








3







Can't write comments yet but,



Like Nunchy wrote, tmp is not defined in that context.
I also noticed that your code never increments the map iterator, which would result in a never ending loop.



I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.



Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.






share|improve this answer













Can't write comments yet but,



Like Nunchy wrote, tmp is not defined in that context.
I also noticed that your code never increments the map iterator, which would result in a never ending loop.



I'm assuming you did not copy your actual code into your post but instead rewrote it hastily which resulted in some typos, but if not, try making sure you're using temp and not tmp in your call to strcmp, and make sure the loop actually increments the iterator.



Like one of the comments on your post points out as well, make sure you actually have data in the map, and the function parameter.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 0:20









PeterPeter

8319




8319













  • never increments the map iterator

    – Armin
    Nov 22 '18 at 0:34











  • this was the issue thanks

    – Armin
    Nov 22 '18 at 0:35



















  • never increments the map iterator

    – Armin
    Nov 22 '18 at 0:34











  • this was the issue thanks

    – Armin
    Nov 22 '18 at 0:35

















never increments the map iterator

– Armin
Nov 22 '18 at 0:34





never increments the map iterator

– Armin
Nov 22 '18 at 0:34













this was the issue thanks

– Armin
Nov 22 '18 at 0:35





this was the issue thanks

– Armin
Nov 22 '18 at 0:35













0














You are declaring temp then referencing tmp which doesn't exist:



    const char* temp = word;

if (strcmp(tmp, r.c_str() ) == 0)


Does this compile? Surely it should be:



    const char* temp = word;

if (strcmp(temp, r.c_str() ) == 0)


?






share|improve this answer


























  • Sorry sorry, my mistake. I wrote bad, i edited

    – Armin
    Nov 22 '18 at 0:16
















0














You are declaring temp then referencing tmp which doesn't exist:



    const char* temp = word;

if (strcmp(tmp, r.c_str() ) == 0)


Does this compile? Surely it should be:



    const char* temp = word;

if (strcmp(temp, r.c_str() ) == 0)


?






share|improve this answer


























  • Sorry sorry, my mistake. I wrote bad, i edited

    – Armin
    Nov 22 '18 at 0:16














0












0








0







You are declaring temp then referencing tmp which doesn't exist:



    const char* temp = word;

if (strcmp(tmp, r.c_str() ) == 0)


Does this compile? Surely it should be:



    const char* temp = word;

if (strcmp(temp, r.c_str() ) == 0)


?






share|improve this answer















You are declaring temp then referencing tmp which doesn't exist:



    const char* temp = word;

if (strcmp(tmp, r.c_str() ) == 0)


Does this compile? Surely it should be:



    const char* temp = word;

if (strcmp(temp, r.c_str() ) == 0)


?







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 0:20

























answered Nov 22 '18 at 0:14









NunchyNunchy

825411




825411













  • Sorry sorry, my mistake. I wrote bad, i edited

    – Armin
    Nov 22 '18 at 0:16



















  • Sorry sorry, my mistake. I wrote bad, i edited

    – Armin
    Nov 22 '18 at 0:16

















Sorry sorry, my mistake. I wrote bad, i edited

– Armin
Nov 22 '18 at 0:16





Sorry sorry, my mistake. I wrote bad, i edited

– Armin
Nov 22 '18 at 0:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422192%2fstrcmp-generate-a-core-dump%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python